Lab 5 2 Using the for Looping Repetition Structure
Lab 5 2 Using the for Looping Repetition Structure
The for looping structure, sometimes called a counted or indexed for loop, is a
specialized form of the while loop that you use to simplify writing count-controlled loops.
To use the for looping structure, you write a for statement, in which for is followed by
the initial statement, loop condition, and update statement enclosed within parentheses.
The program executes a for loop in the following sequence:
Objectives
In this lab, you create programs that use the for loop.
In the following exercises, you design and write programs that use for loops.
1a. Design a program that prompts the user to enter a positive integer. Use a for loop to
calculate and display the factorial value of that number. Increment the counter in your
for loop. The factorial value of a number is a number multiplied by every factor
between 1 and the number, inclusive. For instance, 3 factorial is 3 * 2 * 1. If the user
enters a negative number, display a message indicating that the program calculates only
positive numbers. Otherwise, display the result of the calculation.
Write your design in the following space. Your design should be a list of C++ comments
without any code.
1b. Write a C++ program based on the design you created in Exercise 1a and name it
factorial1.cpp. Step through the code by hand.
Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.
In the following space, show what is displayed on the screen after executing the output
message.
1c. Enter, compile, link, and execute factorial1.cpp. Then copy the output and save it in a
block comment at the end of your program. Save factorial1.cpp in the Chap05 folder of
your Student Data Files.
The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.
Use the following memory table to show what occurs in memory when the C++ code is
executed. (Include line numbers as documentation only. Do not use line numbers when
entering your final program.) To fill out the memory table, use one or two lines for each
variable. On one line, enter declaration information. Write the name of the declared
variable, its data type, and the line number at declaration.
In the following space, show what is displayed on the screen after executing the output
message.
2c. Enter, compile, link, and execute factorial2.cpp. Then copy the output and save it in a
block comment at the end of your program. Save factorial2.cpp in the Chap05 folder of
your Student Data Files.
The following is a copy of the screen results that might display after running your
program, depending on the data entered. The input entered by the user is shown in
bold.
3b. Write a C++ program based on the design you created in Exercise 3a and name it
roster2.cpp. Step through the code by hand.
3c. Enter, compile, link, and execute roster2.cpp. Then copy the output and save it in a
block comment at the end of your program. Save roster2.cpp in the Chap05 folder of
your Student Data Files.
Your screen results should be the same for roster1.cpp and roster2.cpp when running
the same data.