Chapter 5BLab Assignment

Page 1 of 2

Name ______

Lab - Chapter 5B

Objectives:At the end of this assignment you should be able to:

  1. use a switch statement
  2. convert a while loop to a do-while loop
  3. convert a do-while loop to a while loop
  4. convert a while loop to a for loop
  5. convert a for loop to a while loop
  6. convert a for loop to a do-while loop

Part 1: From the C++ compiler open an existing C++ program. The name of the program isCounting.

  1. Debug the program so that the code will count the number of each type of punctuation mark in the file.
  2. Compile and run the program
  3. Add the code that also counts blank spaces.

Save a copy of the program and its output.

Part 2: Make sure that the sample output includes loop termination.

a.Type the following code segment in a C++ program. Declare variables as needed. Run the program to make sure it gives correct output. Now rewrite the loop using a do-while loop instead of a while loop. You should not have any unnecessary code in your program. When your modified program produces the sameresult,savethe program and some sample output.

cout < “Enter 1, 2, or 3: ”; < endl;

cin > response;

while (response != 1 & response != 2 & response != 3)

{

cout < “Enter 1, 2, or 3: “; < endl;

cin > response;

}

b.Type the following code segment in a C++ program. Declare variables as needed. Run the program to make sure it gives correct output. Now rewrite the loop using a while loop instead of a do-while loop. When your modified program produces the sameresult,savethe program and some sample output.

cout < “Enter an integer “ < endl;

cin > intNumber;

if (cin)

do

{

cout < intNumber < endl;

cout < “Enter an integer “ < endl;

cin > intNumber;

} while (cin);

cout < “Processing is terminated” < endl;

c.Type the following code segment in a C++ program. Declare variables as needed. Run the program to make sure it gives correct output. Now rewrite the loop using using a for loop instead of a while loop. When your modified program produces the same result,save the program and its output. You should not have any unnecessary code in your new program.

sum = 0;

count = 1;

while (count <= 1000)

{

sum = sum + count;

count++;

}

cout < “The sum is “ < sum < endl;

cout < “The count is “ < count < endl;

d.Type the following code segment in a C++ program. Declare variables as needed. Run the program to make sure it gives correct output. Now rewrite the loopusing a while loop instead of a for loop. When your modified program produces the same result,savethe program and its output.

for (m = 23; m >= 5; m--)

cout < m < ‘ ‘ < m * m < endl;

e.Type the following code segment in a C++ program. Declare variables as needed. Run the program to make sure it gives correct output. Now rewrite the loop usinga do-while loop instead of a for loop. When your modified program produces the same result,save the program and its output.

for (k = 9; k <= 21; k++)

cout < k < ‘ ‘ < 3 * k < endl;

When you have completed the entire lab, you should e-mail the assignment to me. The subject line should be – C++ CHAP5B LAB

The followingthirteen files should be attached to the email

This assignment with your name

Program for Lab5B-Part 1 and its output file

Program for Lab5B-Part2-a and its output file

Program for Lab5B-Part2-b and its output file

Program for Lab5B-Part2-c and its output file

Program for Lab5B-Part2-d and its output file

Program for Lab5B-Part2-e and its output file