C++ program to find the row and column sums of a two-dimensional array of integers using pointers. The underlying representation of the 2-d array must be a single-subscripted array of integers with rows*columns number of elements. Calculate row sum and column sum and display the result along with the array.
#include<iostream.h>
#include<conio.h>
void main()
{
int
r,c,i,j;
clrscr();
cout<<"Enter the no. of rows and columns"<<endl;
cin>>r>>c;
int
*a=new int[r*c];
int
*colsum=new int[c];
int
*rowsum=new int[r];
cout<<"Enter the array elements\n";
for(i=0;i<r;i++)
{
rowsum[i]=0;
for(j=0;j<c;j++)
{
cin>>a[i*c+j];
rowsum[i]=rowsum[i]+a[i*c+j];
}
}
for(j=0;j<c;j++)
{
colsum[j]=0;
for(i=0;i<r;i++)
{
colsum[j]=colsum[j]+a[i*c+j];
}
}
cout<<"\nThe no.s are\n";
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<a[i*c+j]<<"\t";
}
cout<<endl;
}
cout<<"\nRow sum:\n";
for(i=0;i<r;i++)
{
cout<<"row "<<(i+1)<<":
"<<rowsum[i]<<endl;
}
for(i=0;i<c;i++)
{
cout<<"column
"<<(i+1)<<":"<<colsum[i]<<endl;
}
getch();
I think its a good program
ReplyDeleteCongrats
Thank you.
ReplyDeleteconst int SIZE=4;
ReplyDeletedouble sumColumn(const double m[][SIZE], int rowSize, int columnIndex);
can you pleas use this function to solve the same problem. with double output of course?