All of the work in this project is my own! I have not left copies of my code in public folders on university computers. I have not given any of this project to others. I will not give any portion of this project to others taking this class. I realize that the penalty for turning in work that is not my own can range from an "F" in the class to dismissal from Trinity University.
Print Name __________________________
Signature __________________________
Loop Practice Lab
Individual Assignment
10 Points
Some of the code will result in infinite loops or near infinite loops.
If the loop is going to generate 15 or more outputs, simply list the first
5 with a continuation and the string "Infinite Loop". 2
4 6 8 10 ... Infinite Loop
If in doubt, plug it into the computer and test the
block of code!
Fixed Repetition Loops - for
| Example 1
for (Counter = 1; Counter <= 5; Counter ++ ) cout << Counter; cout << endl << "End -> Counter = " << Counter << endl; |
12345
End -> Counter = 6 |
| Example 2:
for (Counter = 7; Counter >= 1; Counter = Counter - 2 ) cout << setw(5) << Counter; cout << endl << "End -> Counter = " << Counter << endl; |
7 5
3 1
End -> Counter = -1 |
| Example 3:
for (A = 5, Counter = 1; Counter >= 10; Counter ++, A-- ) { cout << setw(5) << Counter << A << endl; Counter = Counter + 2; } cout << endl << "End -> Counter = " << Counter << endl; |
End -> Counter = 1 |
| Example 4:
for (A = 5, Counter = 1; Counter <= 10; Counter ++, A-- ) { cout << setw(5) << Counter << setw (5) << A << endl; Counter = Counter + 2; } cout << endl << "End -> Counter = " << Counter << endl; |
1 5
4 4 7 3 10 2 End -> Counter = 13 |
| Example 5:
for (Counter = 2; Counter <= 5; Counter -- ) cout << Counter << " "; cout << endl << "End -> Counter = " << Counter << endl; |
2 1 0 -1 -2 ... Infinite Loop! |
| Example 1
Counter = 1; while (Counter <= 5) cout << Counter; cout << endl << "End -> Counter = " << Counter << endl; |
11111 ... Infinite Loop |
| Example 2:
Counter = 1; while (Counter <= 4) { cout << setw(5) << Counter; Counter = Counter + 1; } cout << endl << "End -> Counter = " << Counter << endl; |
1 2
3 4
End -> Counter = 5 |
| Example 3:
Counter = 1; while (Counter++ <= 5) cout << Counter; cout << endl << "End -> Counter = " << Counter << endl; |
23456
End -> Counter = 7 |
| Example 4:
Counter = 1; while (Counter <= 8) { cout << setw(5) << Counter; if (Counter % 4 == 0) cout << endl; Counter ++; } cout << endl << "End -> Counter = " << Counter << endl; |
1 2
3 4
5 6 7 8 End -> Counter = 9
|
| Example 5:
Counter = 1; while (Counter <= 8) { cout << setw(5) << Counter * 2; Counter ++; if (Counter % 4 == 0) cout << endl; } cout << endl << "End -> Counter = " << Counter << endl; |
2 4
6
8 10 12 14 16 End -> Counter = 9 |
Counter = 1;
while (Counter <= 10)
{
cout << setw(5) <<
Counter * 3;
Counter ++;
if (Counter % 5 == 0)
cout <<
endl;
}
cout << endl << "End -> Counter
= " << Counter << endl;
| Example 1
Counter = 1; do cout << Counter ++; while (Counter <= 5); cout << endl << "End -> Counter = " << Counter << endl; |
12345 End -> Counter = 6 |
| Example 2:
Counter = 1; do { cout << setw (3) << Counter + 4; Counter ++; } while (Counter <= 5); cout << endl << "End -> Counter = " << Counter << endl; |
5 6 7 8 9
End -> Counter = 6 |
| Example 3:
Counter = 3; do { cout << setw (3) << Counter; if (Counter % 4 == 0) cout << endl; Counter= Counter + 3; } while (Counter <= 18); cout << endl << "End -> Counter = " << Counter << endl; |
3 6 9 12
15 18 End -> Counter = 21 |
| Example 4:
Counter = 10; do { cout << setw (3) << Counter; Counter --; } while (Counter <= 5); cout << endl << "End -> Counter = " << Counter << endl; |
10
End -> Counter = 9 |
1] Write a C++ program, called LoopPractice.cpp which does each of the following in this order
The program will be compiled with the following line of
code:
g++ LoopPractice.cpp
The following line of code will execute your program
and send the output to a file called Output.txt
a.out > Output.txt
Start Windows 2000/NT/98/95 Print LoopPractice.cpp and Output.txt in Courier 8 Point Fonts!
Visual C++ Printing: Page SetUp & Font Selection
A] Divider
B] Copy of this assignment sheet. Using an ink pen, print your name(s) and sign this lab at the top.
C] Printed Copies of LoopPractice.cpp and Output.txt
D] Using an ink pen, initial each and every page of this lab.