How to resolve 'Program received signal SIGSEGV, Segmentation fault.'?

Rohan J Aditya Source

I'm getting the error mentioned in the title when I debug the code. I know it's not pretty. Everything until PART 5 works properly.

I've tested it without PART 6 and it works properly. What i'm trying to do with PART 6 is print elements of a 2D array in a clockwise spiral order without using any loop structures.

#include<stdio.h>

int check;
int arrprint(int a[20][20], int b[20][20], int n, int count, int check);
void loopprint(int a[20][20], int i, int n ,int choice1, int choice2, int x, 
int y);
void clockSpiral(int a[20][20], int n, int k, int l, int mc, int mr);

int main()
{
//PART 1

 int n,a[20][20];
 printf("\nEnter the order of the matrix(<=20) : ");
 scanf("%d",&n);
 int i,j;
 printf("\nEnter the elements of the matrix :\n");
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   scanf("%d",&a[i][j]);
 printf("\nThe matrix in row major form is :\n");
 for(i=0;i<n;i++)
  { 
   for(j=0;j<n;j++)
    printf("%d ",a[i][j]);
   printf("\n"); 
  }

// PART 2

 printf("\nThe matrix in column major form is : ");
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   printf("%d ",a[j][i]);

 int c[20][20];
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   c[i][j]=a[i][j];

 // PART 4

 printf("\nThe matrix in anti diagonal form is : ");
 int count=0;
 printf("\n");
 check=0;
 for(count=1;count<=n;count++)
 {
  int b[count][count];
  arrprint(a,b,n,count,check);
 }


 count=count-2;
 check=1;
 for(count;count>0;count--)
 {
  int b[count][count];
  arrprint(a,b,n,count,check);
 } 

// PART 3

 int d[20][20];
 for(i=0;i<n;i++)
  for(j=0;j<n;j++)
   d[i][j]=a[i][j];

 printf("\nThe matrix in diagonal form is : ");
 printf("\n");
 check=3;
 for(count=1;count<=n;count++)
 {
  int b[count][count];
  arrprint(a,b,n,count,check);
 }

 count=count-2;
 check=4;
 for(count;count>0;count--)
 {
  int b[count][count];
  arrprint(a,b,n,count,check);
 }

//PART 5

 printf("\nThe matrix in anti clockwise spiral order is : ");
 count=0;
 int k=0,l=0,mr=n,mc=n;
 while(count<=n*n && k<mr && l<mc)
 {
  for(i=k;i<mr;i++)
  { 
   printf("%d ",a[i][l]);
   count++;
  }
  l++;
  for(i=l;i<mc;i++)
  {
   printf("%d ",a[mr-1][i]);
   count++;
  }
  mr--;
  for(i=mr-1;i>=k;i--)
  {
   printf("%d ",a[i][mc-1]);
   count++;
  }
  mc--;
  for(i=mc-1;i>=l;i--)
  {
   printf("%d ",a[k][i]);
   count++;
  }
  k++;
 }
 printf("\n");

//PART 6

 printf("\nThe matrix in clockwise spiral form is : ");
 clockSpiral(a,n,0,0,n,n);
 printf("\n");

 return 0;
}

int arrprint(int a[20][20], int b[20][20], int n, int count, int check)
{
 int i,j,ai,aj;
 if(check==0)
 {
  for(i=0,ai=0;ai<n && i<count;ai++,i++)
   {
    for(aj=0,j=0;aj<n && j<count;aj++,j++)
     {
      b[i][j]=a[i][j];
      if(i+j==count-1)
       printf("%d ",b[i][j]);
     }
   } 
 }

 if(check==1)
 {
  for(ai=n-count,i=0;ai<n && i<count;ai++,i++)
   { 
    for(aj=n-count,j=0;aj<n && j<count;aj++,j++)
    {
     b[i][j]=a[ai][aj];
     if(i+j==count-1)
      printf("%d ",b[i][j]);
    }  
   }
 }

 if(check==3)
 {
  for(aj=0,j=0;j<count && aj<n;j++,aj++)
   {
    for(ai=n-1,i=count-1; i>=0 && ai>=0;i--,ai--)
     {
      b[i][j]=a[ai][aj];
      if(i==j)
       printf("%d ",b[i][j]);
     }
   }
 }

 if(check==4)
{
 for(aj=n-count,j=0;j<count && aj<n;j++,aj++)
  {
    for(ai=0,i=0;ai<n && i<count;ai++,i++)
    {
     b[i][j]=a[ai][aj];
     if(i==j)
      printf("%d ",b[i][j]);
     }
   }
 } 

}


void loopprint(int a[20][20], int i, int n, int choice1, int choice2, int x, 
int y)
{
 switch(choice1)
 {
  case 1:
   if(choice2==1)
   {
    if(i<n)
    {
     y=i;
     printf("%d ",a[x][y]);
     i++;
     loopprint(a,i,n,1,1,x,i);
    }
   else 
     return;
   }
   if(choice2==2)
   {
    if(i<n)
    {
     x=i;
     printf("%d ",a[x][y]);
     loopprint(a,i,n,1,2,i,y);
    }
   else
     return;
   }
   break;
  case 2:
   if(choice2==1)
   {
    if(i<=n)
    {
     y=i;
     printf("%d ",a[x][y]);
     i--;
     loopprint(a,i,n,2,1,x,i);
    }
    else
     return;
   }
   if(choice2==2)
   {
    if(i>=n)
    {
     x=i;
     printf("%d ",a[x][y]);
     i--;
     loopprint(a,i,n,2,2,i,y);
    }
    else
     return;
   }
   break;
   default:
    return;
 }
 return;
}

void clockSpiral(int a[20][20], int n, int k, int l, int mc, int mr)
{
 if(k<mr && l<mc)
 {
  loopprint(a,l,mc,1,1,k,l);
  k++;
  loopprint(a,k,mr,1,2,k,mc-1);
  mc--;
  loopprint(a,mc-1,l,2,1,mr-1,mc-1);
  mr--;
  loopprint(a,mr-1,k,2,2,mr-1,l);
  l++;
  clockSpiral(a,n,k,l,mc,mr);
 }
 else
  return;
}
csegmentation-fault

Answers

answered 6 months ago Paul Ogilvie #1

The following part has an error that can cause a segment violation:

// PART 4

printf("\nThe matrix in anti diagonal form is : ");
int count=0;
printf("\n");
check=0;
for(count=1;count<=n;count++)
{
    int b[count][count];
    arrprint(a,b,n,count,check);
}

The index count goes up to and including n (indexes in C go from 0 to n-1).

Function arrprint has a side effect that it actually copies count elements from a to b.

If n==20 then arrprint will copy element 21 of a to element 21 of b. However, these elements do not exist: seg fault!

In addition, the parameter of arrprint int a[20][20] is not the same as an array a[count][count] with count!=20 because the first consists of rows of 20 ints but the second consists of rows of <20 ints. Thus the addressing

b[i][j]=a[i][j];

will be translated by the compiler as

b[i*20+j]=a[i*20+j];

which is not the same as

b[i*count+j]=a[i*count+j];

This will also cause a seg fault.

You should define the function as:

int arrprint(int a[count][count], int b[count][count], int n, int count, int check)

comments powered by Disqus