#include<stdio.h>
#include<conio.h>
void input(int,int);
void traverserow(int,int);
void traversecolumn(int,int);
int a[5][5];
void main()
{
int r,c,c1;
clrscr();
printf("program to demonstrate reading and printing(row major and column major wise) the elements in the 2-D array with the help of functions.");
printf("\n\n\t-------------------------OUTPUT------------------------");
printf("\n\nenter the no. of rows and columns for a matrix");
scanf("%d%d",&r,&c);
input(r,c);
printf("\nenter choice 1. for traversing the 2-D array row major wise");
printf("\nenter choice 2. for traversing the 2-D array column major wise");
printf("\nenter your choice for traversing a 2-D array");
scanf("%d",&c1);
switch(c1)
{
case 1:
traverserow(r,c);
break;
case 2:
traversecolumn(r,c);
break;
default:
printf("\nyou have entered a wrong choice");
break;
}
getch();
}
void input(int r,int c)
{
int i,j;
printf("\nenter the elements in the 2-D array");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
void traverserow(int r,int c)
{
int i,j;
printf("\ntraversing the 2-D array row major wise");
printf("\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t%d",a[i][j]);
}
printf("\n");
}
}
void traversecolumn(int r,int c)
{
int i,j;
printf("\ntraversing the 2-D array column major wise");
printf("\n");
for(i=0;i<c;i++)
{
for(j=0;j<r;j++)
{
printf("\t%d",a[j][i]);
}
printf("\n");
}
}