C program for Matrix Operations | C codechamp

C program for Matrix Operations | C codechamp

486
3
SHARE

C program for Operations : Hey friends, welcome back! Let us add another generalized code to our portal for Matrix Operations. I have written a that asks user several choices related to Matrix operations like Transpose of Matrix, Addition of two Matrices, Multiplication of two Matrices and finding a saddle point in an MxN matrix. So lets see how to write . But first of all lets see what all functions used in program stands for…

Functions used in C program for Matrix operations :

int** create(int m ,int n); /* Creating 2 dimensional matrix*/

void create1(int a[][10],int m , int n);/* Creating MxN matrix*/

void print(int **a,int m ,int n);/* Printing 2 dimensional matrix */

void print1(int a[][10], int m , int n);/* Printing MxN matrix*/

void transpose(int **a,int m ,int n);/* Transpose of a matrix*/

int** addmat(int **a,int m1,int n1 ,int **b,int m2,int n2); /* Adding two matrices */

void  multmat(int a[][10],int m1,int n1 ,int b[][10],int m2,int n2,int c[][10]);/*Multiplying two matrices */

int  saddle(int a[][10],int m,int n); /*Calculating saddle point in a matrix */

Let us see C program for Matrix operations which takes choice from user and then input related to choice selected and displays output.

C program for Matrix Operations :

#include<stdio.h>
#include<conio.h>
int** create(int m ,int n);
void create1(int a[][10],int m , int n);
void print(int **a,int m ,int n);
void print1(int a[][10], int m , int n);
void transpose(int **a,int m ,int n);
int** addmat(int **a,int m1,int n1 ,int **b,int m2,int n2);
void  multmat(int a[][10],int m1,int n1 ,int b[][10],int m2,int n2,int c[][10]);
/*An m x n matrix is said to have a saddle point if some entry a[i][j] is
  smallest value in row i and the largest value of column j */
int  saddle(int a[][10],int m,int n);
int main()
  { 
    int **a,**b,**c,m1,n1,m2,n2,m3,n3;
    int a1[10][10],b1[10][10],c1[10][10];
    int ch;
    printf("----------------------------------------------------\n");
    printf("-------------Made by C codechamp--------------------\n");
    printf("----------------------------------------------------\n\n");
    printf("\t MATRIX OPERATIONS IN C LANGUAGE\n\n");
    do
      { 
    printf("\n\t1. Transpose of the Matrix");
    printf("\n\t2. Add two matrices");
    printf("\n\t3. Multiply two matrices");
    printf("\n\t4. Saddle point in the matrix");
    printf("\n\t5. Exit out of program");
    printf("\n\nEnter Your Choice : ");
    scanf("%d",&ch);
    switch(ch)
     {
        case 1: printf("\nEnter the size of the matrix : ");
            scanf("%d%d",&m1,&n1);
            a=create(m1,n1);
           if(m1==n1)
             {
              transpose(a,m1,n1);
              printf("\nTranspose Matrix is : \n");
              print(a,m1,n1);
              printf("\n\n\n");
             }
            else
              printf("\nPlease enter a square matrix i.e. NxN matrix\n\n");
            break;
        case 2: printf("\nEnter the size of the 1st matrix : ");
            scanf("%d%d",&m1,&n1);
            a=create(m1,n1);
            printf("\n\nEnter the size of the 2nd matrix : ");
            scanf("%d%d",&m2,&n2);
            b=create(m2,n2);
            if(m1==m2 & n1==n2)
              { c=addmat(a,m1,n1,b,m2,n2);
            printf("\nAdd Matrix is : \n");
            print(c,m1,n1);
            printf("\n\n\n");
              }
            else
              printf("\nPlease correct the size of Matrices! Matrices cannot be added.\n\n");
            break;
        case 3: printf("\nEnter the size of the 1st matrix : ");
            scanf("%d%d",&m1,&n1);
            create1(a1,m1,n1);
            printf("\n\nEnter the size of the 2nd matrix : ");
            scanf("%d%d",&m2,&n2);
            create1(b1,m2,n2);
            if(n1==m2)
              { multmat(a1,m1,n1,b1,m2,n2,c1);
            printf("\nMultiplication of two Matrix is : \n");
            print1(c1,m1,n2);
            printf("\n\n\n");
              }
            else
              printf("\n\nMatrices cannot be multiplied! Please correct the sizes.\n\n");
            break;
        case 4: printf("\nEnter the size of the 1st matrix : ");
            scanf("%d%d",&m1,&n1);
            create1(a1,m1,n1);
            saddle(a1,m1,n1);
            printf("\n\n\n");
            break;
    }
      }while(ch!=5);
  }

int **  create(int m ,int n)
       { int i,j; int **a;
     /*creating a matrix */
       a=(int**)malloc(m*sizeof(int*));
       for(i=0;i<m;i++)
         *(a+i)=(int*)malloc(n*sizeof(int));
       printf("\nEnter the data : \n");
     for(i=0;i<m;i++)
       for(j=0;j<n;j++)
         scanf("%d",(*(a+i)+j));
     return(a);
       }

void create1(int a[][10],int m , int n)
       { int i,j;
     /*creating a matrix */
       printf("\nEnter the data : \n");
     for(i=0;i<m;i++)
       for(j=0;j<n;j++)
         scanf("%d",&a[i][j]);
       }

void print(int **a,int m ,int n)
      { int i,j;
     for(i=0;i<m;i++)
       { printf("\n");
         for(j=0;j<n;j++)
           printf("%5d",*(*(a+i)+j));
       }
       }
void print1(int a[][10],int m ,int n)
      { int i,j;
      for(i=0;i<m;i++)
         {
        printf("\n");
        for(j=0;j<n;j++)
             printf("%5d",a[i][j]);
         }
       }

void transpose(int **a,int m ,int n)
    {   int i,j,temp;
        if(m==n)
          {  for(i=1;i<m;i++)
           for(j=0;j<i;j++)
              {
            temp=*(*(a+i)+j);
            *(*(a+i)+j)=*(*(a+j)+i);
            *(*(a+j)+i)=temp;
              }
          }
     }

int ** addmat(int **a,int m1,int n1 ,int **b,int m2,int n2)
      { int i,j; int **c;
      c=(int**)malloc(m1*sizeof(int*));
       for(i=0;i<m1;i++)
         *(c+i)=(int*)malloc(n1*sizeof(int));
      if(m1==m2 && n1==n2)
        for(i=0;i<m1;i++)
          for(j=0;j<n1;j++)
        *(*(c+i)+j)=*(*(a+i)+j) + *(*(b+i)+j);
     return(c);
     }

void multmat(int a[][10],int m1,int n1 ,int b[][10],int m2,int n2,int c[][10])
       { int i,j,k,temp;
     if(n1==m2)
       {  for(i=0;i<m1;i++)
          for(j=0;j<n2;j++)
           {
            temp=0;
            for(k=0;k<n1;k++)
                temp=temp + a[i][k] * b[k][j];
            c[i][j]=temp;
           }
       }
      }

int  saddle(int a[][10],int m,int n)
 { int i,j,small,large,col_of_small,row_of_large;
    for(i=0;i<m;i++) /* find saddle point row wise */
      { small=a[i][0];
    col_of_small=0;
    for(j=1;j<n;j++)
      if(a[i][j] < small)
        {
          small=a[i][j];
          col_of_small=j;
        }
    /* find the largest element in "col_of_small"*/
    large= a[0][col_of_small];
    row_of_large=0;
    for(j=1;j<m;j++)
      if(a[j][col_of_small]>large)
        { large=a[j][col_of_small];
          row_of_large=j;
        }
     if(i==row_of_large)
       {
        printf("\nSaddle point exist at (%d,%d) with value as %d",i,col_of_small,a[i][col_of_small]);
        return(1);
       }
       }
    printf("\nSaddle point does not exist\n\n");
    return(0);
}

We hope that you all have enjoyed the C program for Matrix operations in . If you have any issues or problems with the above, ask us in form of comments.

Uniqueness is the key ! And Quality is what we deliver.

3 COMMENTS

LEAVE A REPLY