rabbinur

Practice session (Matrix)

Aug 7th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. (1)
  2.  
  3. Matrix Sum of Rows and Coloumns
  4. #include<iostream.h>
  5. #include<conio.h>
  6.  
  7.  
  8. void sum(int a[][20],int m,int n)
  9. {
  10. int i,j,s=0;
  11. for(i=0;i<m;i++)
  12. {
  13.     s=0;
  14.     for(j=0;j<n;j++)
  15.         s+=a[i][j];
  16.     cout<<"\nSum of"<<i<<"row"<<s;
  17. }
  18. for(i=0;i<m;i++)
  19. {
  20.     s=0;
  21.     for(j=0;j<n;j++)
  22.         s+=a[j][i];
  23.     cout<<"\nSum of"<<i<<"coloumn"<<s;
  24. }
  25. }
  26. void main()
  27. {
  28. int a[10][20],m,n,i,j;
  29. cout<<"Enter the order of matrix";
  30. cin>>m>>n;
  31. cout<<"Enter the Elements";
  32. for(i=0;i<m;i++)
  33. for(j=0;j<n;j++)
  34. cin>>a[i][j];
  35. sum(a,m,n);
  36. getch();
  37. }
  38.  
  39. (2)
  40.  
  41. Program to find sum of diagonals in a matrix
  42. #include<iostream.h>
  43. #include<conio.h>
  44. int sod(int a[10][10],int r,int c)
  45.  {
  46.   int i,j,sum=0;
  47.   for(i=0;i<r;i++)
  48.      {
  49.       for(j=0;j<c;j++)
  50.          {
  51.            if(i==j||i+j==r-1)
  52.            sum+=a[i][j];
  53.          }
  54.       }
  55.        return(sum);
  56.  }
  57. void main()
  58.  {
  59.  clrscr();
  60.  int a[10][10],r,c,i,j;
  61.  cout<<"\n";
  62.  cout<<"enter size of row and column ";
  63.  cout<<"\n";
  64.  cin>>r>>c;
  65.  cout<<"\n";
  66.  cout<<"enter elements";
  67.  for(i=0;i<r;i++)
  68.     {
  69.      cout<<"\n";
  70.      for(j=0;j<c;j++)
  71.         {
  72.          cin>>a[i][j];
  73.         }
  74.     }
  75.  cout<<"\n";
  76.  cout<<"sum is"<< sod(a,r,c);
  77.  getch();
  78.  }
  79.  
  80. (3)
  81.  
  82. Program for Matrix Multiplication
  83. #include<iostream.h>
  84. #include<conio.h>
  85. void mul(int a[10][10],int b[10][10],int c[20][20],int d,int r,int r1,int c1);
  86. void main()
  87. {
  88.  clrscr();
  89.  int a[10][10],b[10][10],c[20][20],r,d,i,j,r1,c1;
  90.  cout<<"\n";
  91.  cout<<"enter size row and column of matix 1";
  92.  cout<<"\n\n";
  93.  cin>>r;
  94.  cout<<"\n";
  95.  cin>>d;
  96.  cout<<"\n";
  97.  cout<<"enter elements";
  98.  cout<<"\n";
  99.  for(i=0;i<r;i++)
  100.     {
  101.      for(j=0;j<d;j++)
  102.         {
  103.           cout<<"\n";
  104.           cin>>a[i][j];
  105.          }
  106.      }
  107.   cout<<"\n";
  108.   cout<<"enter size of row and column of matix 2";
  109.   cout<<"\n\n";
  110.   cin>>r1;
  111.   cout<<"\n";
  112.   cin>>c1;
  113.   cout<<"\n";
  114.   cout<<"enter elements in matrix 2";
  115.   cout<<"\n";
  116.   for(i=0;i<r1;i++)
  117.      {
  118.       for(j=0;j<c1;j++)
  119.          {
  120.           cout<<"\n";
  121.           cin>>b[i][j];
  122.          }
  123.      }
  124.  mul(a,b,c,d,r,r1,c1);
  125. getch();
  126. }
  127. void mul(int a[10][10],int b[10][10],int c[20][20],int d,int r,int r1,int c1)
  128.   {
  129.    int k,i,j;
  130.    if(d!=r1)
  131.      {
  132.       cout<<"multiplication not possible";
  133.      }
  134.    else
  135.     {
  136.      for(i=0;i<r;i++)
  137.         {
  138.  
  139.          for(j=0;j<c1;j++)
  140.             {
  141.              for(k=0;k<r1;k++)
  142.                 {
  143.                  c[i][j]+=a[i][k]*b[k][j];
  144.                 }
  145.              }
  146.           }
  147.      }
  148.   cout<<"\n";
  149.   cout<<"after multiplication"<<"\n\n";
  150.   for(i=0;i<r;i++)
  151.      {
  152.       for(j=0;j<c1;j++)
  153.          {
  154.           cout<<c[i][j]<<"\t";
  155.          }
  156.       }
  157.  }
  158.  
  159. (4)
  160.  
  161. Program to display numbers divisible by 10 in a Matrix
  162. #include<iostream.h>
  163. #include<conio.h>
  164. void disp(int m[5][5]);
  165. void main()
  166. {
  167. clrscr();
  168. int m[5][5],i,j;
  169. cout<<"\n\n";
  170. cout<<"enter elements";
  171. cout<<"\n";
  172. for(i=0;i<3;i++)
  173.    {
  174.     for(j=0;j<3;j++)
  175.        {
  176.         cout<<"\n";
  177.         cin>>m[i][j];
  178.        }
  179.     }
  180. disp(m);
  181. getch();
  182. }
  183. void disp(int m[5][5])
  184. {
  185.  int i,j;
  186.  cout<<"\n\n";
  187.  cout<<"nos. divisible by 10 are";
  188.  cout<<"\n\n";
  189.  for(i=0;i<3;i++)
  190.     {
  191.      for(j=0;j<3;j++)
  192.         {
  193.          if(m[i][j]%10==0)
  194.            {
  195.             cout<<m[i][j]<<"\t";
  196.            }
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment