C++ program to find the even and odd elements from a matrix

 This C++ program finds the even and odd elements in a matrix. The program uses two arrays even[] and odd[] to store the even and odd elements respectively.  The row and column values of the matrix are read into the variables m and n.  Then the program uses a nested for loop to read the elements one by one into the array a[][], at the same time the address of the first element of each row is stored in the pointer array p[]. Next the program uses modulo operator in the for loop to check each element and stores the even elements in the array even[] and odd elements in the array odd[].


Program


// Matrix even and odd elements c++ program
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],*p[10],m,n,i,j,k=0,l=0,even[20],odd[20];
cout<<"Enter the order of matrics: ";
cin>>m>>n;
cout<<"\n\nEnter the elements\n\n";
for(i=0;i<m;i++)
{
p[i]=&a[i][0];
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(*(p[i]+j)%2==0)
{
even[k]=*(p[i]+j);
k++;
}
else
{
odd[l]=*(p[i]+j);
l++;
}
}
}      
cout<<"\nEven elements\n\n";
for(i=0;i<k;i++)
cout<<even[i]<<" ";
cout<<"\n\nOdd elements\n\n";
for(i=0;i<l;i++)
cout<<odd[i]<<" ";getch();
}


1 comment:

Anonymous said...

Even and odd elements in matrix c++