Welcome

Hi! This blog is for beginners of c++ and java .You can check and pick codes for your programming assignments. This will also help you to understand the concepts of these programming languages. Its all about programming.

Two-Dimensional-Arrays to input two m x n Matrices

Tuesday, June 7, 2011

#include<iostream.h>

int main()
{
    int i,j,choice;
   int mat1[3][3];
   int mat2[3][3];
   int sum[3][3];

    cout<<"Enter elements for Matrices 1:" <<endl;
      for(i=0;i<=(3-1);i++)
      {
          for(j=0;j<=(3-1);j++)
          {
             cin>>mat1[i][j];
          }
      }

    cout<<"Enter elements for Matrices 2:" <<endl;
      for(i=0;i<=(3-1);i++)
      {
          for(j=0;j<=(3-1);j++)
          {
             cin>>mat2[i][j];
          }
      }

    cout<<"What do you want to do ? \n\n";
    cout<<"1.Addition\n2.Multiplication\n";
    cin>>choice;

    switch(choice)
      {
        case 1:

        for(i=0;i<=(3-1);i++)
        {
          for(j=0;j<=(3-1);j++)
          {
             sum[i][j]=mat1[i][j]+mat2[i][j];
          }
        }

        break;

        case 2:

        for(i=0;i<=(3-1);i++)
        {
          for(j=0;j<=(3-1);j++)
          {
             sum[i][j]=mat1[i][j]*mat2[i][j];
          }
        }

        break;
      }


    for(i=0;i<=(3-1);i++)
    {
      for(j=0;j<=(3-1);j++)
      {
         cout<<"\n"<<sum[i][j];
      }
    }
    cout<<endl;

return 0;
}