Repetition Processing Homework Assignment

advertisement
Programming Logic - Beginning
152-101
Repetition Processing
Name
Score
/5
Update Value
5 points
Homework Assignment
5 points (2 incorrect answers = -½ point)
1.
T or F (circle one) The body of a while loop might never execute.
2.
T or F (circle one) The body of a do-while loop might never execute.
3.
T or F (circle one) The body of a for loop might never execute.
4.
T or F (circle one) C tests the for’s condition before the first iteration of the loop.
5.
T or F (circle one) A compiler catches endless loops during syntax checking.
6.
Why should you never put a semicolon at the end of the while loop’s closing parenthesis?
7.
What’s the difference between an accumulator and a counter?
8.
Which is better for looping a specific number of times:
while loop
do-while loop
for loop
9.
What happens when the conditional expression in a for loop is false?
10.
Does the outer loop of two nested loops iterate faster or slower than the inner loop?
Why?
11.
What is wrong with the following loop?
a = 10;
b = 4;
while (a>=10)
{
cout << “a is now ” << a << endl;
b = b + 2;
cout << “b is now “ << b << endl;
} //endwhile
12.
What’s the output of the following section of code?
for (c=0; c<=10; c=c+3)
{ cout << setw(5) << c; }
13.
What’s the output of the following section of code?
for (i=5; i; i--)
{ cout << i << endl; }
14.
How many times does the word “Hello” print?
for (i=0; i > 10; i++)
{ cout << “Hello” << endl; }
15.
How many times does the word “Hello” print? (Look carefully)
for (i=1; i <= 10; i++);
{ cout << “Hello” << endl; }
16.
What is syntactically wrong with the following for statement?
for (i=100, i <= 1000, i++)
{ cout << setw(5) << i; }
17.
What is wrong with the following flowchart fragment?
Display
"Enter Age
>>> "
Get
Age
While
Age <21
T
Display
"Only people
of drinking
age may be
invited. Try
Again."
Reposition
Cursor and
Clear
incorrect
answ er
F
18.
Find and correct the syntax error in the following code fragment.
do {
city = GetCity();
DisplayZipCode(city);
//Call 1000
//Call 2000
cout << “Find another zip code? “;
} while (YorN() == ‘Y’)
19.
Find and correct the syntax error in the following code fragment.
total = 0;
cin >> age; cin.ignore(1);
while age > 0
{
total = total + age;
cin >> age; cin.ignore(1);
} //end while
20.
How many stars will the following code print?
for (r=1; r<=10; r++)
{
for (c=1; c<=4; c++)
{
cout << “*”;
} //end for c
} //end for r
Checkoff Programs
No points
1.
Write a program to display all the printable ASCII characters. The printable characters
are numbered 32-126 and 161-254. Print each of the characters in the following format,
10 per line.
65: A
66: B
67: C etc.
(where 65 is the short ASCII code and A is the associated ASCII character)
Hint: to display the associated character, copy the short ASCII code to char variable.
2.
Update your Grade Points checkoff program to include any number of classes. Continue
to allow the user to enter the grade and number of credits until the user indicates there are
no more classes (implement either version of YorN). Then, calculate the overall GPA.
GPA = total grade points / total credits
Download