Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
C Program Add Two Matrices Using 2D Arrays
1. C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 1
Handout#9
Assignment/ Program Statement:
Write a C program using a multidimensional (2D) array - to add two matrixes.
Learning Objectives:
Students will be able to
- declare the multidimensional (2D) array
- draw the flowchart for solution of the problem statement using
multidimensional (2D) array
- write the algorithm for the problem statement using multidimensional (2D)
array
- write the program using multidimensional (2D) array
Theory:
C programming language allows multidimensional arrays. Here is the
general form of a multidimensional array declaration-
type name[size1][size2]...[sizeN];
For example, the following declaration creates a three dimensional integer
array −
int threedim[5][10][4];
Two-dimensional Arrays
The simplest form of multidimensional array is the two-dimensional array. A
two-dimensional array is, in essence, a list of one-dimensional arrays. To
declare a two-dimensional integer array of size [x][y], you would write
something as follows −
type arrayName [ x ][ y ];
Where type can be any valid C data type and arrayName will be a valid C
identifier. A two-dimensional array can be considered as a table which will
have x number of rows and y number of columns. A two-dimensional array
a, which contains three rows and four columns can be shown as follows –
2. C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 2
Thus, every element in the array a is identified by an element name of the
forma[ i ][ j ], where 'a' is the name of the array, and 'i' and 'j' are the
subscripts that uniquely identify each element in 'a'.
Initializing Two-Dimensional Arrays
Multidimensional arrays may be initialized by specifying bracketed values
for each row. Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11}/* initializers for row indexed by 2 */
};
The nested braces, which indicate the intended row, are optional. The
following initialization is equivalent to the previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
[Reference: http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm ]
4. C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 4
Algorithm:
1. Start
2. Read the elements of first matrix
3. i=0, j=0
4. if i<2
a. if j<2
read first matrix a[i,j] element
j=j+1
got to step 4a
else
i=i+1
go to step 4
else
go to step 5
5. Read the elements of second matrix
6. if i<2
a. if j<2
read second matrix b[i,j] element
j=j+1
got to step 6a
else
i=i+1
go to step 6
else
go to step 7
7. if i<2
a. if j<2
c[i][j]=a[i][j]+b[i][j];
j=j+1
got to step 7a
else
i=i+1
go to step 7
else
go to step 8
5. C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 5
8. Display sum of matrix
9. if i<2
a. if j<2
display matrix element c[i,j]
j=j+1
got to step 9a
else
i=i+1
go to step 9
else
go to step 10
10.Stop
Program:
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], c[2][2];
int i,j;
printf("Enter the elements of 1st matrixn");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{
printf("Enter a%d%d: ",i+1,j+1);
scanf("%f",&a[i][j]);
}
printf("Enter the elements of 2nd matrixn");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{
printf("Enter b%d%d: ",i+1,j+1);
scanf("%f",&b[i][j]);
}
for(i=0;i<2;++i)
6. C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 6
for(j=0;j<2;++j)
{
c[i][j]=a[i][j]+b[i][j];
}
printf("nSum Of Matrix:");
for(i=0;i<2;++i)
for(j=0;j<2;++j)
{
printf("%.1ft",c[i][j]);
if(j==1) /* To display matrix sum in order. */
printf("n");
}
return 0;
}
Input:
Enter the elements of 1st matrix
Enter a11: 2;
Enter a12: 2;
Enter a21: 1;
Enter a22: 3;
Enter the elements of 2nd matrix
Enter b11: 1;
Enter b12: 2;
Enter b21: 3;
Enter b22: 4;
Output:
Sum of Matrix:
3 4
4 7
Practice Problem Statement:
1. Write a program to find the transpose of a matrix
7. C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 7
2. Write a program to multiply the matrix
Conclusion:
Thus a C program using a multidimensional array - to add two matrixes is
implemented.
Learning Outcomes:
At the end of this assignment, students are able to
- declare the multidimensional array.
- draw the flowchart for solution of the problem statement using
multidimensional array.
- write the algorithm for the problem statement using multidimensional array
- write the program using multidimensional array.