P5: Loop

  1. Write programs using looping to print the following patterns:
    Your program ask the user to enter the size of a pattern from keyboard.
    NOTE that you will earn extra points if you work on rectangular patters instead of square patterns. See some samples here
    (1) A square of zeros of any size;
    Enter the size of the square:
    16
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    Press any key to continue . . .
    
    (2) A square of zeros of any size with 1's on the first diagonal;
    Enter the size of the square:
    20
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    Press any key to continue . . .
    
    (3) A square of zeros of any size with 1's on the second diagonal;
    Enter the size of the square:
    18
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    Press any key to continue . . .
    
    (4) A square of zeros of any size with 1's on both diagonals;
    Enter the size of the square:
    20
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
    0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
    0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
    0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
    0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0
    0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
    0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
    0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0
    0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
    0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
    0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
    0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    Press any key to continue . . .
    
    (5) A flag.
    15
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    * * * * * * * - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - -
    Press any key to continue . . .
    
    
    26
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    * * * * * * * * * * * * * - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    Press any key to continue . . .
    
  2. Write a program using loops to print out counting numbers in a triangle pattern with the height chosen by the user (the height < 13).
    Enter the height of the triangle:
    6
      1
      2  3
      4  5  6
      7  8  9 10
     11 12 13 14 15
     16 17 18 19 20 21
    Press any key to continue . . .
    
    Enter the height of the triangle:
    12
      1
      2  3
      4  5  6
      7  8  9 10
     11 12 13 14 15
     16 17 18 19 20 21
     22 23 24 25 26 27 28
     29 30 31 32 33 34 35 36
     37 38 39 40 41 42 43 44 45
     46 47 48 49 50 51 52 53 54 55
     56 57 58 59 60 61 62 63 64 65 66
     67 68 69 70 71 72 73 74 75 76 77 78
    Press any key to continue . . .
    
  3. Write programs using looping to find out
    (1). The largest integer from a data file containing integers: 5 2 3 4 10 6
    (2). The sum of the integers from the same file.
    //Sample skeleton code
    int main()
    {
       //Declare an ifstream variable, say, inData
       //Open a text file containing those integers
       //Read the first integer into a variable, say, num
       //inData >> num;
       //while(inData)
       //{
           //Find the largest integer
    	   //Find the sum of the integers
       //}
       
       //Print out the largest integer and sum.
       
       return 0;
    }
    
  4. At the top section of your program you need to use C++ comments to include your name, class name, programming assignment name, and date the program was written.
  5. Your programs should be properly formatted using indentation and empty lines (spaces). Use "camel" notation for variable naming.
  6. Upon completion, save all programs and the required output into a single text file using notepad (or notepad++). The file should be named in this format: P5_YourName.txt, for example, P5_JohnDoe.txt. Then submit to the Blackboard (Bb) as attachment by the deadline. The submission link on the Bb will be closed automatically after the deadline. Assignments that fail to follow the instructions will NOT be graded.