P5: Loop

  1. Write a program to calculate the sum of the following series:
    \( S = \dfrac{1}{1} - \dfrac{1}{2} +\dfrac{1}{4}-\dfrac{1}{8}+ \dots \)
    Display S for 100 terms.

    Hint: Think about the following
    \( S = \dfrac{1}{2^0} - \dfrac{1}{2^1} +\dfrac{1}{2^2}-\dfrac{1}{2^3}+ \dots + \dfrac{1}{2^i} + \dots = \dfrac{2}{3} \)
  2. 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 the bottom triangle;
    Enter the size of the square:
    20
    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 1 1 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 0
    0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0
    0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0
    0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0
    0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
    0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0
    0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    
    
    (5) A rectangular flag.
    Enter the width and height of a rectangle:
    20 10
    * * * * * * * * * * - - - - - - - - - -
    * * * * * * * * * * - - - - - - - - - -
    * * * * * * * * * * - - - - - - - - - -
    * * * * * * * * * * - - - - - - - - - -
    * * * * * * * * * * - - - - - - - - - -
    - - - - - - - - - - * * * * * * * * * *
    - - - - - - - - - - * * * * * * * * * *
    - - - - - - - - - - * * * * * * * * * *
    - - - - - - - - - - * * * * * * * * * *
    - - - - - - - - - - * * * * * * * * * *
    
    Enter the width and height of a rectangle:
    30 10
    * * * * * * * * * * * * * * * - - - - - - - - - - - - - - -
    * * * * * * * * * * * * * * * - - - - - - - - - - - - - - -
    * * * * * * * * * * * * * * * - - - - - - - - - - - - - - -
    * * * * * * * * * * * * * * * - - - - - - - - - - - - - - -
    * * * * * * * * * * * * * * * - - - - - - - - - - - - - - -
    - - - - - - - - - - - - - - - * * * * * * * * * * * * * * *
    - - - - - - - - - - - - - - - * * * * * * * * * * * * * * *
    - - - - - - - - - - - - - - - * * * * * * * * * * * * * * *
    - - - - - - - - - - - - - - - * * * * * * * * * * * * * * *
    - - - - - - - - - - - - - - - * * * * * * * * * * * * * * *
    
  3. Write a program using loops to print out counting numbers in a triangle pattern with the height chosen by the user (the height < 13). Hint: Use setw(3) to align the numbers vertically.
    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 . . .
    
  4. Given a data file containing integers below, write a program using looping to find out
      8   7  19  14  21  22   6  19  45   8
     36   7  43  42   9  30  11  40  45  26
     14  41  45   6  30  15  12  45  31  48
      3  40  18  20  12  47  22  33  26  27
    
    (1). The largest and second largest integers:
    Hint: To find the second largest number in a data file, try the following steps: Read the first number from the file. Set the first number in the file as both the largest and second largest numbers initially on the assumption that the largest and second largest are two distinct numbers in the file. Then use a loop to read numbers one by one from the file. If a number read from the file is larger than the current largest number, then update the largest with the new number and the "previous" largest becomes the new second largest. Otherwise, check the newly read number from the file is larger than the second largest, if so, update the second largest number with the number.
    (2). The sum of all the integers in the 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;
    }
    
  5. 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.
  6. Your programs should be properly formatted using indentation and empty lines (spaces). Use "camel" notation for variable naming.
  7. 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.