Homework 7: Pine Trees


Due: Tuesday 3/27 by 11:59pm (after Spring Break)


Objectives

More practice with nested for loops and incremental programming.


The Assignment


This assignment is a continuation of the isosceles triangle assignment for Classwork 10. Your final objective is to write a program that draws a "pine tree" shape:


PT[222]% !gcc 
gcc -Wall pine.c
PT[223]% ./a.out
height? 7
      *
     ***
    *****
      *
     ***
    *****
   *******
  *********
      *
     ***
    *****
   *******
  *********
 ***********
*************
PT[224]% 

PT[225]% ./a.out
height? 9
        *
       ***
      *****
     *******
    *********
        *
       ***
      *****
     *******
    *********
   ***********
  *************
        *
       ***
      *****
     *******
    *********
   ***********
  *************
 ***************
*****************
PT[226]% 

Note that the top isosceles triangle is smaller than the middle one and the bottom one. In the last example, the top triangle has 5 rows, the middle one has 7 rows and the bottom 9 rows. (The height entered by the user now specifies the height of the bottom triangle.)

Step-by-Step Instructions

Step 1

Copy your program from Classwork 10 and name the file pine.c

Add a new outer loop around the for loops you wrote for Classwork 10. (Make sure that you indent the lines inside the new loop!) This new outer loop just iterates 3 times. (No you cannot just copy the code 3 times. You will lose a lot of points!)

Now the output of your program should look like:


PT[220]% ./a.out
height? 7
      *
     ***
    *****
   *******
  *********
 ***********
*************
      *
     ***
    *****
   *******
  *********
 ***********
*************
      *
     ***
    *****
   *******
  *********
 ***********
*************
PT[221]% 

Step 2

Notice that in the diagram above, that the asterisks on the top triangle are actually in the right place. There are just too many rows. If you only printed the first 3 rows of the top triangle, then everything is in place. For the middle triangle, you want to print only 5 rows in this example. Then you would have:


PT[223]% ./a.out
height? 7
      *
     ***
    *****
      *
     ***
    *****
   *******
  *********
      *
     ***
    *****
   *******
  *********
 ***********
*************
PT[224]% 

Of course, your program must also work when the user types in a different value for the height. So, figure out how to specify the height of the triangle in terms of the index variable of the outermost for loop (the one you just added). Once you have that, you are done.


PT[242]% ./a.out
height? 12
           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************
           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************
   *****************
  *******************
           *
          ***
         *****
        *******
       *********
      ***********
     *************
    ***************
   *****************
  *******************
 *********************
***********************
PT[243]% 

What to Submit

Use the script command to record 3 sample runs of your program using different values for the height of the "tree". Don't forget to exit from script. Then, submit your program and sample run using.


submit cs104_chang hw07 pine.c typescript