Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help understanding nested for loops -- Creating patterns with them

stirredostirredo Member Posts: 1
I am having problem understanding nested loops, esp in making patterns with them.

I am able to understand simple loops like these:
[CODE]ABCDEFGHIJ

ABCDEFGHIJ

ABCDEFGHIJ

ABCDEFGHIJ

ABCDEFGHIJ

ABCDEFGHIJ
[/CODE]

code for this will be:
[CODE]#include

#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("
");

}



return 0;

}
[/CODE]

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:
[CODE]$

$$

$$$

$$$$

$$$$$

OR

F

FE

FED

FEDC

FEDCB

FEDCBA

OR

A

ABA

ABCBA

ABCDCDA

ABCDEDCBA

[/CODE]

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

Comments

  • anthrax11anthrax11 Member Posts: 511
    In the first problem, the program writes a number of rows on the screen. For each row, the inner loop writes as many '$' characters as the current row number.
    [code]
    #include

    #define ROWS 5

    int main(void)
    {
    int row;

    // Start from row one and end on row 5
    for (row = 1; row <= ROWS; row++)
    {
    int ch;

    // Stops writing characters when the ch counter
    // reaches the current row number
    for (ch = 0; ch < row; ch++)
    putchar('$');

    // New row
    printf("
    ");
    }

    return 0;
    }
    [/code]
    The second problem can be interpreted in different ways. We can define it such that the inner loop prints characters starting from the sixth letter('F') and continues to write decremental letters until it reaches the number of the current row. The outer loop stops when the row counter going backwards reaches zero.
    [code]
    #include <stdio.h>

    #define ROWS 6

    int main(void)
    {
    int row;

    // The first row number is 6, the last one is 1
    for (row = ROWS; row >= 1; row--)
    {
    int ch;

    // The number of characters to write:
    // [the maximum row number] - [current row number]
    for (ch = 0; ch <= (ROWS-row); ch++)
    {
    // The letter to write:
    // [the largest possible letter] - [current row number]
    // For example, on the first row, ch is 0, so:
    // ('A' + ROWS - 1) - ch) = ('G' - 1) - 0 = 'F'
    putchar(('A' + ROWS - 1) - ch);
    }

    printf("
    ");
    }

    return 0;
    }
    [/code]
    The last problem can also be defined in more than one way. It may print spaces after the characters or not.
    First the program writes spaces to align the characters to their correct position. The number of spaces is defined by the maximum number of rows and the current row number.
    [code]
    #include <stdio.h>

    #define ROWS 5

    int main(void)
    {
    int row;

    for (row = 0; row < ROWS; row++)
    {
    int ch;

    // write the spaces
    for (ch = 1; ch < (ROWS-row); ch++)
    putchar(' ');

    // write the letters
    for (ch = 0; ch <= row; ch++)
    putchar('A' + ch);

    // and now go backwards
    for (ch = (row-1); ch >= 0; ch--)
    putchar('A' + ch);

    printf("
    ");
    }

    return 0;
    }
    [/code]
    I don't know how many comments to add so that it wouldn't get too confusing, so just ask if you can't understand something.
Sign In or Register to comment.