CS240 Review2

1. Assuming that the variables are defined and initialized as follows:

int num1 = 9 , num2 = 5 , num3 = 10;

float fnum = 12.50;

char ch = ‘P’;

A. Evaluate the following conditions (using short circuit evaluation whenever possible):

a) 2 * num1 - 5 >= 9 || fnum / 2 + 10 <= 6.5

b)num1 + 5 = = 24 || num1 – 1 > num2 - 5

c) num1 + num2 == num3 + 5 & 2 * num3 <= 4 * num2

d) 2 * num1 - 5 >= 9 & fnum / 2 + 10 <= 6.5

e) num1 - num2 <= num1 / 3 || num1 * num2 > 100

f) ! (num1 + 5 <= 13)

g) num1 - 5 >= num3 || num2 < 15 & num1 >= 9

B. Which of the following conditions are equivalent (that means have the same true value)?

a) num1 != 0d) !num1

b) num1 == 0e) !(num1 == 0)

c) num1

2.Given the following definitions of variables:

int num1 = 5, num2 = 17;

bool flag = false,cond1, cond2 = true;

What is the value stored into variable cond1 after the evaluation of each of the following statements:

a.cond1 = (num1 <= 7);

b.cond1 = (flag || (num2 < 20));

c.cond1 = ((num2 < 15) & (num1 > 10));

d.cond1 = ((num2 > 35) || cond2 & (num1 < 15));

3.a.Specify an algorithm (using a flowchart) to read an integer value and to determine if it is a multiple of 5. If it is a multiple of 5, print it with the message “MULTIPLE OF 5”, otherwise, print it with the message NOT MULTIPLE OF 5.”

b.Write the C++ code segment that corresponds to the algorithm in question 3a.

4.a.Specify an algorithm (using and a flowchart) to read a character value and to determine if it is a letter of the alphabet. If it is, print it with the message “LETTER”; otherwise print it with the message “NOT A LETTER.”

b.Write the C++ code segment that corresponds to the algorithm in question 4a.

5. a.Specify an algorithm (using a flowchart) to read an integer value into a variable num and to do the following: if the value read is greater than 10, subtract 2 from it and print the result; otherwise, add 3 to it and print the result. Finally, multiply the new value of variable num by 5, and print the result.

b.Write the C++ code segment that corresponds to the algorithm in question 5a.

6. a.Specify an algorithm (using a flowchart) to read two integer values and to do the following: if the first value is greater than the second, compute the sum and the difference of the first value minus the second, and print the results; otherwise, compute the product of both integer values, and the quotient in the division of the second value by 2, and print the results. Regardless of the values read, compute two times the first value minus the second value, and print the result.

b. Write the C++ code segment that corresponds to the algorithm in question 6a.

7.a.A department store offers a rebate of 5% if the total amount of the purchase is at least 100 and a rebate of 2% otherwise. A sale tax of 8.25 % is also imposed on the price of each purchase (after the rebate). Specify an algorithm (using a flowchart) to read the unit price and the number of items purchased, and to output the amount of the rebate and the price (including taxes) of the purchase.

b.Write the C++ program that corresponds to the algorithm in question 7a.

8. Assuming that all variables are properly defined and initialized, what are the error(s) in each of the following program segments:

a.cinnum;

if (num = 5)

sum = num + 4;

else

sum = num + 10;

b.cinnum;

if (num > 5 & <= 10)

cout(num + 15);

else

cout(num - 3);

c.cinnum;

if (num < 15)

result1 = 2 * num;

result2 = num + 20;

else

result1 = 5 * num;

result = result1 + result2;

cout “\nThe result is:\t” result;

9. Trace the execution of the following program segment and show its output for each of the following input values:

a. input: 4 b. input: 20

Line #Statements

1cinnum;

2if (num <10 )

{

3num = num + 6;

4 cout endl “num =\t” num;

}

else

5cout endl “num / 4 =\t” (num 4);

6cout endl “result =\t” (2 * num);

10. a.Specify an algorithm (using a flowchart) to read 50 temperature values in Fahrenheit and to convert them to Celsius. You convert a Fahrenheit temperature to Celsius by using the following formula: Celsius = 5.0 / 9 (Fahrenheit - 32).

b.Write the C++ code segmentthat corresponds to the algorithm in question 10a.

11.a.A client would like to purchase 20 types of products in a store. Specify an algorithm (using a flowchart) to read the unit price and the number of items of each product and to compute the total price of all these products.

b.Write the C++ code segmentthat corresponds to the algorithm in question 11a.

12.a.Specify an algorithm (using a flowchart) to read ten integer values , to compute their product and to output the result.

b.Write the C++ program that corresponds to the algorithm in question 12a.

13. a.Specify an algorithm (using a flowchart) to add the first 20 positive integer values ( 1 + 2 + 3 + . . . + 20) and to output the result.

b. Write the C++ code segmentthat corresponds to the algorithm in question 13a.

14. a. Specify an algorithm (using a flowchart) to add the first 20 positive multiples of 5 ( 5 + 10 + 15 + 20 + . . . ) and to output the result.

b.Write the C++ code segmentthat corresponds to the algorithm in question 14a.

15. a.Specify an algorithm (using a flowchart) to read a double precision floating-point value, to calculate its 6th power ( n * n * n * n * n * n), and to output the result.

b. Write the C++ code segmentthat corresponds to the algorithm in question 14a.

16. a.Specify an algorithm (using a flowchart) to generate the squares (n*n) of the integer values 20, 18, 16, 14, . . . , 2, starting with 20.

b.Write the C++ code segmentthat corresponds to the algorithm in question 16a.

17. a. Specify an algorithm (using a flowchart) to read 20 non zero integer values and to count the number of positive values and the number of negative values, and to output the results.

b.Write the C++ code segmentthat corresponds to the algorithm in question 17a.

18. a. Specify an algorithm (using a flowchart) to read 20 non zero integer values and to compute the average of positive values and the average of negative values, and to output the results.

b. Write the C++ code segmentthat corresponds to the algorithm in question 18a.

19. The Fibonacci sequence is the sequence: 1, 1, 2, 3, 5, 8, . . . , such that the first and the second values are 1, and the value at any position n >= 3 is the sum of the values at the two previous positions: the value at position 3 is the sum of the values at positions 2 and 1; the value at position 4 is the sum of the values at positions 3 and 2, . . . etc.

a.Using two integer variables previous1 and previous2 to hold the values at the two previous positions from the current position, and a variable new to hold the value at the current position, specify an algorithm to read an integer value greater than or equal to 3, and to output the Fibonacci sequence from position 3 to the position that corresponds to the value read. For example, if the input value is 8, the output will be: 2, 3, 5, 8, 13, 21.

b.Write the C++ code segmentthat corresponds to the algorithm in question 19a.

20. Assuming that all variables are properly defined, find the mistake(s) in the specification of each of the following while structures:

a.sum = 0; c. count = 0;

while(count < 10)sum = 0;

{ while(count < 10 )

count = 0; {

cin num; cin>num;

sum = sum + num; sum = sum + num;

count = count + 1; }

}

b.sum = 0; d. sum = 0;

count = 10; while (count < 10 )

while ( count > 1); {

{ cin>num;

sum = count + 5; sum = sum + num;

count = count - 2; count = count + 1;

} }

21. Each of the following code segments is supposed to read 10 integer values and to compute their sum. What is wrong in each of these code segments?

a.count = 0; b. count = 0;

while (count < 10 ) while (count < 10 )

{ {

sum = 0; cin>num;

cinnum; sum = sum + num;

sum = sum + num; count = count + 1;

count = count + 1; }

}

22. The manager of a store is required to make a deposit of $ 10,000 whenever the total sales in the store is $ 10,000 or more.

a. Specify an algorithm (using a flowchart) to read the daily sales in that store, and to compute their sum until the total sales is greater than or equal to $ 10,000. The total sales and the message “it is time to make a deposit” are then printed.

b. Write the C++ code segmentthat corresponds to the algorithm in question 22a.

23. A store has 120 units of a product and every time there is a sale, the amount of units sold is deducted from the stock until it becomes less than or equal to 30. The manager of the store must then place a new order.

a. Specify an algorithm (using a flowchart) to read the number of units sold daily and to update the current stock until it becomes less than or equal to 30. The message “place a new order” and the amount of units to purchase in order to have 120 units are then printed.

b. Write the C++ program that corresponds to the algorithm in question 23a.

24. A positive integer value is a prime number if it is only divisible by 1 and by itself.

a.Specify an algorithm (using a flowchart) to read a positive integer value greater than 2 and to output a message indicating whether it is a prime number or not.

To find out whether a positive integer value n greater than 2 is a prime number, you may repeatedly divide it by 2, 3, 4, . . . , until the remainder is 0 or the divisor times the divisor (divisor squared) is greater than n. The given value is a prime number if the remainder is not 0; otherwise, it is not a prime number.

b. Write the C++ program that corresponds to the algorithm in question 24a.

25. a. Specify an algorithm (using a flowchart) to read an integer value greater than 1 and to compute its greatest divisor that is less than the value itself. For example, if the value is 15, the answer is 5, and if the value is 13, the answer is 1.

To compute the greatest divisor of an integer value n greater than 1 that is less than n, repeatedly divide that value n by i= 2, then 3, then 4, . . . etc., until you get a remainder of 0 or i *i>= n. If the remainder is 0, then the greatest divisor is n/i, otherwise, it is 1.

b. Write the C++ code segmentthat corresponds to the algorithm in question 25a.

26. a.Specify an algorithm (using a flowchart) to read the prices of one or more books, and to compute their average price. Book prices are input one after another, and terminated with the sentinel value -99.00.

b.Write the C++ code segmentthat corresponds to the algorithm in question 26a.

27. a.Specify an algorithm (using a flowchart) to read one or more temperature values in Fahrenheit and to convert them to Celsius. The sentinel value -99 marks the end of the input temperatures.

You convert a Fahrenheit temperature to Celsius by using the following formula:

Celsius = 5.0 / 9 (Fahrenheit - 32).

b. Write the C++ code segmentthat corresponds to the algorithm in question 27a.

28. a. Specify an algorithm (using a flowchart) to read the pay rate and the number of hours of work of one or more employees and to do the following:

1) compute each employee’s gross pay: pay rate * number of hours.

2) compute its tax deduction as follows: 15 % of the gross pay if it is less than $ 1,200; otherwise, 19 % of the gross pay.

3) compute the net pay: gross pay - tax deduction.

The dummy pay rate -99.00 marks the end of the input values.

b. Write the C++ code segmentthat corresponds to the algorithm in question 28a.

29. a. Specify an algorithm (using a flowchart) to read one or more positive integer values and to compute their product. The sentinel value 0 marks the end of the input values.

b.Write the C++ program that corresponds to the algorithm in question 29a.

30.a.Specify an algorithm (using a flowchart) to read the test scores of one or more students (until the sentinel value -99 is read) and to compute their average.

b.Write the C++ code segmentthat corresponds to the algorithm in question 30a.

31. The following program that should read a list of integer values and compute their sum until the sentinel value -99 is read, contains some error(s). Rewrite it with these errors corrected.

#include<iostream>

using namespace std;

#defineDUMMYVALUE-99

int main( )

{

int total, value;

cout“\nEnter all values to add followed by”

DUMMYVALUE <“ to stop\n”;

while ( value != DUMMYVALUE )

{

cinvalue;

total = total + value;

}

coutendl “The total of these value is:\t” <total;

return( 0 );

}

32. a.In a department store, a 10% rebate is given for every purchase of at least $ 20.00. Specify an algorithm (using a flowchart) to read the unit price of a product, the quantity purchased, and to compute the amount of the purchase after the rebate if applicable.

b.Write the C++ code segmentthat corresponds to the algorithm in question 32a.

33. a.Specify an algorithm (using a flowchart) to read 50 integer values and to determine the number of even values and print it.

b.Write the C++ code segmentthat corresponds to the algorithm in question 33a.

34. a.Specify an algorithm (using a flowchart) to read one or more integer values and to compute the average of positive values and print it. If there are no positive values, print an appropriate message.

b. Write the C++ program that corresponds to the algorithm in question 34a.

35. a.greatt, smallt, and somet are integer variables. Specify an algorithm (using a flowchart) to read an integer value and to do the following: add 1 to smallt if the value read is less than 5; then add 2 to greatt if the value read is greater than 20, otherwise subtract 3 from smallt. Then, regardless of the value read add 7 to somet.

b.Write the C++ code segmentthat corresponds to the algorithm in question 35a.

36. a. Specify an algorithm (using a flowchart) to carry out the following tasks: read a value into the integer variable num. if the value read is greater than 100, add 1 to variable large_ct; if it is less than 50, add 2 to variable small_ct; otherwise, add 3 to variable middle_ct; regardless of the value read, add it to variable sum.

b.Write the C++ code segmentthat corresponds to algorithm in question 36a.

37.What is the output of the following program segment for the following input values:

a. input value: 4 b. input value: 20

cinnum;

if( num >= 5)

{

pnum = num + 7;

coutendl < “pnum=\t” < pnum;

}

cout“result=\t” < (num - 1);

38. a.The decision table below shows fines imposed for speeding violations. Specify an algorithm (using a flowchart) to read the speed of a car and to output the corresponding fine.

Speed <= 50 mphFine = $ 0.00

50 < Speed <= 70 Fine = $ 15.00

70 < Speed <= 80 Fine = $ 30.00

Speed > 80 Fine = $ 60.00

b. Write the C++ code segmentsegment that corresponds to the algorithm in question 38a.

39. The instructor of a course assigns letter grades according to the average score as follows:

LETTER GRADE AVERAGE SCORE

A score >= 90.00

B 80.00 <= score < 90.00

C 70.00 <= score < 80.00

D 60.00 <= score < 70.00

F score < 60.00

a. Specify an algorithm (using a flowchart) to read a student’s letter grade and to output the range of his/her average score. For example, if it reads “A”, it will output the message “your average score is greater than or equal to 90.00", and if it reads “C”, it will output the message, “your average score is greater than or equal to 70.00, but less than 80.00."

b.Write the C++ code segmentsegment that corresponds to the algorithm in question 39a.

40. A game consists of selecting a positive integer value and rewarding bonuses to the player according to the remainder in the division of that number by 5 as follows:

REMAINDER BONUS

0 200

1 350

2 400

3 500

4 750

a. Specify an algorithm (using a flowchart) to read a positive integer value and to output the bonus received by the user.

b. Write the C++ code segmentsegment that corresponds to the algorithm in question 40a.

41.A florist sells four different types of bouquets of flowers that he has numbered 101, 202, 303, and 404 with the unit prices specified as follows:

ITEM NUMBERUNIT PRICE

101 $ 5.25

202 $ 3.10

303 $ 9.75

404 $ 6.50

a.Specify an algorithm (using a flowchart) to read an item number and the number of bouquets of that type purchased, and to compute and print the total price of those bouquets.

b.Write the C++ code segmentsegment that corresponds to the algorithm in question 41a.

42. a.Specify an algorithm (using a flowchart) to read an integer value, an operator ( +, -, *, %, or / ), and another integer value in this order (for example, 23 + 5), and to compute the expression consisting of the first value, followed by the operator, which is followed by the second value, and to output the result. Note that in the division of the first value by the second, you must make sure that the second value is not zero.

b. Write the C++ code segmentsegment that corresponds to the algorithm in question 42a.

43. A department store offers rebates to customers according to the price of the items purchased as follows:

Price >= $ 100.00 Rebate = $ 10.00

$ 50 <= Price < $ 100.00Rebate = $ 5.00

$ 20 <= price < $ 50.00 Rebate = $ 2.00

price < $ 20.00 No rebate

A sale tax of 8.25% is also imposed on the price of each purchase (after the rebate, if any).

a. Specify an algorithm (using a flowchart) to read a product unit price and the number of items purchased, and to output the rebate received by the customer, the total price (including the rebate), and the amount due (including taxes).

b. Write the C++ code segmentsegment that corresponds to the algorithm in question 43a.

44. Write a program segment to read a character from the keyboard and to determine if it is a letter (‘a’ - ‘z’ or ‘A’ - ‘Z’), a digit (‘0' - ‘9'), or a special character (any other character). If it is a letter, print the message “letter”; if it is a digit, print the message “digit”; and if it is a special character, print the message “special character”.

45. What is the output of the following program segment for the following input values:

a.input value: 3 b. input value: 7 c. input value: 12

cinnum;

if (num < 5)

cout"Red\n";

else if (num < 10)

cout "Yellow\n";

else

cout "Green\n";

cout“blue”

46. Trace the execution of each of the following program segments:

a. 1num = 1; b. 1 ndx = 1;

2while (num <= 20) 2 pnum = 60;

3num = num * 2; 3 while(ndx < 5)

4cout < “\nnum=” < num; {

4 pnum = pnum / ndx;

5 ndx = ndx + 2;

}

6cout < “\npnum=” < pnum

< “\nndx=” < ndx;