I am having problem understanding nested loops, esp in making patterns with them.

I am able to understand simple loops like these:

ABCDEFGHIJ

ABCDEFGHIJ

ABCDEFGHIJ

ABCDEFGHIJ

ABCDEFGHIJ

ABCDEFGHIJ

code for this will be:

#include <stdio.h>

#define ROWS  6

#define CHARS 10

int main(void)

{

    int row;

    char ch;



    for (row = 0; row < ROWS; row++)              /* line 10 */

    {

        for (ch = 'A'; ch < ('A' + CHARS); ch++)  /* line 12 */

            printf("%c", ch);

        printf("\n");

    }



    return 0;

}

In this one the outer loop controls the no. of columns and the inner one control the no. of rows and data to be printed.


But I can't get my head around these kind of patterns:

$

$$

$$$

$$$$

$$$$$

OR

F

FE

FED

FEDC

FEDCB

FEDCBA

OR

    A

   ABA

  ABCBA

 ABCDCDA

ABCDEDCBA

Can someone please guide me? I need an explanation not just code.

basically the "inner loop" is variable, and will execute a number of times that varies depending on the value of the outer loops.

outer loop #1
inner loop executes 1 time

outer loop #2
inner loop executes 2 times

outer loop #3
inner loop executes 3 times

etc.

consider also, that your inner loop may "decrement" instead of incrementing. that is, it will be like for (loop = 10; loop >0; loop--) if you want it to loop from 10 to 1.

when you're printing variable-length lines that depend on the inner loop, you will only print a newline character when the inner loop has completed.

i dont know how to explain this any better, without resorting to code. here it is 'explained' in pseudocode

first problem:

for i = 1 to CHARS
{
    for j = 1 to i
    {
        print "$"
    }
    print \newline
}

second problem:

for i = 1 to CHARS 
{
    for j = (CHARS - 1) to (CHARS - i) 
    {
         print char('A' + j)
    }
    print \newline
}

in this second problem, your "inner loop" will need to be decremented.


third problem:

will be left as an exercise for the reader. in other words, fully understand 1 and 2, then work on 3.

.

i dont know how to explain this any better, without resorting to code. here it is 'explained' in pseudocode

first problem:

for i = 1 to CHARS
{
    for j = 1 to i
    {
        print "$"
    }
    print \newline
}

second problem:

for i = 1 to CHARS 
{
    for j = (CHARS - 1) to (CHARS - i) 
    {
         print char('A' + j)
    }
    print \newline
}

That's not pseudocode. That's BASIC. :icon_wink:

commented: going to look it up on wikipedia, now :P +6

That's not pseudocode. That's BASIC. :icon_wink:

eh, i thought BASIC was pseudo code :P

commented: Well, OK then :D +11

For the third pattern this code will help you,

for(i='A';i<=ch;i++)
        {
                space=ch-i;
                while(space--)
                        printf(" ");
                for(j='A';j<=i;j++)
                        printf("%c",j);
                for(j-=2;j>='A';j--)
                        printf("%c",j);
                printf("\n");
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.