You are on page 1of 8

Worksheet __________________________________________________________________________

Chapter-3
WORKSHEET ON CHAPTER-3
1. True or false: Both of the following if statements perform the same operation.
a. if (calls == 20)
rate *= 0.5;
b. if (calls = 20)
rate *= 0.5;
2. Will the if/else statement shown below function exactly the same as the two separate if statements?
if (x < y) if (x < y)
cout << 1; cout << 1;
else if (x > y)
cout << 2; cout << 2;
3. Assume the variables a = 2, b = 4, and c = 6. Is the following expression true or false?
b > a || b > c && c = = 5
4. Assume the variables a = 2, b = 4, and c = 6. Indicate by circling the T or F if each of the following
conditions is true or false:
a) a == 4 || b > 2
b) 6 <= c && a > 3
c) 1 != b && c != 3
d) a >= -1 || a <= b
e) !(a > 2)
5. Rewrite the following using the ! operator so that the logic remains the same.
if (activeEmployee == false)
6. Write an if statement that multiplies payRate by 1.5 when hours is greater than or equal to 40.
7. Write an if/else statement that assigns 1 to x when y is equal to 100. Otherwise it should assign 0 to x.
8. Write an if/else statement that assigns 0.10 to commission unless sales is greater than or equal to
50,000.00, in which case it assigns 0.20 to commission.
9. Write a C++ statement that prints the message “The number is not valid.” if the variable hours is
outside the range 0 through 80.
10. Write C++ statements that assign the correct value to discount, using the logic described.
a. Assign 0.20 to discount if dept equals 5 and price is $100 or more.
b. Assign 0.15 to discount if dept is anything else and price is $100 or more.
c. Assign 0.10 to discount if dept equals 5 and price is less than $100.
d. Assign 0.05 to discount if dept is anything else and price is less than $100.
11. Write nested if statements that perform the following test: If amount1 is greater than 10 and amount2 is
less than 100, display the greater of the two.
12. Rewrite the following using the ! operator so that the logic remains the same.
if (activeEmployee == false)
13. Rewrite the following if/else statements as conditional expressions.
a. if (hours > 40)
wages *= 1.5;
else
wages *= 1;

b. if (result >= 0)
cout << "The result is positive\n";
else

________________________________________________________________________________2012
1
Fundamentals of Programming I
Worksheet __________________________________________________________________________
Chapter-3
cout << "The result is negative.\n";

14. Rewrite the following conditional expressions as if/else statements.


a. j = k > 90 ? 57 : 12;
b. factor = x >= 10 ? y * 22 : y * 35;
c. total += count = = 1 ? sales : count * sales;
d. cout << ((num % 2) = = 0) ? "Even\n" : "Odd\n");

15. Explain why you cannot convert the following if/else if statement into a switch statement.
if (temp = = 100)
x = 0;
else if (population > 1000)
x = 1;
else if (rate < .1)
x = -1;
16. What is wrong with the following switch statement?
switch (temp)
{
case temp < 0 :
cout << "Temp is negative.\n";
break;
case temp = = 0:
cout << "Temp is zero.\n";
break;
case temp > 0 :
cout << "Temp is positive.\n";
break;
}
17. What will the following program display on the screen when it executes?
#include <iostream.h>
int main()
{
int boolval;
boolval = (12 > 1);
cout<<"\nBoolean value: "<< boolval;
cout<<"\nBoolean value: "<< (2 < 0);
boolval = (5 = = (3 * 2));
cout<<"\nBoolean value: "<< boolval;
cout<<"\nBoolean value: "<< (5 = = 5);
return 0;
}
18. Rewrite the following program using a switch statement.
#include <iostream.h>
int main()
{
int selection;

________________________________________________________________________________2012
2
Fundamentals of Programming I
Worksheet __________________________________________________________________________
Chapter-3
cout << "Which formula do you want to see?\n\n";
cout << "1. Area of a circle\n";
cout << "2. Area of a rectangle\n";
cout << "3. Area of a cylinder\n"
cout << "4. None of them!\n";
cin >> selection;
if (selection = = 1)
cout << "Pi times radius squared\n";
else if (selection = = 2)
cout << "Length times width\n";
else if (selection = = 3)
cout << "Pi times radius squared times height\n";
else if (selection == 4)
cout << "Well okay then, good-bye!\n";
else
cout << "Not good with numbers, eh?\n";
return 0;
}
19. What will the following program print if 40 is entered for test1 and 30 for test2?
#include <iostream>
using namespace std;
int main()
{
cout << "Enter your first test score: ";
int test1;
cin >> test1;
cout << "Enter your second test score: ";
int test2;
cin >> test2;
int sum = test1 + test2;
if (sum > 50)
{
test1 += 10;
test2 += 10;
int sum = test1 + test2;
}
cout << "test 1: " << test1 << endl;
cout << "test 2: " << test2 << endl;
cout << "sum : " << sum << endl;
return 0;
}
20. What will the following program display?
#include <iostream.h>
int main()
{
const int UPPER = 8, LOWER = 2;

________________________________________________________________________________2012
3
Fundamentals of Programming I
Worksheet __________________________________________________________________________
Chapter-3
int num1, num2, num3 = 12, num4 = 3;
num1 = num3 < num4 ? UPPER : LOWER;
num2 = num4 > UPPER ? num3 : LOWER;
cout << num1 << " " << num2 << endl;
return 0;
}
21. Write a C++ program which when two integers x and y are input will output the absolute difference of
the two integers. That is both (x-y) and (y-x) operations return a positive result. Run a test data to
confirm the correctness of your program.

22. Write a C++ program which will compute the area of a square ( ) or a triangle (

) after prompting the user to type (S or s), to compute the area of a square, or
(T or t), to compute the area of a triangle. The values for side, base and height should be entered from
the keyboard.

23. It is decided to base the fine for speeding in a built up area as follows: 250 birr if speed is between 31
and 40 kph, 450 birr if the speed is between 41 and 50 kph and 1000 birr if he speed is above 50 kph.
Write a C++ program to automate the assessment of fines assigned to a speed using if/else statements.
The program should accept the driving speed from a keyboard and display the computed fine on the
screen with an informative message.

24. What will the following program segments display?


a) x = 2;
y = x++;
cout << x << y;
b) x = 2;
y = ++x;
cout << x << y;
c) x = 2;
y = 4;
cout << x++ << --y;
d) x = 99;
if (x++ < 100)
cout "It is true!\n";
else
cout << "It is false!\n";
25. How many times will "Hello World\n" be printed in the following program segment?
int count = 10;
while (count < 1)
{
cout << "Hello World\n";
count++;
}

________________________________________________________________________________2012
4
Fundamentals of Programming I
Worksheet __________________________________________________________________________
Chapter-3
26. How many times will "I love C++ programming!\n" be printed in the following program
segment?
int count = 0;
while (count++ < 10)
cout << "Hello World\n";
cout << "I love C++ programming!\n";
27. What will the following program segments display?
e) int count = 10;
do
cout << "Hello World\n";
while (count++ < 1);
b) int v = 0;
do
cout << v++;
while (v < 5);

28. What will the following program segments display?


a) for (count = 0; count < 6; count++)
cout << (count + count);
b) for (value = -5; value; value++)
cout << value;
c) for (x = 3; x <= 14; x+=3)
cout << x << endl;
cout << 2 * x << endl;
29. Which loop (while, do-while, or for) would be best to use in the following situations?
a) The user must enter a set of exactly 14 numbers.
b) A menu must be displayed for the user to make a selection.
c) A calculation must be made an unknown number of times. (It is possible the calculation will
not be made at all.)
d) A series of numbers must be entered by the user, terminated by a sentinel value.
e) A series of values must be entered. The user specifies exactly how many.
30. How many times will the value in y be displayed in each of the following program segments?
f) for (x = 0; x < 20; x++)
{
for (y = 0; y < 30; y++)
cout << y << endl;
}
g) for (x = 0; x < 20; x++)
{
for (y = 0; y < 30; y++)
{
if (y > 10)
break;

________________________________________________________________________________2012
5
Fundamentals of Programming I
Worksheet __________________________________________________________________________
Chapter-3
cout << y << endl;
}
}
31. What will the following program segment display?
int x = 0, y = 0;
while (x++ < 5)
{
if (x == 3)
continue;
y += x;
cout << y << endl;
}
32. The following would be more appropriately written using a posttest loop. Convert the while
loop to a do-while loop:
char doAgain = ‘y’;
int sum = 0;
cout << "This code will increment sum 1 or more times.\n";
while (doAgain == 'y' || doAgain == 'Y')
{
sum++;
cout << "Sum has been incremented. Increment it again(y/n)?
";
cin >> doAgain;
}
cout << "Sum was incremented " << sum << " times.\n";

33. The following would be more appropriately written using a pretest loop. Convert the do-while
loop to a while loop. When you do this you will no longer need to use an if statement.
int number;
cout << "Enter an even number: ";
do
{
cin >> number;
if (number % 2 != 0)
cout << "Number must be even. Reenter number: ";
} while (number % 2 != 0);
34. Convert the following while loop to a for loop:
int count = 0;
while (count++ < 50)
{
cout << "count is " << count << endl;
}

________________________________________________________________________________2012
6
Fundamentals of Programming I
Worksheet __________________________________________________________________________
Chapter-3
35. Convert the following for loop to a while loop:
for (int x = 50; x > 0; x--)
{
cout << x << " seconds to go.\n";
}
36. What will each of the following program segments display?
h) int x = 1;
while (x < 10);
x++;
cout << x;
i) int x = 1;
while (x < 10)
x++;
cout << x;
j) for (int count = 1; count <= 10; count++)
{
cout << ++count << " "; // This is a bad thing to do!
}
k) for (int row = 1; row <= 3; row++)
{
cout << "\n$";
for (int digit = 1; digit <= 4; digit++)
cout << ‘9’;
}
37. Write an input validation loop that ensures that the number the user enters is in the range of 10
through 25.
38. Write an input validation loop that ensures that the character the user enters is Y, y, N, or n.
39. Write a for loop that displays every fifth number, starting at zero, through 100.
40. Write a for loop that repeats seven times, asking the user to enter a number. The loop should
also calculate the sum of the numbers entered.
41. Write a program that lets the user enter a number. The number should be multiplied by 2 and
printed until the number exceeds 50. Use a while loop.
42. Write a program that asks the user to enter two numbers. The numbers should be added and the
sum displayed. The user should be asked if he or she wishes to perform the operation again. If
so, the loop should repeat; otherwise it should terminate. Use a do-while loop.
43. Write a program that displays the following set of numbers, using for loop.
0, 10, 20, 30, 40, 50 . . . 1000

________________________________________________________________________________2012
7
Fundamentals of Programming I
Worksheet __________________________________________________________________________
Chapter-3
44. Write a program that displays the following output, using nested loop.
*****
*****
*****
45. Write a program with a for loop that calculates the total of the following series of numbers:

46. Write a program with a nested loop that displays 10 rows of ‘#’ characters. There should be 15
‘#’ characters in each row.
47. Write a program that asks the user for a positive integer value. The program should use a loop to
get the sum of all the integers from 1 up to the number entered. For example, if the user enters
50, the loop will find the sum of 1, 2, 3, 4, … 50.
Input Validation: Do not accept an input that is less than 1.
48. Write a program with a loop that lets the user enter a series of integers, followed by -99 to signal
the end of the series. After all the numbers have been entered, the program should display the
largest and smallest numbers entered.
49. Implement a C++ program to generate all powers of two up to the largest power less than
10,000.
50. Write a program to produce the sum of the following series, where n is entered by the user.

51. Write a program to produce an n times multiplication table (n less than or equal to 10). For
example, if n is equal to four the table should appear as follows:
1 2 3 4
1 1 2 3 4
2 2 4 6 8
3 3 6 9 12
4 4 8 12 16
Use nested for loops and pay attention to formatting your output so that it is displayed neatly.

________________________________________________________________________________2012
8
Fundamentals of Programming I

You might also like