Pages

100 Contemporary Living Room Designs

Saturday, September 1, 2012

ROW SUM AND COLUMN SUM OF A 2-D MATRIX USING POINTER


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();
}

OUTPUT


3 comments:

  1. I think its a good program
    Congrats

    ReplyDelete
  2. const int SIZE=4;
    double 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?

    ReplyDelete