C++ program to print alternate elements of a matrix
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],i,j,n,m;
cout<<"\n PROGRAM TO PRINT ALTERNATE ELEMENTS OF A MATRIX\n";
cout<<"\n ENTER THE NO: OF ROWS AND COLUMNS\n";
cout<<"ROW = ";
cin>>n;
cout<<"COLUMN = ";
cin>>m;
clrscr();
cout<<"\n ENTER THE DATA ELEMENTS\n ";
for (i=0;i<n;i++)
{
cout<<"\n "<<i+1<<" - ROW MEMBERS\n";
for (j=0;j<m;j++)
{
cin>>a[i][j];
}
}
if ((m%2!=0)||(m!=n))
{
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
if((i+j+2)%2==0)
{
cout<<"ARRAY ["<<i+1<<"]["<<j+1<<"] = "<<a[i][j]<<"\n";
}
}
}
}
else
{
for (i=0;i<n;i++)
{
for (j=0;j<m;j+=2)
{
cout<<"ARRAY ["<<i+1<<"]["<<j+1<<"] = "<<a[i][j]<<"\n";
if (j+=2>m)
break;
}
}
}
getch();
}
For more C programs - click here