-1

I have an exercise I need to submit. The user should enter 2 numbers, one of height and one of width, and the output should be a rectangle that the outer numbers are low and the inner ones are high. So I wrote it but it does not work in some numbers, for example 9 and 3, but in most things it works. I made 2 variables that are equal to the numbers entered by the user, this data goes down, the other 2 variables are just indicators, of the loops, they have no meaning If anyone has a different direction how to do it, I would love to hear, just in loops ..

For example:

1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 1
1 2 3 3 3 3 3 2 1
1 2 3 3 3 3 3 2 1
1 2 2 2 2 2 2 2 1
1 1 1 1 1 1 1 1 1

Thanks

#include <stdio.h>

int main() {
    int row, col, u, d;

    scanf("%d", &row);
    scanf("%d", &col);

    int row_1, col_1;

    for (u = 1, row_1 = row; u <= row; u++, row_1--) {
        for (d = 1, col_1 = col; d <= col; d++, col_1--) {
            if (col_1 < u  && row_1 > col_1)
                printf("%d", col_1);
            else if (u > d && row_1 >= d)
                printf("%d", d);
            else if(row_1 > u)
                printf("%d", u);
            else
                printf("%d", row_1);
        }
        printf("\n");
    }
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • 3
    What does it mean for the outer numbers to be low and inner numbers to be high? Can you give an example? – Paul Hankin May 21 '21 at 15:32
  • 2
    Also consider naming your variables more descriptively: it's very hard to read it now when `row` and `col` are not the current row and column but rather the number of same, I have no idea what the `_1` suffix is supposed to mean, let alone `u` and `d`. With proper naming it is also easier to spot any errors in your logic. – Arkku May 21 '21 at 15:41
  • I corrected what you said – אהרון קליין May 21 '21 at 16:04

1 Answers1

2

I would start by learning to describe the problem. For example:

  • the user enters the number of rows and columns to print (presumably 1–9 each)
  • for each position, print the distance to the nearest edge of the rectangle, starting from 1

The above description would quite easily lead to a solution like:

for (int row = 1; row <= num_rows; ++row) {
    int y_distance = distance_to_edge(row, num_rows);
    for (int col = 1; col <= num_cols; ++col) {
        int x_distance = distance_to_edge(col, num_cols);
        printf("%d", x_distance < y_distance ? x_distance : y_distance);
    }
    putchar('\n');
}

(Implementation of distance_to_edge left as exercise.)

Of course this is not the only solution; you can also take advantage of the fact that you know how the position changes (as you have attempted in your solution), but such optimisation may make it harder to get right. One easy option there is to break each loop down to two halves (the distance increases in the first half and decreases in the second)…

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • 1
    I can confirm that the posted code works, after implementing the function `distance_to_edge`. – Andreas Wenzel May 21 '21 at 16:33
  • (For those that feel that the distance can not start from 1, consider the edge to which the distance is calculated an unprinted barrier just outside the printed area. =) – Arkku May 21 '21 at 16:40