1

The orange crosses are made from 'G' of the matrix and are valid. Blue ones are made from 'B' and are invalid

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;
}
6
  • 4
    Kind of a disaster of code to try to read, but I see no test for bounds (your arrays are 10x10 but you never check m or n to verify they don't go over. Note too that running this in a debugger will give you an answer just as fast as SO. Commented Jan 29, 2016 at 20:55
  • 1
    You didn't give any input example. Also, use a debugger. You're looping both i and j to n, maybe that is the issue? Commented Jan 29, 2016 at 20:56
  • for ease of understanding and readability by us humans, 1) please indent the code consistently. Indent after every opening brace '{',. Un-indent before every closing brace '}', Perform the indenting even when optional braces are not actually in the code. 2) use meaningful variable names. 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 line Commented Jan 30, 2016 at 19:24
  • when wanting input from the user (see the calls to scanf()) 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. Commented Jan 30, 2016 at 19:27
  • as an example of meaningful names: m could be numRows and n could be numCols. also, in modern C, if input the number of rows and columns first, then declare the matrix M as: char M[numRows][numCols]; then the risk of overrunning the matrix bounds would be greatly reduced, perhaps even eliminated Commented Jan 30, 2016 at 19:31

1 Answer 1

1

Your two while loops can very easily produce an out-of-bounds access to array M. Consider, for example ...

while(M[i][k]!='B' || M[i][l]!='B' || l!=m || k!=0){
    k--;
    l++;
    rl++;
}

... on the first pass through the containing loop, where i, k, and l are all initially zero. Suppose further that M[0][0] has a value different from 'B'. The body of the loop will then be executed, resulting in k taking the value -1, and then the control expression evaluated again, resulting in an attempt to read the value of M[0][-1]. The result is undefined, but a segmentation fault -- then or later -- is entirely plausible.

You need to do your bounds tests first, and you need to use && rather than || to ensure that the loop in fact exits when the bounds are reached. It's not immediately clear to me what you want to do exactly on the edge, but here's the general idea:

while((k > 0) && (l < m) && (M[i][k] != 'B' || M[i][l] != 'B')) {
    k--;
    l++;
    rl++;
}

Similar applies to your other while loop.

Those are not the only issues, but they are probably the ones responsible for the behavior you asked about.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.