简体   繁体   中英

Printing Pattern with Nested for Loops in C

I'm studying for an exam and the professor asked for a program that can print out the following patterns: picture of the expected output

N=2    N=3    N=4    N=5
**     ***    ****   *****
 **     ***    ****   *****
       ***    ****   *****
               ****   *****
                     *****

(Except the image misses the fifth line for N=5 .)

My program can get a similar output except it doubles the number of rows for each expected output (IE when N=3 there are 6 rows, when N=4 there are 8 rows). Not sure how to stop it running after the number of rows hits N. Here's my code below:

#include <stdio.h>

int main() {

    int N, rows1, width1, rows2, width2;

    printf("Please enter a number between 2 and 5 (including 2 and 5).\n");
    scanf("%d", &N);

    if (N<2 || N>5)
    {
        printf ("The number you entered is either less than 2 or greater than 5."
                " Please try again.\n");
                return 0;
    }

    for (rows1=1; rows1<=N; rows1++)
    {
        for(width1=1; width1<=N; width1++)
            printf ("*");
        printf ("\n");

        for(rows2=1; rows2<=1; rows2++)
            printf (" ");

        for(width2=1; width2<=N; width2++)
            printf ("*");
        printf ("\n");
    }
    return 0;
}

1) In the first conditional, you return 0 to indicate a success. I would go with the macro EXIT_FAILURE (even better use a while loop until you get a valid input from the user) in the conditional obtaining the user's input.

2) I would trace through this and see step by step what you expect to get at each step in your program. Think about if you could eliminate some of the for loops, how many are really essential for your program to run?

*************ONLY LOOK AT THIS WHEN YOU HAVE ATTEMPTED FIXING IT YOURSELF ***********

int main() {

        int N;

        printf("Please enter a number between 2 and 5 (including 2 and 5). \n");
        scanf("%d", &N);

        if (N<2 || N>5)
        {
            printf ("The number you entered is either less than 2 or greater than 5. \
            Please try again.");
            return EXIT_FAILURE;
        }

        for (int length= 0; length < N; length++) {
            if(length %2 == 1){
                    printf(" ");
            }
            for(int width = 0; width < N; width++) {

                printf("*");
            }
            printf("\n");
        }

        return EXIT_SUCCESS;
}

You print twice in your outer loop. Ie you have

for 1 to N, stepping by 1
    print line

    print leading space
    print line

So you print two times N lines when you want to print exactly N lines.

Either increase your increment (replace rows++ with rows += 2 which will cause you to only print an even number of lines, so you'll have to fix it for odd N ) or change to print only one line per iteration (where you'll have to fix the alternating leading space). The @malanb5 answer codes an example of the latter solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM