Friday, 27 January 2017

Passing 2D Array to Function

NIELIT O Level January 2016

Q 6.c :  Write a function to read a two dimensional matrix along with the number of rows and columns in it, from the user. The contents of the array and the number of rows and columns should be passed back to the calling function.

Solution : 

/*   ************************************************
           Girfa : Student Help
           Passing Two dimentional array to a function
           for more program visit : http://girfahelp.blogspot.com/p/c-language-assignment.html
     *************************************************/
#include<stdio.h>
#include<conio.h>
#define ROW 10
#define COL 10
void print(int (*)[],int,int);
void main()
{
     int ar[ROW][COL],r,c,i;
     clrscr();
     for(r=0,i=1;r<ROW;r++)
     {
           for(c=0;c<COL;c++)
           {
                ar[r][c]=i;
                i++;
           }
     }
     printf("Enter Number of row and column to read>> ");
     scanf("%d%d",&r,&c);
     print(ar,r,c);


}
void print(int (*ar)[COL],int r,int c)
{
     int a,b;
     if(r>ROW || c>COL)
           printf("\nOut of range row or column");
     else
     {
           for(a=0;a<r;a++)
           {
                for(b=0;b<c;b++)
                     printf("%d\t",ar[a][b]);
                printf("\n");
           }
           getch();

     }
}

2 comments: