Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.70 KB | None | 0 0
  1. # include <iostream>
  2. # include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. void readArr(int, int, float**);
  8. void multArrs(int, float **, int, float **, float **);
  9. void printArr(int,int, float[3][3]);
  10.  
  11.  
  12. int main(int argc, char *argv[])
  13. {
  14.     int r1, c1, r2, c2;
  15.     float **Mat_1, **Mat_2, **Mat_Res;
  16.  
  17.     r1 = atoi(argv[1]);
  18.     c1 = atoi(argv[2]);
  19.     r2 = atoi(argv[3]);
  20.     c2 = atoi(argv[4]);
  21.  
  22.     if (c1 == r2)
  23.     {
  24.         cout<<endl<<"\tM A T R I X  1"<<endl<<endl;
  25.         readArr(r1,c1,Mat_1);
  26.         cout<<endl<<"\tYou wrote:"<<endl<<endl;
  27.         printArr(c1,r1,Mat_1);
  28.         cout<<endl<<"\tYou wrote:"<<endl<<endl;
  29.         printArr(c2,r2,Mat_2);
  30.         cout<<endl<<"\tM A T R I X  2"<<endl<<endl;
  31.         readArr(r2,c2,Mat_2);
  32.         multArrs(r1,Mat_1,c2,Mat_2,Mat_Res);
  33.         printArr(c1,r2,Mat_Res);
  34.  
  35.     }
  36.     else
  37.     {
  38.         cerr<<"Invalid parameters."<<endl;
  39.         exit(0);
  40.     }
  41.  
  42.  
  43. return 0;
  44. }
  45.  
  46.  
  47. void readArr(int r, int c, float** Mat)
  48. {
  49. int i,j;
  50. i = 0;
  51. j = 0;
  52.  
  53.     Mat =(float**)calloc(r,sizeof(float*));
  54.  
  55.     for(i=0;i<r;i++)
  56.         *(Mat+i)=(float*)calloc(c,sizeof(float));
  57.  
  58.        
  59.     for (i=0;i<r;i++)
  60.     {
  61.         for (j=0;j<c;j++)
  62.         {
  63.             cout<<"\tValue for Matrix ["<<i<<"]["<<j<<"]: ";
  64.             cin>>Mat[i][j];
  65.         }
  66.     }              
  67.        
  68. }
  69.  
  70.  
  71. void multArrs(int r1, float** Mat_1, int c2, float** Mat_2, float** Mat_res)
  72. {
  73.     int i,j,k,res;
  74.  
  75.     Mat_res =(float**)calloc(r1,sizeof(float*));
  76.  
  77.     for(i=0;i<r1;i++)
  78.         *(Mat_res+i)=(float*)calloc(c2,sizeof(float));
  79.  
  80.     for (i=0;i<r1;i++)
  81.     {
  82.  
  83.         for (j=0;j<c2;j++)
  84.         {  
  85.             for(k=0; k<r1; k++)
  86.                 res += Mat_1[i][k]*Mat_2[k][j];
  87.    
  88.         }
  89.            
  90.     }
  91.  
  92. }
  93.  
  94.  
  95. void printArr(int c,int r, float Mat[3][3])
  96. {
  97.     int i,j;
  98.         for (i=0;i<c;i++)
  99.         {
  100.             cout<<"\t|";
  101.             for (j=0;j<r;j++)
  102.                 cout<<Mat[i][j];
  103.             cout<<'|'<<endl;       
  104.         }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement