Classwork 10: Isosceles Triangle

Objectives

To practice using nested for loops and incremental programming.

The Assignment

For this assignment, you will write a program that prints out an isosceles triangle (of asterisks) with a height that is specified by the user. A sample run of the program might look like:


PT[130]% ./a.out
height? 5
    *
   ***
  *****
 *******
*********
PT[131]%


PT[131]% ./a.out
height? 10
         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************
PT[132]% 

You will also practice incremental programming where you build a program step by step and each step is a working program.

Step-by-Step Instructions

Step 1

Your starting point is the following program with two nested for loops:


#include <stdio.h>

int main() {
   int i, j, n ;

   printf("height? ") ;
   scanf("%2d", &n) ;

   for (i = 1 ; i <= n ; i++) {

      // printf("%d: ", i) ;

      for (j = 1 ; j <= i ; j++) {

         // Pick *one* of the following 
         // printf("%d", j % 10) ;
         printf("*") ;
      }
      printf("\n") ;

   }


   return 0 ;
}

Copy this program into a file. Call it triangle.c

When you run the program, you should get output that looks like a right triangle:


PT[152]% ./a.out
height? 7
*
**
***
****
*****
******
*******
PT[153]% 

If you uncomment the printf() statements, you can get output that is useful for debugging. Fiddle with the comments so you can get this output (comment out the line that prints asterisks):


PT[157]% ./a.out
height? 12
 1: 1
 2: 12
 3: 123
 4: 1234
 5: 12345
 6: 123456
 7: 1234567
 8: 12345678
 9: 123456789
10: 1234567890
11: 12345678901
12: 123456789012
PT[158]% 

Step 2

Look at the isosceles triangle you are trying to print at the top of this page. The number of asterisks goes up by 2 for each row. That is, the first row has 1 asterisk, the second row has 3, the third has 5, ... In our program, each row only has 1 more asterisk than the previous row.

Modify your program so that the asterisks increase by two for each succeeding row. The output of your program, should look like:


PT[167]% ./a.out
height? 5
*
***
*****
*******
*********
PT[168]% 

It might be easier to use the debugging printf() statements, rather than count asterisks with your finger:

PT[178]% ./a.out
height? 7
 1: 1
 2: 123
 3: 12345
 4: 1234567
 5: 123456789
 6: 12345678901
 7: 1234567890123
PT[179]% 

When you have the right number of asterisks in each row, you may proceed to the next step.

Step 3

Now, we have the right number of asterisks in each row, but the asterisks are in the wrong positions. To have the asterisks in each row appear in the right place, you need a new inner loop that prints the right number of spaces.

First, work on adding a new for loop that prints out in debug mode, and gives this kind of output:


PT[189]% ./a.out
height? 7
 1: 123456*
 2: 12345***
 3: 1234*****
 4: 123*******
 5: 12*********
 6: 1***********
 7: *************
PT[190]% 

Hint: Notice that the number of "spaces" goes down when i increases! So, the number iterations in the inner loop should depend on i in a negative way.

Now you can print spaces instead of digits. You get:


PT[198]% ./a.out
height? 7
      *
     ***
    *****
   *******
  *********
 ***********
*************
PT[199]% 

That means you are done!

What to submit

Use the script command to record yourself compiling and running your program a few times with different triangle heights. (Do not record yourself editing your program!) Exit from script. Submit your program and the typescript file:


submit cs104_chang cw10 triangle.c typescript