summaryrefslogtreecommitdiff
path: root/03_code2/code2.c
blob: 25af8a0cb4268b65a04ce823b5bc67bff03605da (plain)
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
int printTriangle(int size) {
  //start with starCount being 0
  int starCount = 0;

  //count from 0 (inclusive) to size (exclusive), for each number i that you count
  for (int i = 0; i < size; i++) {

     //count from 0 (inclusive) to i (inclusive), for each number j that you count
    for (int j = 0; j <= i; j++) {

        //print a "*"
      printf("*");

        //increment starCount
      starCount++;
    } 

     //when you finish counting on j, 

     //print a newline ("\n")
    printf("\n");
  }

  //when you finish counting on i, 

  //your answer is starCount
  return starCount;

}


int main(void) {
  int numStars;
  printf("Here is a triangle with height 4\n");
  numStars = printTriangle(4);
  printf("That triangle had %d total stars\n", numStars);
  //now print "Here is a triangle with height 7\n"
  printf("Here is a triangle with height 7\n");

  //then call printTriangle, passing in 7, and assign the result to numStars
  numStars = printTriangle(7);

  //finally, print "That triangle had %d total stars\n", such that the %d 
  //prints the value of numStars
  printf("That triangle had %d total stars\n", numStars); 


  return 0;
}