BladeMechanics

matrix

Feb 23rd, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #ifndef dynamicmatrix_h
  4. #define dynamicmatrix_h
  5. #define R 7
  6. #define C 7
  7.  
  8. void display(int a[][C], int row, int column);
  9. void displayTranspose(int a[][C], int row, int column);
  10. float average(int a[][C], int row, int column);
  11. int findlowest(int a[][C], int row, int column);
  12. int isSquare(int row, int column);
  13. void matrixSum(int A[][C], int rowA, int colA, int B[][C], int rowB, int colB);
  14. void matrixProduct(int A[][C], int rowA, int colA, int B[][C], int rowB, int colB);
  15. void input(int(*input)[C], int rowS, int colS);
  16. void sizeinput(int *row, int *column, char designator);
  17.  
  18. #endif
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <conio.h>
  23. #include "dynamicmatrix.h"
  24.  
  25.  
  26. void main()
  27. {
  28.  
  29. int matrixA[R][C];
  30. int matrixB[R][C];
  31. int rowA, colA, rowB,colB;
  32.  
  33. sizeinput(&rowA, &colA, 'A');
  34. printf("\nPlease enter values for Matrix A");
  35. input(matrixA, rowA, colA);
  36. printf("\n- The numbers in matrix A are: \n");
  37. display(matrixA, rowA, colA);
  38.  
  39. sizeinput(&rowB, &colB, 'B');
  40. printf("\nPlease enter values for Matrix B");
  41. input(matrixB, rowB, colB);
  42. printf("\n- The numbers in matrix B are: \n");
  43. display(matrixB, rowB, colB);
  44.  
  45. printf ("\n\n\n- The average of the numbers in matrix A is: %.2f", average(matrixA, rowA, colA));
  46. printf ("\n\n- The transposed numbers in matrix A are: \n");
  47. displayTranspose(matrixA, rowA, colA);
  48. printf ("\n\n\n- The lowest number in matrix A is: %d", findlowest(matrixA, rowA, colA));
  49. printf("\nFor matrix A:");
  50. switch(isSquare(rowA, colA))
  51. {
  52. case 1: printf ("\n\n- The matrix is a square.");
  53. break;
  54. case 0: printf ("\n\n- The matrix is not a square.");
  55. break;
  56. }
  57. printf("\nFor matrix B:");
  58. switch(isSquare(rowB, colB))
  59. {
  60. case 1: printf ("\n\n- The matrix is a square.");
  61. break;
  62. case 0: printf ("\n\n- The matrix is not a square.");
  63. break;
  64. }
  65. matrixSum(matrixA, rowA, colA, matrixB, rowB, colB);
  66. matrixProduct(matrixA, rowA, colA, matrixB, rowB, colB);
  67. _getch();
  68. }
  69.  
  70. #include <stdio.h>
  71. #include <conio.h>
  72. #include "dynamicmatrix.h"
  73.  
  74.  
  75. void sizeinput(int *row, int *column, char designator)
  76. {
  77. int rowinput, columninput;
  78. printf_s("\nPlease enter the following information for matrix %c\n", designator);
  79. printf_s("\n Row: "); scanf_s("%d", &rowinput);
  80. printf_s("\nColumn: "); scanf_s("%d", &columninput);
  81. *row=rowinput;
  82. *column=columninput;
  83. }
  84.  
  85.  
  86. void input(int(*input)[C], int rowS, int colS)
  87. {
  88. int row, col;
  89. for (row = 0; row < rowS; row++)
  90. {
  91. for (col = 0; col < colS; col++)
  92. {
  93. printf("\nEnter value for pos[%d,%d]\n", col, row);
  94. scanf_s("%d", &input[row][col]);
  95. }
  96. }
  97. }
  98.  
  99. void matrixProduct(int A[][C], int rowA, int colA, int B[][C], int rowB, int colB)
  100. {
  101. int psum = 0, product[R][C];
  102. if (colA==rowB)
  103. {
  104. for (int row = 0; row < rowA; row++)
  105. {
  106. for (int col = 0; col < colB; col++)
  107. {
  108. for (int control = 0; control < colA; control++)
  109. {
  110. psum = psum + A[row][control] * B[control][col];
  111. }
  112. product[row][col] = psum;
  113. psum = 0;
  114. }
  115. }
  116. printf("\nThe product of matrix A and B is: \n");
  117. display(product, rowA, colB);
  118. }
  119. else
  120. printf_s("\nDimension mismatch! Cannot perform multiplication.");
  121. }
  122.  
  123.  
  124.  
  125. void matrixSum(int A[][C], int rowA, int colA, int B[][C], int rowB, int colB)
  126. {
  127. int row, col, sum[R][C];
  128. if (rowA==rowB && colA==colB)
  129. {
  130. for (row = 0; row < rowA; row++)
  131. {
  132. for (col = 0; col < colB; col++)
  133. {
  134. sum[row][col] = A[row][col] + B[row][col];
  135. }
  136. }
  137. printf("\nThe sum of matrix A and B is: \n");
  138. display(sum, row, col);
  139. }
  140. else printf_s("\nDimension mismatch! Cannot perform addition");
  141. }
  142.  
  143. void displayTranspose(int a[][C], int row, int column)
  144. {
  145. for (int rowD = 0; rowD<column; rowD++)
  146. {
  147. printf("\n");
  148. for (int columnD = 0; columnD<row; columnD++)
  149. {
  150. printf(" %d ", a[columnD][rowD]);
  151. }
  152. }
  153. }
  154.  
  155.  
  156. void display(int a[][C], int row, int column)
  157. {
  158. for (int rowD = 0; rowD<row; rowD++)
  159. {
  160. printf("\n");
  161. for (int columnD = 0; columnD<column; columnD++)
  162. {
  163. printf(" %d ", a[rowD][columnD]);
  164. }
  165. }
  166. }
  167.  
  168. float average(int a[][C], int row, int column)
  169. {
  170. float total=0;
  171. int count=0;
  172. for(int rowD=0; rowD<row; rowD++)
  173. {
  174. for(int columnD=0; columnD<column; columnD++)
  175. {
  176. total+=a[rowD][columnD];
  177. count++;
  178. }
  179. }
  180. return total/count;
  181. }
  182.  
  183. int findlowest(int a[][C], int row, int column)
  184. {
  185. int lowest=10;
  186. for(int rowD=0; rowD<row; rowD++)
  187. {
  188. for(int columnD=0; columnD<column; columnD++)
  189. {
  190. if (a[rowD][columnD]<lowest)
  191. lowest=a[rowD][columnD];
  192. }
  193. }
  194. return lowest;
  195. }
  196. int isSquare(int row, int column)
  197. {
  198. if (row==column)
  199. return 1;
  200. else
  201. return 0;
  202. }
Advertisement
Add Comment
Please, Sign In to add comment