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


Set Print Margins [ Left = 1; Right = .2; Top = .6; Bottom = .6; Select Document Title, Location,  Page Number, Page Total Black Text l]


1] Each of the following blocks of code may or may not produce any output. Record any output to the right of the block of code. See Examples. If syntax corrections need to be made in order to make the code compile corretly, use a pen or different colored ink to make the corrections.!

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!


Assume the following declarations, at the top of main, for all.
int
   Counter,
   A;

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!


 for (Counter = 9; Counter <= 11; Counter ++ )
    cout << Counter;
 cout << endl << "End -> Counter = " << Counter << endl;


 for (Counter = 1; Counter <= 5; Counter ++ )
    cout << setw(5) << Counter * 4;
 cout << endl << "End -> Counter = " << Counter << endl;


 for (Counter = 1; Counter <= 5; Counter ++ )
    cout << setw(5) << Counter * 3 + 2;
 cout << endl << "End -> Counter = " << Counter << endl;


 for (Counter = 1; Counter <= 5; Counter ++ )
    cout << setw(5) << Counter * 5 - 7;
 cout << endl << "End -> Counter = " << Counter << endl;


 for (Counter = 1; Counter <= 8; Counter ++ )
 {
    cout << setw(5) << Counter;
    if (Counter % 4 == 0)
    cout << endl;
 }
 cout << endl << "End -> Counter = " << Counter << endl;


 for (Counter = 10; Counter >= 2; Counter = Counter - 3 )
    cout << setw(5) << Counter;
 cout << endl << "End -> Counter = " << Counter << endl;


 for (A = 3, Counter = 7; Counter <= 10; Counter ++, A++ )
 {
    cout << setw(5) << Counter << A << endl;
    Counter = Counter + 2;
 }
 cout << endl << "End -> Counter = " << Counter << endl;

 for (A = 3, Counter = 7; Counter >= 10; Counter ++, A++ )
 {
  cout << setw(5) << Counter << A << endl;
  Counter = Counter + 2;
 }
 cout << endl << "End -> Counter = " << Counter << endl;

 for (Counter = 1; Counter <= 10; Counter ++ ); // Not a syntax error!
 {
  cout << setw(5) << "Counter = " << Counter << endl;
 }
 cout << endl << "Counter = " << Counter << endl;

PreTest Loops - while


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;
    if (Counter % 5 == 0)
       cout << endl;
    Counter ++;
 }
 cout << endl << "End -> Counter = " << Counter << endl;


Counter = 11;
while (Counter >= 15)
cout << Counter;
cout << endl << "End -> Counter = " << Counter << endl;


 Counter = 3;
 while (Counter <= 7)
 {
    cout << setw(5) << Counter;
    Counter = Counter + 1;
 }
 cout << endl << "End -> Counter = " << Counter << endl;


 Counter = 1;
 while (Counter++ <= 5)
    cout << Counter * 3;
 cout << endl << "End -> Counter = " << Counter << endl;


 Counter = 1;
 while (Counter++ <= 4)
    cout << Counter * 4 - 1;
 cout << endl << "End -> Counter = " << Counter << endl;

 Counter = 1;
 while (Counter <= 8)
 {
    cout << setw(5) << Counter * 2 - 15;
    if (Counter % 4 == 0)
        cout << endl;
    Counter ++;
 }
 cout << endl << "End -> Counter = " << Counter << endl;

 Counter = 1;
 while (Counter <= 10)
 {
    cout << setw(5) << Counter * 3;
    Counter ++;
    if (Counter % 5 == 0)
        cout << endl;
 }
 cout << endl << "End -> Counter = " << Counter << endl;


PostTest Loops - while


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


 Counter = 1;
 do
 {
    cout << setw (3) << Counter + 4;
    Counter --;
 }
 while (Counter <= 5);
 cout << endl << "End -> Counter = " << Counter << endl;


 Counter = 1;
 do
 {
    cout << setw (5) << Counter;
    if (Counter % 4 == 0)
     cout << endl;
    Counter= Counter + 1;
 }
 while (Counter <= 6);
 cout << endl << "End -> Counter = " << Counter << endl;


 Counter = 1;
 do
 {
    cout << setw (5) << Counter * 3 - 4;
    if (Counter % 3 == 0)
       cout << endl;
    Counter = Counter + 1;
 }
 while (Counter <= 8);
 cout << endl << "End -> Counter = " << Counter << endl;

 Counter = 1;
 do
 {
    cout << setw (5) << Counter;
    Counter ++;
 }
 while (1 == 1);
 cout << endl << "End -> Counter = " << Counter << endl;

 Counter = 2;
 do
 {
    cout << setw (5) << Counter;
    Counter ++;
 }
 while (true);
 cout << endl << "End -> Counter = " << Counter << endl;

Practice Quiz Questions - Code each of the following:


1] Write a C++ program, called LoopPractice.cpp  which does each of the following in this order

  1. cout << "\n\n===============================================\n";
  2. cout << " \n\n\nFixed Repetition 10 - 20  [1 to a line]\n\n";
  3. Write a fixed repetition loop which will display the numbers 10 - 20 [1 to a line]
  4. cout << "\n\n===============================================\n";
  5. cout << " \n\n\nPostTest 100 - 200  [10  to a line - 8 characters each]\n\n";
  6. Write a fixed posttest loop which will display 100 - 200  [10  to a line - 8 characters each]
  7. cout << "\n\n===============================================\n";
  8. cout << " \n\n\nPreTest -10 to + 10  [5  to a line - 8 characters each]\n\n";
  9. Write a fixed pretest loop which will display 100 - 200  [10  to a line - 8 characters each]
  10. cout << "\n\n===============================================\n";
  11. cout << " \n\n\nFixed Repetition 500 nos in sequence 5,10,15,...  [12 to a line-6 characters]\n\n";
  12. Write a fixed repetition loop which will display the first 500 numbers in the sequence 5, 10, 15, 20, 25, ...  [12  to a line - 6 characters each]

 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

 



Those Labs labeled "Individual Assignment" are to be done separately by each individual. Using a pen,  each individual is to print  his/her name at the top of this document in the space provided and sign it.  Those Labs labeled "Team Assignment" may be done as a team or individually. Using a pen,  each individual on the team is to print print his/her name at the top of this document in the space provided and sign it. Submit only one copy of team assignments!

Include the following in your wire-band binder:

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.