Advertisement
sabertooth09

Matrix operation

Oct 13th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. ll a[1009][1009],b[1009][1009],c[1009][1009];
  6.  
  7. int main()
  8. {
  9.     //as we declared the arrays globally so no need to set their value to 0 before applying multiplication
  10.     ll n,m,i,j,p,q;
  11.     char ax;
  12.     printf("Welcome To Matrix Operations\n1.Type 'A/a' to perform addition\n2.Type 'S/s' to perform Subtraction\n3.Type 'M/m' to perform Multiplication\n");
  13.     scanf("%c",&ax);
  14.     if(ax=='A' || ax=='a')
  15.     {
  16.         printf("To perform Addition 2 Matrix Row and column must be same\nInputs for row and column\n");
  17.         scanf("%lld %lld",&n,&m);
  18.         puts("Inputs for First matrix");
  19.         for(i=0; i<n; i++)
  20.             for(j=0; j<m; j++)
  21.                 scanf("%lld",&a[i][j]);
  22.         puts("Input for second matrix");
  23.         for(i=0; i<n; i++)
  24.             for(j=0; j<m; j++)
  25.                 scanf("%lld",&b[i][j]);
  26.         for(i=0; i<n; i++)
  27.             for(j=0; j<m; j++)
  28.                 c[i][j]=a[i][j]+b[i][j];
  29.         printf("\nOutputs\n");
  30.         for(i=0; i<n; i++)
  31.         {
  32.             for(j=0; j<m; j++)
  33.                 printf("%lld ",c[i][j]);
  34.             printf("\n");
  35.         }
  36.     }
  37.     else if(ax=='S' || ax=='s')
  38.     {
  39.         printf("To perform Subtraction 2 Matrix Row and column must be same\nInputs for Row and column\n");
  40.         scanf("%lld %lld",&n,&m);
  41.         puts("Inputs for First matrix");
  42.         for(i=0; i<n; i++)
  43.             for(j=0; j<m; j++)
  44.                 scanf("%lld",&a[i][j]);
  45.         puts("Input for second matrix");
  46.         for(i=0; i<n; i++)
  47.             for(j=0; j<m; j++)
  48.                 scanf("%lld",&b[i][j]);
  49.         for(i=0; i<n; i++)
  50.             for(j=0; j<m; j++)
  51.                 c[i][j]=a[i][j]-b[i][j];
  52.         printf("\nOutput\n");
  53.         for(i=0; i<n; i++)
  54.         {
  55.             for(j=0; j<m; j++)
  56.                 printf("%lld ",c[i][j]);
  57.             printf("\n");
  58.         }
  59.     }
  60.     else if(ax=='M' || ax=='m')
  61.     {
  62.         printf("A's Row:    ");
  63.         scanf("%lld",&n);
  64.         printf("A's Column/B's Row:    ");
  65.         scanf("%lld",&m);
  66.         printf("B's Column:    ");
  67.         scanf("%lld",&q);
  68.         puts("Input for First matrix");
  69.         for(i=0; i<n; i++)
  70.             for(j=0; j<m; j++)
  71.                 scanf("%lld",&a[i][j]);
  72.         puts("Input for Second matrix");
  73.         for(i=0; i<m; i++)
  74.             for(j=0; j<q; j++)
  75.                 scanf("%lld",&b[i][j]);
  76.         for(i=0; i<n; i++)
  77.             for(j=0; j<q; j++)
  78.                 for(p=0; p<m; p++)
  79.                     c[i][j]+=a[i][p] * b[p][j];
  80.         printf("\nOutput\n");
  81.         for(i=0; i<n; i++)
  82.         {
  83.             for(j=0; j<q; j++)
  84.             {
  85.                 printf("%lld ",c[i][j]);
  86.             }
  87.             printf("\n");
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement