You are on page 1of 9

CLB 10503 - TUTORIAL CHAPTER 4

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

f. !(a * b) !(5 * 2) !(10) 0

b. a != b 5 != 2 1

c. d % b == c % b 6 % 2 == 4 % 2 0 == 0 1

g. !(a % b * c) !(5 % 2 * 4) !(1 * 4) !(4) 0

d. a * c != d * b 5 * 4 != 6 * 2 20 != 12 1

h. !(c % b * a) !(4 % 2 * 5) !(0 * 5) !(0) 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

Prepared by: Miss Azlina Din

CLB 10503 - TUTORIAL CHAPTER 4

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

Prepared by: Miss Azlina Din

CLB 10503 - TUTORIAL CHAPTER 4


Exercises 4.2 1. Write appropriate if statements for each of the following conditions: a. If an angle is equal to 90 degrees print the message The angle is a right angle. else print the message that The angle is not a right angle.
if( angle == 90) cout << The angle is a right angle; else cout << The angle is not a right angle;

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;

h. If x is greater than y and z is less than 20, read in a value for p.


if( x > y && z < 20 ) cin >> p;

Prepared by: Miss Azlina Din

CLB 10503 - TUTORIAL CHAPTER 4


i. If distance is greater than 20 and it is less than 35, read in a value for time.
if( distance > 20 && distance < 35 ) cin >> time;

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;

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; }

Prepared by: Miss Azlina Din

CLB 10503 - TUTORIAL CHAPTER 4


4. A small factory generates its own power with a 20-kilowatt generator and a 50-kilowatt generator. The plant manager indicates which generator is required by typing a character code. Write a C++ program that accepts this code as input. If code s is typed a message directing the plant foremen to use the smaller generator should be displayed; otherwise a message directing the use of the larger generator should be output. #include <iostream> using namespace std; int main() { char code; cout << "Enter a character code: "; cin >> code; if ((code == 's') || (code == 'S')) cout << "Use the smaller generator."; else cout << " Use the larger generator."; 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; }

Prepared by: Miss Azlina Din

CLB 10503 - TUTORIAL CHAPTER 4


2. Write nested if statements corresponding to the conditions illustrated in each of the following flowcharts. a) if (grade == 'A') { if (weight > 35) bin = 1; t = s + a; } b) sum = 0; if (count < 5) { if (grade < 50) fail = fail + 1; }

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;

Prepared by: Miss Azlina Din

CLB 10503 - TUTORIAL CHAPTER 4

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))

Grade is : " <<letGrade <<endl;

Prepared by: Miss Azlina Din

CLB 10503 - TUTORIAL CHAPTER 4


Exercises 4.4 1. Rewrite the following if-else chain using a switch statement: if (letterGrade == A) cout << The numerical grade is between 90 and 100\n; else if (letterGrade == B) cout << The numerical grade is between 80 and 89.9\n; else if (letterGrade == C) cout << The numerical grade is between 70 and 79.9\n; else if (letterGrade == D) cout << How are you going to explain this one\n; else { cout(Of course I had nothing to do with my grade.\n; cout(It must have been the lecturers fault.\n; }

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;

switch (factor) { case 1: case 2: case 3: case 4: case 5: case 6: }

pressure = 25.0; break; pressure = 36.0; break; pressure = 45.0; break;

pressure = 49.0; break;

Prepared by: Miss Azlina Din

CLB 10503 - TUTORIAL CHAPTER 4


3. Each disk drive in a shipment of these devices is stamped with a code from 1 through 4, which indicates a drive of manufacturers as follows: Code Disk Drive Manufacturer 1 3M Corporation 2 Maxell Corporation 3 Sony Corporation 4 Verbatim Corporation Write a C++ program that accepts the code number as an input and based on the value entered displays the correct disk drive manufacturer. #include <iostream> using namespace std; int main() { int code; cout << "Enter the code number: "; cin >> code; switch (code) { case 1: cout << "3M Corporation."; break; case 2: cout << "Maxell Corporation."; break; case 3: cout << "Sony Corporation."; break; case 4: cout << "Verbatim Corporation."; break; default: cout << "An invalid code number was entered."; } return 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

You might also like