More Repetitions Of Repetitions

(Nested Repetitions And Nested Loops)

Problem: Write a program to print a pattern (string) consisting of an a then some b's, then another a and some b's and so on. Each time the number of b's should be one less than the number of b's before. It should stop when the number of b's becomes one. It should get the first number of b's from the user. For example if the number the user gives the program is 5, then the program should print:

abbbbbabbbbabbbabbab

Analysis. Let's look for the repetitions. The big repetitions are an a followed by some b's: The small repetitions are the b's within each big rep. Unlike the previous example each big rep is not identical to the one preceding it. In fact each new big rep has one less b

abbbbb abbbb abbb abb ab
Solution 1: As a first step let's write the first part of the program that asks the user for input:
int bNum;
printf("Please enter the number of b's: ");
scanf("%d", &bNum);
Now we are supposed to print an a and then print some b's. Here's code for that:
printf("a");
int i=0;
while (i<bNum) {
  printf("b");
  i++;
}
The letters printed come out in a line because no newline (\n) is printed. The code above is supposed to go inside the body of the outside loop. Also next time we want one fewer b:
while  (????) {
   printf("a");
   int i=0;
   while (i<bNum) {
     printf("b");
     i++;
   }
   bNum--;   //one fewer b in the next rep
}
What condition do we put in place of ???? ? Well we are supposed to keep on going while the number of b's is 1 or larger. So:
while  (bNum>=1) {
   printf("a");
   int i=0;
   while (i<bNum) {
     print("b");
     i++;
   }
   bNum--;   //one fewer b in the next rep
}
printf("\n");
The print of the newline at the bottom moves us to a new line. It is not strictly necessary.

Now let's look at a second solution. Instead of doing lots of little prints, we will accumulate the characters in a string, and when we are finished building the string, we will print it. The pattern of the loops though will be the same. In addition to variables bNum and i, we will need a char array to hold the string, and a variable to keep track of where in the array to put the next character. Remember char arrays store char codes. Here are the important variables:

int bNum;
char str[100];
int p=0;  //the first position to fill is str[0]
Now instead of printing an a we will write
str[p] = 'a';
p++;
The same for storing the b's. NOTE: the second statement above sets up so that the next character stored will be in the next cell right after the a. So here is the complete program.
int bNum;
char str[100];
int p=0;
printf("Please enter the number of b's ");
scanf("%d, &bNum);
while  (bNum>=1) {
   str[p] = 'a';
   p++;
   int i=0;
   while (i<bNum) {
      str[p] = 'b';
      p++;
      i++;
   }
   bNum--;   //one fewer b in the next rep
}
str[p] = '\n';
p++;
str[p]=0; //make it a string by putting in the end-of-string marker
p++;
printf("%s", str);

Next: Demo: Playing Computer with Nested Loops