Professional Documents
Culture Documents
AnsTutCH4 PDF
AnsTutCH4 PDF
AnsTutCH4 PDF
Exercises 4.1
1. Determine the value of the following expressions. Assume a = 5, b =
2, c = 4, d = 6, and e = 3.
a. a > b
5 > 2
1
b. a != b
5 != 2
1
c. d % b == c % b
6 % 2 == 4 % 2
0 == 0
1
d. a * c != d * b
5 * 4 != 6 * 2
20 != 12
1
e. d * b == c * e
6 * 2 == 4 * 3
12 == 12
1
f. !(a * b)
!(5 * 2)
!(10)
0
g. !(a % b * c)
!(5 % 2 * 4)
!(1 * 4)
!(4)
0
h. !(c % b * a)
!(4 % 2 * 5)
!(0 * 5)
!(0)
1
i. b % c * a
2 % 4 * 5
2 * 5
10
c. b % c * a && a % c * b
2 % 4 * 5 && 5 % 4 * 2
2 * 5 && 1 * 2
10 && 2
1
b. a % b * c || c % b * a
5 % 2 * 4 || 4 % 2 * 5
1 * 4 || 0 * 5
4 || 0
1
d. b % c * a && a % c * b
2 % 4 * 5 || 5 % 4 * 2
2 * 5 || 1 * 2
10 || 2
1
b. b * d == c * c
2 * 5 == 4 * 4
10 == 16
0
c.
d
5
1
4
0
1
% b * c > 5 || c % b * d < 7
% 2 * 4 > 5 || 4 % 2 * 5 < 7
* 4 > 5 || 0 * 5 < 7
> 5 || 0 < 7
|| 1
b. If the temperature is above 100 degrees display the message above the
boiling point of water else display the message below the boiling
point of water.
if ( temp > 100)
cout << above the boiling point of water;
else
cout << below the boiling point of water;
c. If the number is positive add the number to possum, else add the number
to negsum.
if( number > 0)
possum += number;
else
negsum += number;
d. If the slope is less than 0.5 set the variable flag to zero, else set
flag to one.
if( slope < 0.5 )
flag = 0;
else
flag = 1;
e. If the difference between volts1 and volts2 is less than 0.001, set the
variable approx to zero, else calculate approx as the quantity (volts1
volts2)/2.0.
if ( volts1 volts2 < .001 )
approx = 0;
else
approx = (volts1 volts2) / 2.0;
f. If the frequency is above 60, display the message The frequency is too
high.
if ( frequency > 60 )
cout << The frequency is too high;
g. If the difference between temp1 and temp2 exceeds 2.3, calculate error
as (temp1 temp2) * factor.
if ( temp1 temp2 > 2.3 )
error = (temp1 temp2) * factor;
b) if (c == 15)
{ volts
pwr =
}
else
{ volts
pwr =
}
= 5;
10;
d) if (count == 10)
{ average = sum / count;
cout << average;
}
= 16;
25;
3. Write a C++ program that asks the user to input two numbers. If the first
numbered entered is greater than the second number the program should
print the message The first number is greater. Else it should print the
message The first number is smaller. What do you think your program will
display if the two numbers entered is equal? Test this case.
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter integer number one : ";
cin >> num1;
cout << "Enter integer number two : ";
cin >> num2;
if (num1 == num2)
cout << Number 1 is equal to number 2.
else if (num1 > num2)
cout << "The first number is greater.";
else
cout << " The first number is smaller.";
return 0;
}
Exercises 4.3
1. Modify Program 4.5 to accept both lower and uppercase letters as codes.
For example, if a user enters either an m or an M, the program should
display the message The item is military grade.
#include <iostream>
using namespace std;
int main()
{
char code;
cout << "Enter a specification code: ";
cin >> code;
if ((code == 's') || (code == 'S'))
cout << "The item is space exploration grade.";
else if ((code == 'm') || (code == 'M'))
cout << "The item is military grade.";
else if ((code == 'c') || (code == 'C'))
cout << "The item is commercial grade.";
else if ((code == 't') || (code == 'T'))
cout << "The item is toy grade.";
else
cout << "An invalid code was entered.";
return 0;
}
b) sum = 0;
if (count < 5)
{
if (grade < 50)
fail = fail + 1;
}
is Freshman" <<endl;
32) && (numOfCredits <= 63))
is Sophomore" <<endl;
64) && (numOfCredits <= 95))
is Junior" <<endl;
is Senior" <<endl;
switch (letterGrade)
{
case A: cout << The numerical grade is between 90 and 100\n;
break;
case B: cout << The numerical grade is between 80 and 89.9\n;
break;
case C: cout << The numerical grade is between 70 and 79.9\n;
break;
case D: cout << How are you going to explain this one\n;
break;
default:
{
cout(Of course I had nothing to do with my grade.\n;
cout(It must have been the lecturers fault.\n;
}
}
switch (factor)
{
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
}