Thursday, July 9, 2009

C++ sum array column?

I need a short code on C++ language.





I have a matrix that is full of numbers with size m x n... but m and n will vary in different case, so also need code to test the size out each times,





All i need to do is, sum the number in each columns, and then store in another array that have the same number of elements as the number of column in the matrix before.





thanks alot.

C++ sum array column?
#include%26lt;iostream.h%26gt;


void main()


{


int *mat1 = new mat1[100][100];


int i,j,m,n,sum=0;


int *arr1=new arr1[100];


cout%26lt;%26lt;"\nEnter the value of m %26amp; n of MXN matrix";


cin%26gt;%26gt;m%26gt;%26gt;n;


for(i=0;i%26lt;m;i++)


{


for(j=0;j%26lt;n;j++)


{


cin%26gt;%26gt;mat1[i][j];


}


}


for(i=0;i%26lt;m;i++)


{


for(j=0;j%26lt;n;j++)


{


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


}


arr1[i]=sum;


}


cout%26lt;%26lt;"\nEntered Matrix is as follows : \n";


for(i=0;i%26lt;m;i++)


{


for(j=0;i%26lt;n;j++)


{


cout%26lt;%26lt;mat1[i][j];


}


cout%26lt;%26lt;"\n";


}


cout%26lt;%26lt;"\nColumn addition is as follows : ";


for(i=0;i%26lt;m;i++)


{


cout%26lt;%26lt;arr1[i] "\n";


}


}





Use sizeof() function to get the size of the array wherever you need.... I am not sure where exactly you need the size of array.............
Reply:Do your own homework!!!!


No comments:

Post a Comment