Code Scrappers


Matrix Operations C Program


                  #include<stdio.h>
                  #include<stdlib.h>

                  void add();

                  void sub();

                  void multiply();

                  int a[8][8], b[8][8], c[8][8];

                  int i, j , k, m, n, p, q, choice;

                  void main()
                  {
                          printf("enter size of 1st matrix\n");

                          scanf("%d %d",&m,&n);

                          printf("enter elements of 1st matrix \n");

                          for(i=0; i<m; i++)
                          {
                                  for(j=0; j<n; j++)
                                  {
                                          scanf("%d", &a[i][j]);
                                  }
                          }
                          printf("enter size of 2nd matrix\n");

                          scanf("%d %d", &p, &q);

                          printf("enter elements of 2nd matrix \n");

                          for(i=0; i<p; i++)
                          {
                                  for(j=0; j<q; j++)
                                  {
                                          scanf("%d", &b[i][j]);
                                  }
                          }

                          printf("Matrix Operations: \n 1. Addition \n 2. Subtraction \n 3. Multiplication \n");

                          printf("Enter your choice : ");

                          scanf("%d",&choice);

                          switch(choice)
                          {
                                  case 1 : add();
                                          break;

                                  case 2 : sub();
                                          break;

                                  case 3 : multiply();
                                          break;
                          }
                  }

                  void add()
                  {
                        if(m!=p || n!=q)

                        printf("\nMatrix addition not possible.");

                        else
                        {
                              for(i=0; i<m; i++)
                              {
                                    for(j=0; j<n; j++)
                                    {
                                          c[i][j] = a[i][j] + b[i][j];
                                    }
                              }
                              printf("\nMatrix addition is : \n");

                              for(i=0; i<m; i++)
                              {
                                    for(j=0; j<n; j++)
                                    {
                                        printf("%d ",c[i][j]);
                                    }
                                    printf("\n");
                              }
                        }
                  }

                  void sub()
                  {
                      if(m!=p || n!=q)
                      {
                          printf("Matrix subtraction not possible.");
                      }
                      else
                      {
                          for(i=0; i<m; i++)
                          {
                                for(j=0; j<n; j++)
                                {
                                      c[i][j]=a[i][j]-b[i][j];
                                }
                          }

                          printf("Matrix subtraction is : \n");

                          for(i=0; i<m; i++)
                          {
                                for(j=0; j<n; j++)
                                {
                                     printf("%d ",c[i][j]);
                                }
                                printf("\n");
                          }
                      }

                  }

                  void multiply()

                  {
                      if(n!=p)
                      {
                            printf("Matrix multiplication not possible.");
                      }

                      else
                      {
                            for(i=0; i<m; i++)
                            {
                                  for(j=0; j<q; j++)
                                  {
                                        c[i][j]=0;

                                        for(k=0; k<p; k++)
                                        {
                                              c[i][j]=a[i][k]*b[k][j]+c[i][j];
                                        }
                                  }
                            }
                            printf("Matrix multiplication is : \n");

                            for(i=0; i<m; i++)
                            {
                                  for(j=0; j<q; j++)
                                  {
                                        printf("%d ",c[i][j]);
                                  }

                                  printf("\n");
                            }
                      }
                 }

              



Output

      enter size of 1st matrix
      2
      2

      enter elements 1st matrix
      1
      2
      3
      4

      enter size of 2nd matrix
      2
      2

      enter elements of 2nd matrix
      1
      1
      1
      1

      Matrix Operations:
       1. Addition
       2. Subtraction
       3. Multiplication

      Enter your choice : 1

      Matrix addition is :

      2 3
      4 5