Segmentation fault

When i run my program code i get segmentation fault or ID returned error code 1. why?

Hi Anuj… pls share ur code so that i can check it…

#include

using namespace std;

int SpiralPrint(int a[][100], int m,int n){

int startRow=0;

int startCol=0;

int endRow=m-1;

int endCol=n-1;

while (startRow<=endRow and startCol<=endCol)

{

   for(int i=startCol;i<=endCol;i++)

   {

       cout<<a[startRow][i]<<" ";

   }

   startRow++;

   for(int i=startRow;i<=endRow;i++)

   {

       cout<<a[i][endCol]<<" ";

   }

   endCol--;

   for(int i=endCol;i>=startCol;i--)

   {

       cout<<a[endRow][i]<<" ";

   }

   endRow--;

   for(int i=endRow;i>=startRow;i++)

   {

       cout<<a[i][startCol]<<" ";

   }

    startCol++;

}

}

int main(){

int n,m;

cin>>n>>m;

int val=1;

int a[100][100]={0};

for(int row=0;row<n;row++)

{

    for (auto col = 0; col < m; col++)

    {

        a[row][col]=val;

        val+=1;

    }

    cout<<endl;

}

SpiralPrint(a,m,n);

return 0;    

}

Sir, Please give some suggestions, why segmentation fault occur?

https://ide.codingblocks.com/s/594111 I have corrected ur code… its working fine now… actually at one place instead of i-- u had written i++ thats why u ere getting error

At square matrix it gives correct output but at this {5 3} input it’s give the this output:
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
1 2 3 0 0 0 0 0 9 8 7 4 5 6 0 6 5
why 4times zeros?

refer to the code i sent…
image
these 2 checks were missing in ur code…

still gives same output no change.

maybe u have interchanged n and m in ur code…
refer to code i sent… its working fine
image
see the output of 5 3
https://ide.codingblocks.com/s/594111

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Hello sir, I am not getting correct output. Can i know,Why?
#include

#include

using namespace std;

void sum_of_All_Submatrix(int a[100][100],int n)

{

int sum=0;

// For TopLeft..............

for(int li=0;li<n;li++)

{

    for(int lj=0;lj<n;lj++)

    {

        // For BottomRight.................

        for(int bi=li+1;bi<n;bi++) 

        {

            for(int bj=li+1;bj<n;bj++)

            {

                // For SUM...................

                for(int i=li;i<bi;i++)

                {

                    for(int j=lj;j<bj;j++)

                    {

                        sum=sum+a[i][j];

                    }

                }

            }

        }

    }

}

cout<<sum<<endl;

}

int main()

{

int n;

cin>>n;

int a[100][100]={0};

for(int i=0;i<n;i++){

   for(int j=0;j<n;j++)

   {

       cin>>a[i][j];

   }

}

sum_of_All_Submatrix(a,n);

return 0;

}

which ques is this?? pls raise a new doubt in that particular ques only…