Nested loops: the crown jewels

  1. lucy1.C and lucy2.C produce exactly the same output. Lucy in the Sky With Diamonds.
    Exercise. Could you output the 25 lowercase a’s with a for loop?
  2. doublespace.C. Let the user decide if they want the output single spaced, double spaced, triple spaced, etc.
  3. charloop.C:
    Use a variable of type int to count through a series of consecutive numbers (1, 2, 3, 4).
    Use a variable of type char to count through a series of consecutive characters ('a', 'b', 'c').
  4. Pictures:
    1. line.C. Why are all the Xs on the same line?
    2. rectangle.C. Why are all the lines the same length?
    3. triangle1.C and triangle2.C. Why do the lines become longer and longer?
    4. parallelogram.C. Why are there more and more spaces in front of each line?
  5. Graph paper. Exercise: add the missing right and bottom edges.
    1. graph1.C: no loop, hardwired to do 10 rows and 10 columns of boxes
    2. graph2.C: one loop, inputs the number of rows, hardwired to output 10 columns.
    3. graph3.C: nested loops, inputs the number of rows and columns.
    4. Homework. Let the user input the following four numbers when outputting graph paper. For simplicity, allow the right and bottom edges to remain ragged. Here are two examples:
      How many rows of boxes (e.g., 10)? 2
      How many columns of boxes (e.g., 10)? 4
      How many rows of blanks in each box (e.g., 1)? 1
      How many columns of blanks in each box (e.g., 3)? 4
      
      +----+----+----+----
      |    |    |    |
      +----+----+----+----
      |    |    |    |
      
      How many rows of boxes? 2
      How many columns of boxes? 4
      How many rows of blanks in each box (e.g., 1)? 3
      How many columns of blanks in each box (e.g., 3)? 8
      
      +--------+--------+--------+--------
      |        |        |        |
      |        |        |        |
      |        |        |        |
      +--------+--------+--------+--------
      |        |        |        |
      |        |        |        |
      |        |        |        |
      
  6. solid.C and solid.png: Output a solid colored flag in plain ppm format.
    solid.C has the same two-dimensional structure as rectangle.C.
    RGB stands for “red, green, blue”.
    jsmith@storm:~$ cd
    
    jsmith@storm:~$ wget https://markmeretzky.com/fordham/1600/src/nested/solid.C
    jsmith@storm:~$ ls -l                 (Should be a new file named solid.C)
    
    jsmith@storm:~$ c++ solid.C
    jsmith@storm:~$ ls -l                 (Should be a new file named a.out)
    
    jsmith@storm:~$ ./a.out | /usr/bin/pnmtopng > solid.png
    jsmith@storm:~$ ls -l                 (Should be a new file named solid.png)
    
    jsmith@storm:~$ file solid.png        (Make sure that solid.png is a png file)
    solid.png: PNG image data, 30 x 20, 1-bit colormap, non-interlaced
    
    jsmith@storm:~$ cp solid.png public_html
    
    jsmith@storm:~$ cd public_html
    jsmith@storm:~$ ls -l                 (Should be a new file named solid.png)
    
    Then point your web browser at
    https://storm.cis.fordham.edu/~jsmith/solid.png
    where jsmith is your Fordham login name.
  7. Exercise. Pick a color, any color. The recipe for a color is three numbers, telling how much red, green, and blue light to mix in. The minimum amount for each ingredient is 0, the maximum amount is 255. For example,
    255   0   0 red   
      0 255   0 green 
      0   0 255 blue  
    255 215   0 gold  
    192 192 192 silver
    184 115  51 copper
    128 128   0 olive 
      0 255 255 aqua  
    
    Put the three numbers of your color into solid.C, replacing my blue color 0 0 255. Run the C++ program, together with the other program /usr/bin/pnmtopng, following the instructions here. Then can you see a little 20 × 30 image file of your chosen color in your web browser?
  8. factor.C: find the prime factors of an integer, in order of increasing size.
    Improvement: only need to check factors that are less than or equal to sqrt(n), so #include <cmath>