You are on page 1of 6

Answer Key

CPT 168
Fall 2012
Homework # 13
Chapter 5: Review Questions, Multiple Choice, T/F, Programming Exercises #2, 5

Multiple Choice
1. A _________________-controlled loop uses a true/false condition to control the number of
times that it repeats.
a. Boolean
b. condition
c. decision
d. count
2. A ________________-controlled loop repeats a specific number of times.
a. Boolean
b. condition
c. decision
d. count
3. Each repetition of a loop is known as a(n) _______________.
a. cycle
b. revolution
c. orbit
d. iteration
4. The While loop is a _______________ type of loop.
a. pretest
b. posttest
c. prequalified
d. post iterative
5. The Do-While loop is a _______________ type of loop.
a. pretest
b. posttest
c. prequalified
d. post iterative
6. The For loop is a ________________ type of loop.
a. pretest
b. posttest
c. prequalified
d. post iterative

7. A(n) _______________ loop has no way of ending and repeats until the program is
interrupted.
a. indeterminate
b. interminable
c. infinite
d. timeless
8. A _______________ loop always executes at least once.
a. pretest
b. posttest
c. condition-controlled
d. count-controlled
9. A(n) _______________ variable keeps a running total.
a. sentinel
b. sum
c. total
d. accumulator
10. A(n) _______________ is a special value that signals when there are no more items from a
list of items to be processed. This value cannot be mistaken as an item from the list.
a. sentinel
b. flag
c. signal
d. accumulator

True or False
1. A condition-controlled loop always repeats a specific number of times.
FALSE
2. The While loop is a pretest loop.
TRUE
3. The Do-While loop is a pretest loop.
FALSE
4. You should not write code that modifies the contents of the counter variable in the body of a
For loop.
TRUE

5. You cannot display the contents of the counter variable in the body of a loop.
FALSE
6. It is not possible to increment a counter variable by any value other than 1.
FALSE
7. The following statement decrements the variable x: Set x = x - 1.
TRUE
8. It is not necessary to initialize accumulator variables.
FALSE
9. In a nested loop, the inner loop goes through all of its iterations for every single iteration of
the outer loop.
TRUE
10. To calculate the total number of iterations of a nested loop, add the number of iterations of
all the loops.
FALSE

Programming Exercises
2. Calories Burned
Running on a particular treadmill you burn 3.9 calories per minute. Design a program that uses a loop
to display the number of calories burned after 10, 15, 20, 25 and 30 minutes.
// This program calculates and displays the number of calories burned for
// 10, 15, 20, 25 and 30 minutes on a particular treadmill.
// Declare the variables for Calories Burned and counter for loop
Declare Real calsBurned = 0
Declare Integer counter = 10
// Declare constant for calories burned per minute
Constant Real CALS_PER_MIN = 3.9
// For loop to make each calculation and display the result.
// Increment counter by 5.
For counter = 10 to 30 Step 5
calsBurned = counter * CALS_PER_MIN
Display counter, minutes on the treadmill will burn , calsBurned,
calories.
Loop

// Call exit module


closing()
// Exit module
Module closing()
Declare String anyKey =
Display
Display ----Press any key and hit ENTER to exit----
Input anyKey
End Module
// CPT-168-HW-13-CaloriesBurned.cpp : Defines the entry point for the console application.
//
// Program to calculate and display calories burned for 10, 15, 20, 25 and 30 minutes
// on a particular treadmill
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
// Function definition
void closing();
int main()
{
// Declare variables for calories burned and counter
double calsBurned = 0;
int count = 0;
// Declare constant for calories burned per minute
const double CALS_PER_MIN = 3.9;
for (int count = 10; count <= 30; count += 5)
{
calsBurned = count * CALS_PER_MIN;
cout << "Calories burned in " << count << " minutes on the treadmill: " << calsBurned
<< endl;
}
closing();
system (pause);
return 0;
}
void closing()
{
string anyKey = "";
cout << endl << "----Press any key and hit ENTER to exit----";
cin >> anyKey;
}

5. Tuition Increase

At one college, the tuition for a full-time student is $6000 per semester. It has been announced that
the tuition will increase by 2 percent each year for the next five years. Design a program with a loop
that displays the projected semester tuition amount for the next five years.
// This program calculates and displays the projected semester tuition for the
// next five years given a 2% per year tuition increase.
// Declare variables for tuition and counter
Declare Real tuition = 6000.00
Declare Integer counter = 1
// Declare constant for 2% yearly tuition increase
Constant Real PERCENT_INCREASE
// Call intro text module
introText(tuition)
// For loop to calculate and display the projected tuition for each of the
// next five years
For counter = 1 To 5
tuition = tuition * 1.02
Display Year , counter, tuition will be , currencyFormat(tuition), .
Loop
// Call exit module
closing()
// Intro Module
Module introText(Real tuition)
Display Current tuition: , currencyFormat(tuition)
Display // blank line
Display Projected tuition for the next five years with the 2% increase:
Display // blank line
End Module
// Exit module
Module closing()
Declare String anyKey =
Display
Display ----Press any key and hit ENTER to exit----
Input anyKey
End Module

// CPT-168-HW-13-Tuition-Increase.cpp : Defines the entry point for the console application.

//
// Program to calculate and display the projected tuition for the next five years given a
// 2% per year raise in tuition
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
// Function definitions
void introText(double tuition);
void closing();
int main()
{
// Declare variables for tuition and counter
double tuition = 6000.00;
int count = 1;
// Declare constant for amount of tuition increase
const double YEARLY_INCREASE = 0.02;
// Format tuition to 2 decimal places
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
// Call intro text function
introText(tuition);
// For loop to calculate and display the projected tuition for each of the
// next five years
for(count = 1; count <=5; count ++)
{
tuition = tuition * 1.02;
cout << "Year " << count << " tuition will be $" << tuition << ". \n";
}
// Call exit function
closing();

system (pause);
return 0;

void introText(double startTuition)


{
cout << "Current tuition: $" << startTuition;
cout << endl << endl;
cout << "Projected tuition for the next five years with the 2% increase:";
cout << endl << endl;
}
void closing()
{
string anyKey = "";
cout << endl;
cout << "----Press any key and hit Enter to exit----";
cout << endl;
cin >> anyKey;
}