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;
}
Here's an updated version of your code which works properly:
Running this gives the following results with your sample input values:
Unesite sirinu i visinu matrice: 9 7
Unesite X i Y: 4 6
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:
Your
matricamatrix is allocated on the stack, so by default it contains random values. Your later code, however, assumes that anything not assigned withxshould be set toy. Although it's quite unlikely, there's a chance some of the unassigned values may have a value ofx, so that wouldn't work properly. Thus, I added the linememset(matrica, 0, sizeof(matrica));to set all of the values to 0. This assumes thatxwill never be 0, so you may wish to change this if that's a possible valid value.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
vandsvalues around in the lines initializing the values ofpoc_iandpoc_j.The width and height dimensions may possibly be considerably different, so your
whileloop condition based on only the heightvmay potentially exit prematurely. Instead, we want to use the maximum of these values, which I do with mym = (s > v ? s : v);line. Note I didn't try to ensure amaxfunction 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).The indices used to set the
matricamatrix 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.I switched the
vandsvalues around in the loops to, for any values not yet set tox, to set them toy, as explained in my point #2 above.