The following code is for finding the 2 maximum areas of cross possible with equal lengths of hand from a 2D array(made from 'G' and 'B'.). The cross should be made up of 'G.' THe following code is giving me segmentation fault while/after scanning the 2D Matrix. Please help!!
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,j,k,l,p,q;
char M[10][10];
int m, n;
int min=0;
int rl=0;
int max1=1;
int max2=1;
int area;
int tarea;
int cu=0;
scanf("%d",&n);
scanf("%d",&m);
//matrix scan
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%c",&M[i][j]);
}
}
// finding first cross
for(i=0;i<n;i++){
for(j=0;j<n;j++){
k=j;
l=k;
p=i;
q=i;
while(M[i][k]!='B' || M[i][l]!='B' || l!=m || k!=0){
k--;
l++;
rl++;
}
while(M[p][j]!='B' || M[q][j]!='B' || p!=0 || q!=n){
p--;
q++;
cu++;
}
if(rl>cu){
min=cu;
}
else
min=rl;
area=min*4+1;
if(area>max2){
max2=area;
}
if(area>max1){
max1=area;
}
}
}
tarea=max1+max2;
printf("%d",tarea);
return 0;
}
mornto verify they don't go over. Note too that running this in a debugger will give you an answer just as fast as SO.M,p,q,rl,cu, etc are not meaningful. 3) separate code blocks (for, if, else, while, do...while, switch, case, default) by a single blank linescanf()) always tell the user what they are expected to enter. Otherwise, the user is left staring at a blank screen with a blinking cursor and no idea of what to do next.mcould benumRowsandncould benumCols. also, in modern C, if input the number of rows and columns first, then declare the matrixMas:char M[numRows][numCols];then the risk of overrunning the matrix bounds would be greatly reduced, perhaps even eliminated