There are three ways of passing two-dimensional arrays to functions. First, we can pass individual elements of the array. This is exactly same as passing elements of a one-dimensional array. Second, we can pass a single row of the two-dimensional array.
PASSING
TWO-DIMENSIONAL ARRAYS TO FUNCTIONS
There
are three ways of passing two-dimensional arrays to functions. First, we can
pass individual elements of the array. This is exactly same as passing elements
of a one-dimensional array. Second, we can pass a single row of the
two-dimensional array. This is equivalent to passing the entire one-dimensional
array to a function. This has already been discussed in the previous section..
Third, we can pass the entire two-dimensional array to the function. Refer
Figure 5.30 which shows the three ways of using two-dimensional arrays for
inter-function communication.
A
row of a two-dimensional array can be passed by indexing the array name with
the row number. When we send a single row of a two-dimensional array, then the
called function receives a one-dimensional array. Figure 5.31 illustrates how a
single row of a two-dimensional array is passed to the called function.
To
pass a two-dimensional array to a function, we use the array name as the actual
parameter. However, the parameter in the called function must indicate that the
array has two dimensions.
31.
Write a menu-driven program to read and display an m × n matrix. Also find the
sum, transpose, and product of two m x n matrices.
#include <stdio.h>
#include <conio.h>
Programming Tip:
A
compiler error will be generated if you omit the array size in the parameter
declaration for any array dimension other than the first.
void read_matrix (int mat [2] [2],
int, int);
void sum matrix (int mat1 [2] [2],
int mat2 [2] [2], int, int);
void mul matrix (int mat1 [2] [2],
int mat2 [2] [2], int, int);
void transpose_matrix (int mat2 [2]
[2], int, int);
void display_matrix (int mat [2]
[2], int r, int c);
int main()
{
int mat1 [2] [2], mat2 [2] [2];
clrscr();
do
{
printf("\n ******* MAIN MENU
********") ;
printf("\n 1. Read the two
matrices");
printf("\n 2. Add the
matrices");
printf("\n 3. Multiply the
matrices");
printf("\n 4. Transpose the
matrix");
printf("\n 5. EXIT");
printf("\n\n Enter your
option: ");
scanf("%d", &option);
switch (option)
{
case 1:
printf("\n Enter the number of
rows and columns of the matrix: ");
scanf("%d %d", &row,
&col);
printf("\n Enter the first
matrix: ");
read_matrix (matl, row, col);
printf("\n Enter the second
matrix: ");
read matrix (mat2, row, col);
break;
case 2:
sum matrix (mat1, mat2, row, col);
break;
case 3:
if (col = = row)
mul_matrix (mat1, mat2, row, col);
else
printf("\n To multiply two
matrices, number of columns in the first matrix must be equal to number of rows
in the second matrix");
break;
case 4:
transpose_matrix (mat1, row, col);
break;
}
} while (option != 5);
getch();
return 0;
}
void read matrix (int mat [2] [2],
int r, int c)
{
int i, j;
for (i = 0; i < r;i++)
{ printf("\n");
for(j = 0; j < C;j++)
{
printf("\t mat [%d] [%d] =
",i,j);
scanf("%d", &mat [i]
[j]);
}
}
}
void sum matrix (int mat1 [2] [2],
int mat2 [2] [2], int r, int c)
{
int i, j, sum [2] [2];
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
sum [i][j] = mat1 [i][j] + mat2
[i][j];
}
display_matrix (sum, r, c);
}
void mul_matrix (int mat1 [2] [2],
int mat2 [2] [2], int r, int c)
{
int i, j, k, prod [2] [2];
for (i=0; i<r; i++)
{
for(j=0;j<c;j++)
{
prod[i][j] = 0;
for (k=0;k<c;k++)
prod [i][j] += mat1 [i] [k] * mat2
[k] [j];
}
}
display_matrix (prod, r, c);
}
void transpose_matrix (int mat [2]
[2], int r,int c)
{
int i, j, tp_mat [2] [2];
for (i=0;i<r;i++)
{
for(j=0;j<c;j++)
tp_mat [j] [i] = mat [i][j];
}
display_matrix (tp_mat, r, c);
}
void display_matrix (int mat [2]
[2], int r,int c)
{
int i, j;
for (i=0;i<r;i++)
{
printf("\n");
for(j=0;j<c;j++)
printf("\t mat [%d] [%d] =
%d", i, j, mat [i] [j]);
}
}
Output
******* MAIN MENU ********
1. Read the two matrices
2. Add the matrices
3. Multiply the matrices
4. Transpose the matrix
5. EXIT
Enter your option: 1
Enter the number of rows and
columns of the matrix: 2 2
Enter the first matrix:
mat [0] [0] = 1 mat [0] [1] = 2
mat [1] [0] = 3 mat [1] [1] = 4
Enter the second matrix :
mat [0] [0] = 2 mat [0] [1] = 3
mat [1] [0] = 4 mat [1] [1] = 5
******* MAIN MENU ********
1. Read the two matrices
2. Add the matrices
3. Multiply the matrices
4. Transpose the matrix
5. EXIT
Enter your option: 2
mat [0] [0] = 3 mat [0] [1] = 5
mat [1] [0] = 7 mat [1] [1] = 9
``````````
32.
Write a program to fill a square matrix with value zero on the diagonals, 1 on
the upper right triangle, and -1 on the lower left triangle.
#include <stdio.h>
#include <conio.h>
void read_matrix (int mat [5] [5],
int);
void display_matrix (int mat [5]
[5], int);
int main()
{
int row;
int mat [5] [5];
clrscr();
printf("\n Enter the number of
rows and columns of the matrix ");
scanf("%d", &row);
read matrix (mat, row);
display_matrix (mat, row);
getch();
return 0;
}
void read_matrix (int mat [5] [5],
int r)
{
int i, j;
for (i = 0; i < r;i++)
{
for (j = 0; j < r;j++)
{
if (i==j)
mat [i] [j] = 0;
else if (i>j)
mat [i][j] = -1;
else
mat [i][j] = 1;
}
}
}
void display_matrix (int mat [5]
[5], int r)
{
int i, j;
for (i=0; i<r; i++)
{
printf("\n");
for(j=0;j<r;j++)
printf("\t %d", mat [i]
[j]);
}
}
Output
Enter the number of rows and
columns of the matrix: 2
0 1
-1 0
Programming in C: Unit II (a): Arrays : Tag: : with Example C Programs - Passing Two-Dimensional Arrays to Functions
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation