Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. // MatrixOps.c
  2. // To compute the matrix sum and matrix product of two matrices.
  3.  
  4. #include <stdio.h>
  5. #define MAX_ROW 10
  6. #define MAX_COL 10
  7.  
  8. void scanMatrix(float [][MAX_COL], int, int);
  9. void printMatrix(float [][MAX_COL], int, int);
  10. void sumMatrix(float [][MAX_COL], float [][MAX_COL], float [][MAX_COL], int, int);
  11. void prodMatrix(float [][MAX_COL], float [][MAX_COL], float [][MAX_COL], int, int, int);
  12.  
  13. int main(void) {
  14. float matrixA[MAX_ROW][MAX_COL]; // input matrix
  15. float matrixB[MAX_ROW][MAX_COL]; // input matrix
  16. float matrixC[MAX_ROW][MAX_COL]; // sum matrix
  17. int matrixArows, matrixAcols; // number of rows and columns for matrix A
  18. int matrixBrows, matrixBcols; // number of rows and columns for matrix B
  19.  
  20. printf("Matrix A:\n");
  21. printf("Enter number of rows and columns: ");
  22. scanf("%d %d", &matrixArows, &matrixAcols);
  23. scanMatrix(matrixA, matrixArows, matrixAcols);
  24.  
  25. printf("Matrix B:\n");
  26. printf("Enter number of rows and columns: ");
  27. scanf("%d %d", &matrixBrows, &matrixBcols);
  28. scanMatrix(matrixB, matrixBrows, matrixBcols);
  29.  
  30. if ((matrixArows == matrixBrows) &&
  31. (matrixAcols == matrixBcols)) {
  32. sumMatrix(matrixA, matrixB, matrixC, matrixArows, matrixAcols);
  33. printf("Sum matrix:\n");
  34. printMatrix(matrixC, matrixArows, matrixAcols);
  35. }
  36. else
  37. printf("Unmatched dimensions; cannot be added.\n");
  38.  
  39. if (matrixAcols == matrixBrows) {
  40. prodMatrix(matrixA, matrixB, matrixC,
  41. matrixArows, matrixAcols, matrixBcols);
  42. printf("Product matrix:\n");
  43. printMatrix(matrixC, matrixArows, matrixBcols);
  44. }
  45. else
  46. printf("Unmatched dimensions; cannot be multiplied.\n");
  47.  
  48. return 0;
  49. }
  50.  
  51. // To sum mtxA and mtxB to obtain mtxC
  52. void sumMatrix(float mtxA[][MAX_COL], float mtxB[][MAX_COL],
  53. float mtxC[][MAX_COL], int row_size, int col_size) {
  54. int row, col;
  55.  
  56. for (row=0; row<row_size; row++)
  57. for (col=0; col<col_size; col++)
  58. mtxC[row][col] = mtxA[row][col] + mtxB[row][col];
  59. }
  60.  
  61. // To multiply mtxA and mtxB to obtain mtxC
  62. void prodMatrix(float mtxA[][MAX_COL], float mtxB[][MAX_COL],
  63. float mtxC[][MAX_COL], int mtxA_row_size,
  64. int mtxA_col_size, int mtxB_col_size) {
  65.  
  66. int row, col, colb;
  67. float sum = 0.0;
  68.  
  69. for (row=0; row<mtxA_row_size; row++){
  70. for(col=0; col<mtxB_col_size; col++){
  71. for(colb = 0; colb<mtxA_col_size; colb++){
  72. sum += (mtxA[row][colb] * mtxB[colb][col]);
  73.  
  74. }
  75. mtxC[row][col] = sum;
  76. sum =0.0;
  77. }
  78. }
  79. // To be completed
  80. }
  81.  
  82. // To read values into mtx
  83. void scanMatrix(float mtx[][MAX_COL], int row_size, int col_size) {
  84. int row, col;
  85.  
  86. printf("Enter values for matrix:\n");
  87. for (row=0; row<row_size; row++)
  88. for (col=0; col<col_size; col++)
  89. scanf("%f", &mtx[row][col]);
  90. }
  91.  
  92. // To print values of mtx
  93. void printMatrix(float mtx[][MAX_COL], int row_size, int col_size) {
  94. int row, col;
  95.  
  96. for (row=0; row<row_size; row++) {
  97. for (col=0; col<col_size; col++)
  98. printf("%.2f\t", mtx[row][col]);
  99. printf("\n");
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement