Saturday, 25 February 2012

Addition in 2-dimensional array

#include<iostream>
using namespace std;

//this program gives the sum of even and odd entities of 2d array

int main()
{
const int row=3;
const int col=3;
//3x3 matrix
int arr[row][col];
//inputting values
for(int r_index=0;r_index<row;r_index++)
{
for(int c_index=0;c_index<row;c_index++)
{
cout<<"Enter the value at ["<<r_index<<"]["<<c_index<<"] th index : ";
cin>>arr[r_index][c_index];
}
}
int sum=0,sum_e=0,sum_o=0;
for(int i=0;i<row;i++)
{
for(int j=0;j<row;j++)
{
if(arr[i][j]%2==0)
{
sum_e = sum_e + arr[i][j];
}
else
{
sum_o = sum_o + arr[i][j];
}
}
}
cout<<"The Sum of Even Enteries is : "<<sum_e<<endl;
cout<<"The Sum of Odd Enteries is : "<<sum_o<<endl;
sum = sum_e + sum_o;
cout<<"The Sum of All Enteries is : "<<sum<<endl;
return 0;
}

<by asif>

No comments:

Post a Comment