My program should return the biggest spiral submatrix. For 2x2 matrix with values [[1,2], [4,3]] it returns 2x2 submatrix with same values (as it should), but when I enter 2x3 matrix with values [[1,2,5], [4,3,9]] it doesn't return anything, just prints that there are no spiral submatrices.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string.h>
void printMat(int n, int m, int mat[n][m]);
int isSpiral(int n, int m, int mat[n][m]);
void printSpiral(int n, int m, int mat[n][m]);
void copyMat(int n, int m, int mat[n][m], int mat2[n][m]);
int main(void) {
int n, m;
printf("Enter number of rows and columns: \n");
scanf("%d %d", &n, &m);
int mat[n][m];
printf("Enter %d elements of matrix: \n", n * m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
scanf("%d", &mat[i][j]);
}
}
printMat(n, m, mat);
printSpiral(n, m, mat);
return 0;
}
void printMat(int n, int m, int mat[n][m]) {
printf("\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
printf("%5d", mat[i][j]);
}
printf("\n");
}
}
int isSpiral(int n, int m, int mat[n][m]) {
int k = 0;
for(int i = 0; i <= n / 2 && k < n * m; i++) {
if((i > 0) && (mat[i][i] - mat[i][i - 1] != 1))
return 0;
else
k++;
for(int j = i; j < m - 1 - i && k < n * m; j++) {
k++;
if(mat[i][j + 1] - mat[i][j] != 1)
return 0;
}
for(int j = i; j < n - 1 - i && k < n * m; j++) {
k++;
if(mat[j + 1][m - 1 - i] - mat[j][m - 1 - i] != 1)
return 0;
}
for(int j = i; j < m - 1 - i && k < n * m; j++) {
k++;
if(mat[n - 1 - i][j] - mat[n - 1 - i][j + 1] != 1)
return 0;
}
for(int j = i + 1; j < n - 1 - i && k < n * m; j++) {
k++;
if(mat[j][i] - mat[j + 1][i] != 1)
return 0;
}
}
return 1;
}
void copyMat(int n, int m, int mat[n][m], int mat2[n][m]) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
mat2[i][j] = mat[i][j];
}
}
}
void printSpiral(int n, int m, int mat[n][m]) {
int mat3[n][m], i2 = 0, j2 = 0;
int maxi2 = -1, maxj2 = -1;
int mat2[n][m];
for(int g = n; g > 1; g--) {
for(int f = m; f > 1; f--) {
for(int k = 0; k < g - 1; k++) {
for(int l = 0; l < f - 1; l++) {
i2 = 0;
for(int i = k; i < g; i++) {
j2 = 0;
for(int j = l; j < f; j++) {
mat2[i2][j2] = mat[i][j];
j2++;
}
i2++;
}
if(isSpiral(i2, j2, mat2)) {
if(i2 >= maxi2 && j2 >= maxj2) {
maxi2 = i2;
maxj2 = j2;
copyMat(maxi2, maxj2, mat2, mat3);
}
}
}
}
}
}
if(maxi2 > 0 && maxj2 > 0)
printMat(maxi2, maxj2, mat3);
else
printf("\nNo spiral submatrix");
}
I tried debugging it but it didn't help much. I noticed that when it calls isSpiral function from printSpiral function it returns 0 for [[1,2], [4,3]] submatrix and it returns it when variable k is equal to 3 at second nested for, which shouldn't do.
This my first time asking question here so I apologize in advance if my question doesn't have proper form.
The problem was the way I was declaring my functions parameters.
Here, I created isSpiral(int n, int m, int mat[n][m])
, and the problem was int mat[n][m]
, because n
and m
would change the size of my mat
variable (matrix).
If the size was changed, the function would get wrong indexes, so I just added two new parameters, int i2
and int j2
:
isSpiral(int n, int m, int i2, int j2, int mat[i2][j2])
.
That way, the size of my matrix remains the same, constant, as it should, and limits for submatrices are variable.