Friday 13 January 2017

Friday 21 October 2016

Alternate Elements of 2-D Array

C++ PROGRAMS


In this program the number of rows, columns and the elements of the 2-D Array are entered by the user. The output is the arranged Array (just like the output of the previous program 2-D Array) and the Alternate Elements of the Array starting from the first element.

The Code consists a user-defined function in which the Array, number of rows and number of columns are passed as arguments.

Code:

#include<iostream.h>
#include<conio.h>

void alternate(int A[10][10], int r, int c)
{
int i,j;
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 {
  if((i+j)%2==0)
  cout<<A[i][j]<<" ";
 }
}

void main()
{
clrscr();
int i,j,r,c,A[10][10];
cout<<"Enter the number of Rows: ";
cin>>r;
cout<<"Enter the number of columns: ";
cin>>c;
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 {
  cin>>A[i][j];
 }
}
clrscr();
cout<<"Your Array is: \n";
for(i=0;i<r;i++)
{
 for(j=0;j<c;j++)
 {
  cout<<A[i][j]<<" ";
 }
 cout<<"\n";
}

cout<<"\nAlternate Elemnts of Array are: ";

alternate(A,r,c);

getch();
}

Output:









For any queries or suggestions please comment.