sum of the diagonals of a matrix. program giving segmentation error !

After entering the values of the matrix, it is giving me segmentation error.
Can anybody explain why ?
also "if possible", correct it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
using namespace std;

int main()
{
    int sum(int n, int **abc);
    int pi; int z;
    cout<<"Enter the size of the matrix: "; cin>>pi; cout<<"\n";
    //The matrix should be a square so as to have diagonals
    int **arr=new int*[pi];
    for(z=0; z<pi; z++)
    {
        arr[z]=new int[pi];
    }
    cout<<"Enter its elements :\n";
    for(int i=0; i<pi; i++)
    {
        cout<<"Row "<<i+1<<":\n";
        for(int j=0; j<pi; j++)
        {
            cout<<"Column "<<j+1<<": ";
            cin>>arr[i][j]; cout<<"\n";
        }
        cout<<endl;
    }
    sum(pi, arr); return 0;
}

int sum(int n, int **abc)
{
    int sum_left=0; int sum_right=0; int middle; int sum_total; int mid; int mod=n%2;
    for(int x=0; x<n; x++)
    {
        for(int y=0; y<n; y++)
        {
            if(x==y)
            {
                sum_left=abc[x][y]+sum_left;
            }
        }
    }
    for(int g=0; g<n; g++)
    {
        for(int h=n-1; h>=0; h--)
        {
            sum_right=abc[g][h]+sum_right;
        }
    }
    if(mod!=0)
    {
        mid=n-1/2;  middle=abc[mid][mid];
        sum_total=sum_left+sum_right-middle;
        cout<<"The sum of diagonals is "<<sum_total<<".\n";
    }
    if(mod==0)
    {
        sum_total=sum_left+sum_right;
        cout<<"The sum of diagonals is "<<sum_total<<".\n";
    }
}
Last edited on
The way that you calculate mid is wrong.

The function also needs a return statement.
Thanks ;)
I have made the corrections and it is working fine now.
Here is the new code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
using namespace std;

int main()
{
    int sum(int n, int **abc);
    int pi; int z;
    cout<<"Enter the size of the matrix: "; cin>>pi; cout<<"\n";
    //The matrix should be a square so as to have diagonals
    int **arr=new int*[pi];
    for(z=0; z<pi; z++)
    {
        arr[z]=new int[pi];
    }
    cout<<"Enter its elements :\n";
    for(int i=0; i<pi; i++)
    {
        cout<<"Row "<<i+1<<":\n";
        for(int j=0; j<pi; j++)
        {
            cout<<"Column "<<j+1<<": ";
            cin>>arr[i][j]; cout<<"\n";
        }
        cout<<endl;
    }
    cout<<endl<<"The matrix you entered is :\n";
    for(int e=0; e<pi; e++)
    {
        for(int f=0; f<pi; f++)
        {
            cout<<arr[e][f]<<" ";
        }
        cout<<endl;
    }
    cout<<endl; sum(pi, arr); return(0);
}

int sum(int n, int **abc)
{
    int sum_left=0; int sum_right=0; int middle; int sum_total; int mid; int mod=(n%2);
    for(int x=0; x<n; x++)
    {
        for(int y=0; y<n; y++)
        {
            if(x==y)
            {
                sum_left=abc[x][y]+sum_left;
            }
        }
    } int h=n-1;
    for(int g=0; g<n; g++)
    {
        if(h>=0)
        {
            sum_right=abc[g][h]+sum_right; h--;
        }
    }
    if(mod!=0)
    {
        mid=(n-1)/2;  middle=abc[mid][mid];
        sum_total=sum_left+sum_right-middle;
        cout<<"The sum of diagonals is "<<sum_total<<".\n";
    }
    if(mod==0)
    {
        sum_total=sum_left+sum_right;
        cout<<"The sum of diagonals is "<<sum_total<<".\n";
    }
}
Topic archived. No new replies allowed.