An interactive C program to do the following computation by providing the option using the switch statement 1.Add two matrices. 2. Subtract two matrices. 3. Multiply two matrices.

By | September 2, 2013

Let’s identify variables needed for this program.

In this program, we need to save matrices which consists Rows and Columns. To save this we need Two dimensional ARRAY.

Multi-dimensional Array is a nothing different than any Array but the only difference is that it has more than one dimension to it e.g. square has two dimension and cube has three dimension. The Dimension of array is decided by us in number of square brackets [] selected. If We select two dimension than we have to take two square brackets[][]. It has to be accessed with the help of index number ranging from 0 to n-1 and 0 to m-1. (e.g. num[n][m] will have num[0][0],num[0][1],num[2][2] so on.)

First few variables will be the one which will save the values of matrix with the choice of rows and columns entered by the user and it will be array A[MAX][MAX] and B[MAX][MAX] will be in the form A[M][N] and B[R][S] therefore we will need M,N,R,S will be needed as user decides the row and column of two matrices. Here since we are taking row and column of matrices from the user and hence we can not keep the square brackets empty we will fill that value by maximum value (MAX). This MAX variable will be defined as a constant above the program. Next variable will be array C[MAX][MAX] will be in the form C[M][S] for saving resultant value which will be displayed later. other variables will be I, J and K which will be for FOR Loop so in all TEN variables and ONE constant selected.
The identified variables are A[MAX][MAX],B[MAX][MAX],C[MAX][MAX],I,J,K,M,N,R,S,CH; and identified constant is MAX.

#define MAX 15
int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX],I,J,K,M,N,R,S,CH;

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

Best Loop for such kind of condition is FOR Loop the value of loop will start with 0 to M-1. Now here we need nested loop one loop inside the other. So that we can access all the elements with it. In each Case we will need First two nested loops for scanning required array values of Two matrices. Next nested loop will be nested with three FOR loop for multiplication or addition or subtraction of matrices depending on selection in switch-case construct and save it in the third empty matrix. and fourth nested loop for printing resultant array values as output.

This let’s you understand nested loop

for(I=0;I<M;I++)
{
for(J=0;J<N;J++)
{
scanf("%d",&A[I][J]);
}
}

let’s understand selection in switch-case construct

printf("\nSELECT UR CHOICE (1.ADD 2.SUB 3.MULT) : ");
     scanf("%d",&CH);
     switch(CH)
     {
 
    case 1 :
    {
       if(M==R && N==S)
       {
        \\code for matrix addition
       }
       else
       {
        printf("\nERROR : CONDITION FOR ADDITION");
        printf(" MATRICES IS NOT SATISFIED!");
       }
    break;
    }
    case 2 :
    {
       if(M==R && N==S)
       {
        \\code for matrix subtraction
       }
       else
       {
        printf("\nERROR : CONDITION FOR SUBTRACTION");
        printf(" MATRICES IS NOT SATISFIED!");
       }
    break;
    }
 
    case 3 :
    {
       if(N==R)
       {
        \\code for matrix multiplication
       }
       else
       {
        printf("\nERROR : CONDITION FOR MULTIPLICATION");
        printf(" MATRICES IS NOT SATISFIED!");
       }
    break;
    }
    default:
    {
       printf("YOU ENTERED WRONG CHOICE !!!");
    }
     }

Condition for Addition or subtraction of two Matrices (matrix) : In order to add two matrices, A and B, the number of rows and columns in A must be equal to the number of rows and columns in B. Thus, if A is an m x n matrix and B is r x s matrix, m=r and n=s.

if(M==R && N==S)
    {
    \\code for matrix addition
    }
else
       {
        printf("\nERROR : CONDITION FOR ADDITION");
        printf(" MATRICES IS NOT SATISFIED!");
       }

Adding or subtraction two matrices is simple as we have to add or subtract all the elements of the first matrix with same row and column to the other matrix. The matrices can only be added or subtracted if the row and column is same.

for(I=0;I<M;I++)
{
for(J=0;J<N;J++)
{
C[I][J]=A[I][J]+(plus or minus)-B[I][J];
}
}

Condition for Multiplication of two Matrices (matrix) : In order to multiply two matrices, A and B, the number of columns in A must be equal to the number of rows in B. Thus, if A is an m x n matrix and B is r x s matrix, n=r.

if(N==R)
        {
    \\code for matrix multiplication
    }
else
       {
        printf("\nERROR : CONDITION FOR ADDITION");
        printf(" MATRICES IS NOT SATISFIED!");
       }

Multiplication of two matrices is a bit complex as we have to do dot product of first row with first column multiply and add there product all the elements of the first matrix with row and other matrix with column. The matrices can only be added if the column of first matrix is equal to row of the second.

for(I=0;I<M;I++)
{
for(J=0;J<S;J++)
{
C[I][J]=0;
for(K=0;K<R;K++)
{
C[I][J]=C[I][J]+A[I][K]*B[K][J];
}
}
}

Finally print the Numbers of Two Dimension Resultant Array in the Matrix form by using Tab (\t) and New Line (\n) character in the nested Loop at the end of the program.

for(I=0;I<M;I++)
{
for(J=0;J<S;J++)
{
printf("%d\t",C[I][J]);
}
printf("\n");
}

C program code :

  1. #include<stdio.h>
  2. #define MAX 15
  3. void main()
  4. {
  5. int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX],I,J,K,M,N,R,S,CH;
  6. clrscr();
  7. printf("ENTER THE SIZE OF A MATRIX (M X N) : \n");
  8. printf("ENTER VALUE FOR M (LESS THAN 15) :");
  9. scanf("%d",&M);
  10. printf("ENTER VALUE FOR N (LESS THAN 15) :");
  11. scanf("%d",&N);
  12. printf("\nENTER THE SIZE OF B MATRIX (R X S) : \n");
  13. printf("ENTER VALUE FOR R (LESS THAN 15) :");
  14. scanf("%d",&R);
  15. printf("ENTER VALUE FOR S (LESS THAN 15) :");
  16. scanf("%d",&S);
  17. printf("\nSELECT UR CHOICE (1.ADD 2.SUB 3.MULT) : ");
  18. scanf("%d",&CH);
  19. switch(CH)
  20. {
  21. case 1 :
  22. {
  23. if(M==R && N==S)
  24. {
  25. printf("ENTER %d X %d MATRIX A VALUES\n",M,N);
  26. for(I=0;I<M;I++)
  27. {
  28. for(J=0;J<N;J++)
  29. {
  30. scanf("%d",&A[I][J]);
  31. }
  32. }
  33. printf("ENTER %d X %d MATRIX B VALUES\n",R,S);
  34. for(I=0;I<R;I++)
  35. {
  36. for(J=0;J<S;J++)
  37. {
  38. scanf("%d",&B[I][J]);
  39. }
  40. }
  41. for(I=0;I<M;I++)
  42. {
  43. for(J=0;J<N;J++)
  44. {
  45. C[I][J]=A[I][J]+B[I][J];
  46. }
  47. }
  48. printf("\nRESULT %d X %d MATRIX C VALUES ARE :\n",M,N);
  49. for(I=0;I<M;I++)
  50. {
  51. for(J=0;J<N;J++)
  52. {
  53. printf("%d\t",C[I][J]);
  54. }
  55. printf("\n");
  56. }
  57. }
  58. else
  59. {
  60. printf("\nERROR : CONDITION FOR ADDITION");
  61. printf(" MATRICES IS NOT SATISFIED!");
  62. }
  63. break;
  64. }
  65. case 2 :
  66. {
  67. if(M==R && N==S)
  68. {
  69. printf("ENTER %d X %d MATRIX A VALUES\n",M,N);
  70. for(I=0;I<M;I++)
  71. {
  72. for(J=0;J<N;J++)
  73. {
  74. scanf("%d",&A[I][J]);
  75. }
  76. }
  77. printf("ENTER %d X %d MATRIX B VALUES\n",R,S);
  78. for(I=0;I<R;I++)
  79. {
  80. for(J=0;J<S;J++)
  81. {
  82. scanf("%d",&B[I][J]);
  83. }
  84. }
  85. for(I=0;I<M;I++)
  86. {
  87. for(J=0;J<N;J++)
  88. {
  89. C[I][J]=A[I][J]-B[I][J];
  90. }
  91. }
  92. printf("\nRESULT %d X %d MATRIX C VALUES ARE :\n",M,N);
  93. for(I=0;I<M;I++)
  94. {
  95. for(J=0;J<N;J++)
  96. {
  97. printf("%d\t",C[I][J]);
  98. }
  99. printf("\n");
  100. }
  101. }
  102. else
  103. {
  104. printf("\nERROR : CONDITION FOR SUBTRACTION");
  105. printf(" MATRICES IS NOT SATISFIED!");
  106. }
  107. break;
  108. }
  109. case 3 :
  110. {
  111. if(N==R)
  112. {
  113. printf("ENTER %d X %d MATRIX A VALUES\n",M,N);
  114. for(I=0;I<M;I++)
  115. {
  116. for(J=0;J<N;J++)
  117. {
  118. scanf("%d",&A[I][J]);
  119. }
  120. }
  121. printf("ENTER %d X %d MATRIX B VALUES\n",R,S);
  122. for(I=0;I<R;I++)
  123. {
  124. for(J=0;J<S;J++)
  125. {
  126. scanf("%d",&B[I][J]);
  127. }
  128. }
  129. for(I=0;I<M;I++)
  130. {
  131. for(J=0;J<S;J++)
  132. {
  133. C[I][J]=0;
  134. for(K=0;K<R;K++)
  135. {
  136. C[I][J]=C[I][J]+A[I][K]*B[K][J];
  137. }
  138. }
  139. }
  140. printf("\nRESULT %d X %d MATRIX C VALUES ARE :\n",M,S);
  141. for(I=0;I<M;I++)
  142. {
  143. for(J=0;J<S;J++)
  144. {
  145. printf("%d\t",C[I][J]);
  146. }
  147. printf("\n");
  148. }
  149. }
  150. else
  151. {
  152. printf("\nERROR : CONDITION FOR MULTIPLICATION");
  153. printf(" MATRICES IS NOT SATISFIED!");
  154. }
  155. break;
  156. }
  157. default:
  158. {
  159. printf("YOU ENTERED WRONG CHOICE !!!");
  160. }
  161. }
  162. getch();
  163. }

Note:- whenever you have large number of inputs to be entered, we can use space instead of enter button all the time. You can see have its done in Output screen.

SCREEN SHOTS:-

C_program_Matrix_Algebra

C_program_Matrix_Algebra_Output

Leave a Reply