document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4.  
  5. struct matrix{
  6.   int **elements;
  7.   int rows;
  8.   int cols;
  9. };
  10.  
  11. matrix read(){
  12.   int rows = 0, cols = 0, i, j;
  13.   matrix m = {NULL, 0, 0};
  14.   printf("\\nEnter the number of rows: ");
  15.   scanf("%d", &rows);
  16.   printf("\\nEnter the number of columns: ");
  17.   scanf("%d", &cols);
  18.   if( rows <= 0 || cols <= 0){
  19.     printf("%dx%d matrix does not exist!", rows, cols);  
  20.   }else {
  21.     m.rows = rows;
  22.     m.cols = cols;
  23.     m.elements = (int **)malloc(m.rows * sizeof(int *));
  24.     for( i = 0; i < rows; i++){
  25.       m.elements[i] = (int *)malloc(m.cols * sizeof(int));
  26.       for( j = 0; j < cols; j++){
  27.         printf("\\nEnter element at %d x %d: ", i + 1, j + 1);
  28.         scanf("%d", &m.elements[i][j]);
  29.       }
  30.     }
  31.   }
  32.   return m;
  33. }
  34.  
  35. void display(char msg[], matrix m){
  36.   int i, j;
  37.   printf("\\n========== %s ==========\\n", msg);
  38.   for(i = 0; i < m.rows; i++){
  39.     for(j = 0; j < m.cols; j++){
  40.       printf("%d\\t", m.elements[i][j]);
  41.     }
  42.     printf("\\n");
  43.   }
  44. }
  45.  
  46. matrix process(matrix m1, matrix m2, char opr){
  47.   int i, j, k;
  48.   matrix m = {NULL, 0, 0};
  49.   if((opr == \'1\' || opr == \'2\') && (m1.rows != m2.rows || m1.cols != m2.cols)){
  50.     printf("\\nMatrix addition or subtraction is not possible since number of rows and columns of the two matrices are not equal.");
  51.   }else if(opr == \'3\' && m1.cols != m2.rows){
  52.     printf("\\nMatrix multiplication is not possible since number of columns of first matrix is not equal to number of rows of second matrix.");
  53.   }else{
  54.     if(opr == \'1\' || opr == \'2\'){
  55.       m.rows = m1.rows;
  56.       m.cols = m1.cols;
  57.       m.elements = (int **)malloc(m.rows * sizeof(int *));
  58.       for(i = 0; i < m1.rows; i++){
  59.         m.elements[i] = (int *)malloc(m.cols * sizeof(int));
  60.         for( j = 0; j < m1.cols; j++){
  61.           if(opr == \'1\'){
  62.             m.elements[i][j] = m1.elements[i][j] + m2.elements[i][j];
  63.           }else if(opr == \'2\'){
  64.             m.elements[i][j] = m1.elements[i][j] - m2.elements[i][j];
  65.           }
  66.         }
  67.       }
  68.     }else if(opr == \'3\'){
  69.       m.rows = m1.rows;
  70.       m.cols = m2.cols;
  71.       m.elements = (int **)malloc(m.rows * sizeof(int *));
  72.       for(i = 0; i < m1.rows; i++){
  73.         m.elements[i] = (int *)malloc(m.cols * sizeof(int));
  74.         for( j = 0; j < m2.cols; j++){
  75.           m.elements[i][j] = 0;
  76.           for(k = 0; k < m2.rows; k++){
  77.             m.elements[i][j] += m1.elements[i][k] * m2.elements[k][j];
  78.           }
  79.         }
  80.       }
  81.     }
  82.   }
  83.   return m;
  84. }
  85.  
  86. int main(){
  87.   while(1){
  88.     printf("\\nPress 1 to add two matrices\\nPress 2 to subtract\\nPress 3 to multiply two matrices\\nPress q to quit");
  89.     char opt = getch();
  90.     if(opt == \'q\' || opt == \'Q\'){
  91.       break;
  92.     }
  93.     matrix m1 = read();
  94.     matrix m2 = read();
  95.     if(m1.elements != NULL && m2.elements != NULL && (opt == \'1\' || opt == \'2\' || opt == \'3\')){
  96.       matrix m3 = process(m1, m2, opt);
  97.       if(m3.elements != NULL){
  98.         display("Matrix 1", m1);
  99.         display("Matrix 2", m2);
  100.         display("Result", m3);
  101.       }
  102.     }
  103.   }
  104.   return 0;
  105. }
');