Professional Documents
Culture Documents
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
i. b % c * a 2 % 4 * 5 2 * 5 10
2. Using parentheses, rewrite the following expressions to correctly indicate their order of evaluation. Then evaluate each expression assuming a = 5, b = 2, and c = 4. a. a % b * c && c % b * a 5 % 2 * 4 && 4 % 2 * 5 1 * 4 && 0 * 5 4 && 0 0 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
3. Write relational expressions to express the following conditions (use variable names of your own choosing): a. The distance is equal to 30 feet. distance == 30 b. The ambient temperature is 86.4. temp == 86.4 c. A speed is 55 MPH. speed == 55 d. The current month is 12 (December). month == 12 e. The letter input is K. letterIn == K f. A length is greater than two feet and less than three feet. length > 2 && length < 3 g. The current day is the 15th day of the 1st month. day == 15 && month == 1 h. The automobiles speed is 35 MPH and its acceleration is greater than 4 MPH per second. speed == 35 && acceleration > 4 i. An automobiles speed is greater than 50 MPH and it has been moving for at least 5 hours. speed > 50 && time >= 5 j. The code is less than 500 characters and takes more than 2 microseconds to transmit. code < 500 && transmit > 2
4. Determine the value of the following expressions, assuming a = 5, b = 2, c = 4, and d = 5. a. a == 5 5 == 5 1 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. b * d == c * c 2 * 5 == 4 * 4 10 == 16 0
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;
2. Write if statements corresponding to the conditions illustrated by each of the following flow charts. a) if (ace < 25) sum = sum + a; else count = count +1; c) if (id > 22) factor = 7;
= 5; 10;
= 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; }
3. The grade level of undergraduate college students is typically determined according to the following schedule: Number of Credits Completed Grade Level Less than 32 Freshman 32 to 63 Sophomore 64 to 95 Junior 96 or more Senior Using this information, write a C++ program that accepts the number of credits a student has completed, determines the students grade level, and displays the grade level. #include <iostream> using namespace std; int main() { int numOfCredits; cout << "Please enter the number of credits : "; cin >> numOfCredits; if (numOfCredits < 32) cout << "Grade Level else if ((numOfCredits >= cout << "Grade Level else if ((numOfCredits >= cout << "Grade Level else cout << "Grade Level return 0; }
is Freshman" <<endl; 32) && (numOfCredits <= 63)) is Sophomore" <<endl; 64) && (numOfCredits <= 95)) is Junior" <<endl; is Senior" <<endl;
4. A students letter grade is calculated according to the following schedule: Numerical grade Letter grade Greater than or equal to 90 A Less than 90 but greater than or equal to 80 B Less than 80 but greater than or equal to 70 C Less than 70 but greater than or equal to 60 D Less than 60 F Using this information, write a C++ program that accepts a students numerical grade, converts the numerical grade to an equivalent letter grade, and displays the letter grade. #include <iostream> using namespace std; int main() { int numGrade; char letGrade; cout << "Please enter the students numerical grade : "; cin >> numGrade; if (numGrade > 90) letGrade = A; else if ((numGrade < letGrade = B; else if ((numGrade < letGrade = C; else if ((numGrade < letGrade = D; else letGrade = F; cout << "Your Letter return 0; }
90) && (numGrade >= 80)) 80) && (numGrade >= 70)) 70) && (numGrade >= 60))
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; } }
2. Rewrite the following if-else chain using a switch statements: if (factor == 1) pressure = 25.0; else if (factor == 2) pressure = 36.0; else if (factor == 3) pressure = 45.0; else if (factor == 4) || (factor == 5) || (factor == 6) pressure = 49.0;
4. Rewrite Program 4.5 using a switch statement. #include <iostream> using namespace std; int main() { char code; cout << "Enter a specification code: "; cin >> code; switch (code) { case 's': case 'S': cout << "The item is space exploration grade."; break; case 'm': case 'M': cout << "The item is military grade."; break; case 'c': case 'C': cout << "The item is commercial grade."; break; case 't': case 'T': cout << "The item is toy grade."; break; default: cout << "An invalid code was entered."; } return 0; }
Prepared by: Miss Azlina Din