0% found this document useful (0 votes)
2 views

Lab 5 5 Using Nested Control Structures

Lab 5.5 focuses on using nested control structures in programming, specifically within C++. The lab includes exercises that require designing and writing programs to count character occurrences in sentences and display a multiplication table. Participants will learn to recognize control structure boundaries and the behavior of nested loops through hands-on coding and memory tracking.

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 5 5 Using Nested Control Structures

Lab 5.5 focuses on using nested control structures in programming, specifically within C++. The lab includes exercises that require designing and writing programs to count character occurrences in sentences and display a multiplication table. Participants will learn to recognize control structure boundaries and the behavior of nested loops through hands-on coding and memory tracking.

Uploaded by

navid.panah1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab 5.

5 Using Nested Control Structures

One control structure can be nested (contained) within another control structure, and, in
turn, that structure can be nested within another control structure. The associated control
structures do not have to be of the same type.

Inner control structures close before outer control structures.

Objectives
In this lab, you use a nested control structure within a control structure.

After completing this lab, you will be able to:


• Recognize the beginning and end of control structures.
• Recognize that when an inner control structure is a for loop, the loop is reinitialized
with each iteration of an outer loop.

Estimated completion time: 60–75 minutes


Lab 5.5 Steps: Using Nested Control Structures
In the following exercises, you design and write programs that use nested control
structures.

1a. Critical Thinking Exercise: Design a program that prompts the user to enter a character
and a sentence. Count the number of times the character you entered appears in the
sentence. (Remember that an uppercase and a lowercase version of a letter are
different letters.) Display a message that tells how often the specified character appears
in the sentence. Your program should also allow the user to enter multiple sentences
until the user chooses to quit the program.

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
countChar.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.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when
Initialized

In the following space, show what is displayed on the screen after executing the output
message.

1c. Enter, compile, link, and execute countChar.cpp. Then copy the output and save it in a
block comment at the end of your program. Save countChar.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.
Enter a character to count the number of times it is in
a sentence: c

Enter a sentence to search for a specified character:


Learning to program in C++ is fun and challenging.

Your sentence had 1 c character(s)


Do you wish to enter another sentence (y/n)? y

Enter a sentence to search for a specified character:


Working with nested loops requires using braces.

Your sentence had 1 c character(s)


Do you wish to enter another sentence (y/n)? n

2a. Critical Thinking Exercise: Design a program that displays a table resembling a
spreadsheet. The column headings should show the values A, B, C, D, and E, and the row
headings should show 1, 2, 3, 4, and 5. The row values should be multiples of the value
of the row number. Use a for loop to output the column headings. Use nested for
loops to output the row number and the values within the rows and columns. The
output should look like the following:

A B C D E
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
Write your design in the following space. Your design should be a list of C++ comments
without any code.
2b. Write a C++ program based on the design you created in Exercise 2a and name it
table.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.

Variable Name Data Type Value in Line Number at Line Number


Memory Declaration when Initialized

In the following space, show what is displayed on the screen after executing the output
message.

2c. Enter, compile, link, and execute table.cpp. Then copy the output and save it in a block
comment at the end of your program. Save table.cpp in the Chap05 folder of your
Student Data Files. Compare the output of your program to the desired input above.

You might also like