Spiral matrix pattern in c?

112 views Asked by At

Trying to fill the matrix in spiral form in this pattern and got problems in testing, can anyone help me?Code I tried and idea is below..

Create the following program: The user first enters the width S and height V of a matrix of integers (not greater than 100). Then the user enters the integers X and Y.

The matrix should be filled with the numbers X and Y as follows: in the middle of the matrix, ie at the coordinates [S/2,V/2], there is the number X (the solution will be recognized if the coordinates of the center deviate by 1 from the expected ones). Then the matrix is ​​filled spirally with the number X, first to the left, then down, then right, then up, etc. Between these spirals, space is left for the number Y. Example of the layout of the matrix for S=9, V=7, X=4 and Y= 6 is given below:

6 6 6 6 6 6 6 6 4
4 4 4 4 4 4 4 6 4
4 6 6 6 6 6 4 6 4
4 6 4 4 4 6 4 6 4
4 6 4 6 6 6 4 6 4
4 6 4 4 4 4 4 6 4
4 6 6 6 6 6 6 6 4

The element in the center of the matrix is ​​marked in bold. The completed matrix should be printed on the screen at the end.

#include <stdio.h>

int main() {
    int matrica[100][100], i, j, s, v, x, y;

    printf("Unesite sirinu i visinu matrice: ");
    scanf("%d%d", &s, &v);
    printf("Unesite X i Y: ");
    scanf("%d%d", &x, &y);

    int br = 2;
    int poc_i = s / 2;
    int poc_j = v / 2;
    int ind = 1;
    while (poc_i < v && poc_j < v) {
        for (int i = 0; i < br; i++) {
            matrica[poc_i][poc_j] = x;
            if (ind % 2 != 0) {
                poc_j--;
            } else {
                poc_j++;
            }
        }
        for (int i = 0; i < br; i++) {
            matrica[poc_i][poc_j] = x;
            if (ind % 2 != 0) {
                poc_i++;
            } else {
                poc_i--;
            }
        }
        br += 2;
        ind++;
    }

    for (int i = 0; i < s; i++) {
        for (int j = 0; j < v; j++) {
            if (matrica[i][j] != x) {
                matrica[i][j] = y;
            }
        }
    }

    for (i = 0; i < v; i++) {
        for (j = 0; j < s; j++)
            printf("%5d", matrica[i][j]);
        printf("\n");
    }
    return 0;
}
1

There are 1 answers

1
John Omielan On

Here's an updated version of your code which works properly:

#include <stdio.h>
#include <memory.h>

int main()
{
    int matrica[100][100], i, j, s, v, x, y, m;

    memset(matrica, 0, sizeof(matrica));

    printf("Unesite sirinu i visinu matrice: ");
    scanf("%d%d", &s, &v);
    printf("Unesite X i Y: ");
    scanf("%d%d", &x, &y);

    int br = 2;
    int poc_i = v / 2;
    int poc_j = s / 2;
    int ind = 1;
    m = (s > v ? s : v);
    while (poc_i < m && poc_j < m) {
        for (i = 0; i < br; i++) {
            if (0 <= poc_i && poc_i < v && 0 <= poc_j && poc_j < s) {
                matrica[poc_i][poc_j] = x;
            }
            if (ind % 2 != 0) {
                poc_j--;
            } else {
                poc_j++;
            }
        }
        for (i = 0; i < br; i++) {
            if (0 <= poc_i && poc_i < v && 0 <= poc_j && poc_j < s) {
                matrica[poc_i][poc_j] = x;
            }
            if (ind % 2 != 0) {
                poc_i++;
            } else {
                poc_i--;
            }
        }
        br += 2;
        ind++;
    }

    for (i = 0; i < v; i++) {
        for (j = 0; j < s; j++) {
            if (matrica[i][j] != x) {
                matrica[i][j] = y;
            }
        }
    }

    for (i = 0; i < v; i++) {
        for (j = 0; j < s; j++)
            printf("%5d", matrica[i][j]);
        printf("\n");
    }
    
    return 0;
}

Running this gives the following results with your sample input values:

Unesite sirinu i visinu matrice: 9 7
Unesite X i Y: 4 6

    6    6    6    6    6    6    6    6    4
    4    4    4    4    4    4    4    6    4
    4    6    6    6    6    6    4    6    4
    4    6    4    4    4    6    4    6    4
    4    6    4    6    6    6    4    6    4
    4    6    4    4    4    4    4    6    4
    4    6    6    6    6    6    6    6    4

As you can see, the outputted matrix matches what you're requesting. I made the following code changes to deal with several issues in your code:

  1. Your matrica matrix is allocated on the stack, so by default it contains random values. Your later code, however, assumes that anything not assigned with x should be set to y. Although it's quite unlikely, there's a chance some of the unassigned values may have a value of x, so that wouldn't work properly. Thus, I added the line memset(matrica, 0, sizeof(matrica)); to set all of the values to 0. This assumes that x will never be 0, so you may wish to change this if that's a possible valid value.

  2. Based on how you output the results, your memory is set up so the first index is the vertical position and the second one is the horizontal position, which is the opposite of the normal situation. Thus, I switched the v and s values around in the lines initializing the values of poc_i and poc_j.

  3. The width and height dimensions may possibly be considerably different, so your while loop condition based on only the height v may potentially exit prematurely. Instead, we want to use the maximum of these values, which I do with my m = (s > v ? s : v); line. Note I didn't try to ensure a max function or macro is available, and then use it, since it's used only once. In particular, as asked & explained in Is max(a,b) defined in stdlib.h or not?, we can't necessarily generally assume there's a macro defined for it in the standard headers. Thus, ensuring that an appropriate macro (or function) is defined here instead for just one use didn't seem to me to be particularly necessary (I believe it's even somewhat inappropriate to have done so in this situation).

  4. The indices used to set the matrica matrix values may be out of range, so I added checks to ensure that doesn't happen in the 2 places in the main while loop where the values are being updated.

  5. I switched the v and s values around in the loops to, for any values not yet set to x, to set them to y, as explained in my point #2 above.