#include<iostream>
usingnamespace 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=newint*[pi];
for(z=0; z<pi; z++)
{
arr[z]=newint[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";
}
}
#include<iostream>
usingnamespace 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=newint*[pi];
for(z=0; z<pi; z++)
{
arr[z]=newint[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";
}
}