Jun 27, 2014

C Programming #26: Loop - nested

Loop inside a loop is called  nested loop. Nested loops are most interesting in loop constructs. Simplest of nested loop construct might produce complex output, while some time complex construct might produce simple output.

Remember the basics of loops before venturing into nested loop. Always do step by step analysis.

Lets get straight into programs


Program 1

Write a Program to print following
*
**
***
****
*****
******

Strategy:
First line contains one star, while second line contains 2 stars, third line contains 3 stars and so on. So we need a loop to generate series of number 1, 2, 3... so on corresponding to number of stars. To print stars within that loop we would need another loop.


#include <stdio.h>

int main()
{
   int r, c;
   int max = 6; /* Max number of stars */

   for(r = 1; r <= max; r++) {
      /* r contains number of stars in a row */
      /* Below loop will print r number of stars consecutively */
      for(c = 0; c < r; c++) {
          printf("*");
      }
      printf("\n");
   }
   return 0;
}

Let me slightly modify the above program.

Program 2

Write a Program to print following
******
*****
****
***
**
*

Strategy:
Very similar to program 1, but the we need to count reverse from 6, 5, 4, 3, 2, 1. Loop that prints number of stars need no modification.


#include <stdio.h>

int main()
{
   int r, c;
   int max = 6; /* Max number of stars */

   for(r = max; r >= 1; r--) {
      /* r contains number of stars in a row */
      /* Below loop will print r number of stars consecutively */
      for(c = 0; c < r; c++) {
          printf("*");
      }
      printf("\n");
   }
   return 0;
}

Note that inner loop did not change at all, only the outer loop has changed from incrementing loop to decrementing loop.

Lets make little bit more complicated.

Program 3

Write a program to print the following

    *
   ***
  *****
 *******
*********

Strategy:
Row one has 1 star, row two has 3 star, row three has 5 star and so on. We need to generate these odd number for each row. If you look closely apart from this there is some spaces in start which is also important. Row one has 4 space, row two has 3 space, row three has 2 space so on.


#include <stdio.h>

int main()
{
   int max = 5; /* Maximum number of rows */
   int row_cnt;
   int space_cnt;
   int star_cnt;

   for(row_cnt = 1; row_cnt <= max; row_cnt++) {
      for(space_cnt = 1; space_cnt <= max - row_cnt; space_cnt++) {
          printf(" ");
      }
      for(star_cnt = 1; star_cnt <= (2 * row_cnt - 1); star_cnt++) {
          printf("*");
      }
      printf("\n");
   }
   return 0;
}

Explanation
  • row_cnt loop is counting each row starting from 1 to max.
  • space_cnt loop is printing max - row_cnt of spaces.
  • While star_cnt is printing (2 * row_cnt - 1) of star. (Remember that 2n-1 is to generate odd number).

Links

Quiz - Not Yet written !!
Next Article - C Programming #27: Loop - infinite
Previous Article - C Programming #25: Loop - continue
All Articles - C Programming

No comments :

Post a Comment