1

I have tried like this, but I can print this pattern correctly only for 4.

When I give 5,2 or other numbers as input the code fails...

How can I modify this program to match the exact output? I have searched in many platforms but cannot find the answer..

This is the expected output:

input: 2

2 2 2
2 1 2 
2 2 2

input: 5

5 5 5 5 5 5 5 5 5 
5 4 4 4 4 4 4 4 5 
5 4 3 3 3 3 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 2 1 2 3 4 5 
5 4 3 2 2 2 3 4 5 
5 4 3 3 3 3 3 4 5 
5 4 4 4 4 4 4 4 5 
5 5 5 5 5 5 5 5 5

input: 7

7 7 7 7 7 7 7 7 7 7 7 7 7 
7 6 6 6 6 6 6 6 6 6 6 6 7 
7 6 5 5 5 5 5 5 5 5 5 6 7 
7 6 5 4 4 4 4 4 4 4 5 6 7 
7 6 5 4 3 3 3 3 3 4 5 6 7 
7 6 5 4 3 2 2 2 3 4 5 6 7 
7 6 5 4 3 2 1 2 3 4 5 6 7 
7 6 5 4 3 2 2 2 3 4 5 6 7 
7 6 5 4 3 3 3 3 3 4 5 6 7 
7 6 5 4 4 4 4 4 4 4 5 6 7 
7 6 5 5 5 5 5 5 5 5 5 6 7 
7 6 6 6 6 6 6 6 6 6 6 6 7 
7 7 7 7 7 7 7 7 7 7 7 7 7 

Here is the code:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int n;
    scanf("%d", &n);
    // Complete the code to print the pattern.
    int limit = (n * 2) - 1;
    int row, column;

    for (row = 1; row <= limit; row++) {
        for (column = 1; column <= limit; column++) {
            if (column == 1 || column == 7 || row == 1 || row == 7)
                printf("%d ", n);
            else if (column == 2 || column == 6 || row == 2 || row == 6)
                printf("%d ", n - 1);
            else if (column == 3 || column == 5 || row == 3 || row == 5)
                printf("%d ", n - 2);
            else if (column == 4 || column == 4 || row == 4 || row == 4)
                printf("%d ", n - 3);
        }
        printf("\n");
    }
    return 0;
}
1
  • What pattern do you mean? What are the desired and actual output? Please edit your question by adding additional details. Commented Jul 29, 2022 at 9:54

1 Answer 1

2

You are working too hard. (Or, depending on your point of view, not working hard enough!). Instead of chaining a lot of if statments, just compute the value based on the current location. Something like:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define max(x, y) ((x) > (y) ? (x) : (y))

int
main(int argc, char **argv)
{
    char *k = "";
    int n = argc > 1 ? strtol(argv[1], &k, 10) : 5;
    if( *k || n > 9 || n < 1 ){
        fprintf(stderr, "Invalid argument.  Must be 0 < n < 10\n");
        return 1;
    }
    for( int x = 1 - n; x < n; x += 1 ){
        for( int y = n - 1; y > -n; y -= 1 ){
            printf("%d", 1 + max(abs(x),abs(y)));
            putchar(y == 1 - n ? '\n' : ' ');
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Nice and efficient! May I suggest the javelin operator: for (int y = n; y -->- n;)
This code gives exact output only for 5 (input).
@chqrlie Your loop would run 2n times though. for (int y = n; y -->- n + 1; ) would be fully equivalent.
@YurkoFlisk: indeed... more dashes needed: for (int y = n; y ---1>- n; )
@No_name_instant Pass the parameter as an argument, not in the input stream.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.