Basic Matrix operations using 2D integer arrays

Program Description

Implements basic matrix operations like addition,subtraction,multiplication transpose and saddle point using 2D integer arrays.The program prompts the user for matrix dimensions(assumes 10X10 to be max size for this program) and takes the inputs from the user. No error checking is performed while taking user input. Assumes all inputs all valid

Program
/***************************************
* code.cheraus.com 
* 
* Basic Matrix operations using 2D integer arrays
*  
**************************************/

#include<stdio.h>

//Function prototypes
void add(int (*mat1)[],int (*mat2)[],int row1,int column1);
void subtract(int (*mat1)[],int (*mat2)[],int row1,int column1);
void multiply(int (*mat1)[],int (*mat2)[],int row1,int column1,int row2,int column2);
void saddle(int (*mat1)[],int (*mat2)[],int row1,int column1,int row2,int column2);
void transpose(int (*mat1)[],int (*mat2)[],int row1,int column1,int row2,int column2);


int main()

{
int row1=0;
int column1=0;
int row2=0;
int column2=0;
int i,j;
int num=0;
int choice=0;

//Initialization of 10X10 matrices(assuming input not greater than this size)
int mat1[10][10];
int mat2[10][10];

//Ask user for dimension of matrices
printf("How many rows in the first matrix? ");
scanf("%d",&row1);

printf("How many columns in the first matrix? ");
scanf("%d",&column1);

printf("How many rows in the second matrix? ");
scanf("%d",&row2);

printf("How many columns in second matrix? ");
scanf("%d",&column2);


//Writing the elements only upto the size user specified
printf("Enter the elements of first matrix A:  ");

 for(i=0;i<row1;i++)
    {
        for(j=0;j<column1;j++)
          {
             printf("A[%d][%d]= ",i,j);
             scanf("%d",&mat1[i][j]);
          }

    }
printf("Enter the elements of the second matrix");
for(i=0;i<row2;i++)
    {
         for(j=0;j<column2;j++)
             {
               printf("B[%d][%d]= ",i,j);
               scanf("%d",&mat2[i][j]);
             }
    }

//Makes the program menu driven
do
{
printf("\n\nMENU\n\n");
printf("1.Add\n2.Subtract\n3.Multiply\n4.Saddle point\n5.Transpose\n6.Exit\n");
printf("Your choice: ");
scanf("%d",&choice);    
switch(choice)
{
case 1:
//checking for valid dimensions to perform addition
if((row1!=row2) && (column1!=column2))
{  printf("The addition is not possible. The two matrices must have equal dimensions\n");
   break;
   }
else
{
add(mat1,mat2,row1,column1);
break;
}

case 2:

if((row1!=row2) && (column1!=column2))
{
 //checking for valid dimensions for subtraction               
 printf("Subtraction is not possible. The two matrices must have equal dimensions\n");
 break;
 }
 else
 {
   subtract(mat1,mat2,row1,column1);
   break;
   }
   
case 3:
//checking for valid dimensions for multiplication
if(row1!=column2)
{
  printf("Multiplication is not possible.");
  break;
  }
  
  else
  {
  multiply(mat1,mat2,row1,column1,row2,column2);
  break;
  }
  
  case 4:
  saddle(mat1,mat2,row1,column1,row2,column2);
  break;
  
  case 5:
  transpose(mat1,mat2,row1,column1,row2,column2);
  break;

  case 6: return 0;   
  default :
  printf("Improper choice. Try again\n ");
  break;
  }   

printf("Do you wish to continue?\nPress 1 to continue or else any key to exit\n");
printf("Your choice: ");
scanf("%d",&num);
}while(num==1);

return 0;

}

/* FUNCTION DEFINITION FOR ADDITION

Function: add() This functions adds the two integer matrices if addition is possible.
          The error checking for invalid addtion is done before calling this function.
Inputs: Takes the following argumets:
        1) Matrix 1
        2) Matrix 2
        3) Number of rows
        4) Number of columns
Return: The function does not return anything. The resultant matrix is displayed immediately
        after addition          


*/
void add(int (*mat1)[10],int (*mat2)[10],int row1,int column1)
{
	int mat3[row1][column1],i,j;

	for(i=0;i<row1;i++)
		{
			for(j=0;j<column1;j++)
			{
				mat3[i][j]=mat1[i][j]+mat2[i][j];
			}
		}
	printf("\n\nAddition of matrices is : \n\n");
	for(i=0;i<row1;i++)
	{
		for(j=0;j<column1;j++)
		{
			printf("\t%d",mat3[i][j]);
		}
		printf("\n");
	}
	
}
	
/* FUNCTION DEFINITION FOR SUBTRACTION 

Function: subtract() This functions subtracts the two integer matrices if subtraction is possible.
          The error checking for invalid subtraction is done before calling this function.
Inputs: Takes the following argumets:
        1) Matrix 1
        2) Matrix 2
        3) Number of rows
        4) Number of columns
Return: The function does not return anything. The resultant matrix is displayed immediately
        after subtraction          


*/
void subtract(int (*mat1)[10],int (*mat2)[10],int row1,int column1)
{
	int mat3[row1][column1],i,j;

	for(i=0;i<row1;i++)
		{
			for(j=0;j<column1;j++)
			{
				mat3[i][j]=mat1[i][j]-mat2[i][j];
			}
		}
	printf("\n\nDifference of matrices is : \n\n");
	for(i=0;i<row1;i++)
	{
		for(j=0;j<column1;j++)
		{
			printf("\t%d",mat3[i][j]);
		}
		printf("\n");
	}
	
}



/* FUNCTION DEFINITION FOR MULTIPLICATION 

Function: multiply() This functions multiplies the two integer matrices if multiplication is possible.
          The error checking for invalid multiplication is done before calling this function.
Inputs: Takes the following argumets:
        1) Matrix 1
        2) Matrix 2
        3) Number of rows in first matrix
        4) Number of columns in first matrix
        5) NUmber of rows in second matrix
        6) Number of columns in second matrix
Return: The function does not return anything. The resultant matrix is displayed immediately
        after multiplication          


*/

void multiply(int (*mat1)[10],int (*mat2)[10],int row1,int column1,int row2,int column2)
{
	int mat3[row1][column2],i,j,k;

	
		for(i=0;i<row1;i++)
		{
			for(j=0;j<column2;j++)
			{
				mat3[i][j]=0;
				
				for(k=0;k<column1;k++)
				{
					mat3[i][j]+=mat1[i][k]*mat2[k][j];
				}

			}
		}


        //printing the resultant multiplication matrix
	printf("\n\nMultiplition of matrices is : \n\n");
	for(i=0;i<row1;i++)
	{
		for(j=0;j<column1;j++)
		{
			printf("\t%d",mat3[i][j]);
		}
		printf("\n");
	}	
}



/* FUNCTION DEFINITION FOR  SADDLE POINT


  A saddle point in a matrix is an element a[i][j] such that it has the minimum value in the 
  row and maximum in a column.

Function: saddle() This functions finds the saddle point in the given matrix.It takes both the 
          matrices as argument and calulates the saddle point of the required matrix as per the
          users choice. 
         
Inputs: Takes the following argumets:
        1) Matrix 1
        2) Matrix 2
        3) Number of rows in first matrix
        4) Number of columns in first matrix
        5) NUmber of rows in second matrix
        6) Number of columns in second matrix
Return: The function does not return anything. The saddle point of the respective matrix
        is printed immediately after calulation. 

*/

void saddle(int (*mat1)[10],int (*mat2)[10],int row1,int column1,int row2,int column2)
{

	int i,j,k,min,max,col,ch;

        
	printf("\n\n1:For Saddle in Matrix A\n2:For Saddle in Matrix B\n");
	scanf("%d",&ch);
	switch(ch)
	{

         //calculating saddle point for matrix 1

	case 1: 
	for(i=0;i<row1;i++)
	{
		min=mat1[i][0];
		for(j=0;j<column1;j++)
		{
			if(mat1[i][j]<=min)
			{
				min=mat1[i][j];
				col=j;
			}
		}
		max=mat1[0][col];
		for(k=0;k<row1;k++)
		{
			if(mat1[k][col]>=max)
			{
				max=mat1[k][col];
			}
		}
		if(min==max)
		{
			printf("\n\nIn Matrix A (%d,%d) is saddle point having value %d.\n",i+1,col+1,max);
		}
	}
	break;


        //calculating saddle point for matrix 2

	case 2:
	for(i=0;i<row2;i++)
	{
		min=mat2[i][0];
		for(j=0;j<column2;j++)
		{
			if(mat2[i][j]<=min)
			{
				min=mat2[i][j];
				col=j;
			}
		}
		max=mat2[0][col];
		for(k=0;k<row2;k++)
		{
			if(mat2[k][col]>=max)
			{
				max=mat2[k][col];
			}
		}
		if(min==max)
		{
			printf("\n\nIn Matrix B (%d,%d) is saddle point having value %d.\n",i+1,col+1,max);
		}
	}
 }

}


/* FUNCTION DEFINITION FOR FINDING TRANSPOSE 


Function: transpose() This functions finds the transpose of the given matrix. The function
          takes both the matrices as input and then finds the transpose of the required matrix
          as per the users choice.
         
Inputs: Takes the following argumets:
        1) Matrix 1
        2) Matrix 2
        3) Number of rows in first matrix
        4) Number of columns in first matrix
        5) NUmber of rows in second matrix
        6) Number of columns in second matrix
Return: The function does not return anything. The tranpose  of the respective matrix
        is printed immediately after calulation. 

*/

void transpose(int (*mat1)[10],int (*mat2)[10],int row1,int column1,int row2,int column2)
{
	int i,j,ch,mat3[5][5];
	printf("\n\nFor \nTranspose of A:1\nTranspose of B:2\n");
	scanf("%d",&ch);
	
	switch(ch)
	{

        //finding transpose of matrix 1
	case 1:
	{
		for(i=0;i<row1;i++)
		{
			for(j=0;j<column1;j++)
			{
				mat3[j][i]=mat1[i][j];
			}
		}

                //printing transpose of matrix 1
		printf("\n\nTranspose of Matrix A is :\n");

		for(i=0;i<column1;i++)
		{
			for(j=0;j<row1;j++)
			{
				printf("\t%d",mat3[i][j]);
			}
			printf("\n");
		}
	}
	break;

        //finding transpose of matrix 2
	case 2:
	{
		for(i=0;i<row2;i++)
		{
			for(j=0;j<column2;j++)
			{
				mat3[j][i]=mat2[i][j];
			}
		}

                //printing transpose of matrix 2
		printf("\n\nTranspose of Matrix B is :\n");

		for(i=0;i<column2;i++)
		{
			for(j=0;j<row2;j++)
			{
				printf("\t%d",mat3[i][j]);
			}
			printf("\n");
		}
        }
   }
}

Output

How many rows in the first matrix? 3
How many columns in the first matrix? 3
How many rows in the second matrix? 3
How many columns in second matrix? 3
Enter the elements of first matrix A: A[0][0]= 1
A[0][1]= 2
A[0][2]= 3
A[1][0]= 4
A[1][1]= 5 A[1][2]= 6
A[2][0]= 7
A[2][1]= 8
A[2][2]= 9
Enter the elements of the second matrixB[0][0]= 12
B[0][1]= 34
B[0][2]= 56
B[1][0]= 89
B[1][1]= 9
B[1][2]= 1
B[2][0]= 2
B[2][1]= 6
B[2][2]= 8

MENU
1.Add
2.Subtract
3.Multiply
4.Saddle point
5.Transpose
6.Exit
Your choice: 1
Addition of matrices is :

13 36 59
93 14 7
9 14 17
Do you wish to continue?
Press 1 to continue or else any key to exit
Your choice: 1
MENU
1.Add
2.Subtract
3.Multiply
4.Saddle point
5.Transpose
6.Exit
Your choice: 2
Difference of matrices is :
-11 -32 -53
-85 -4 5
5 2 1
Do you wish to continue?
Press 1 to continue or else any key to exit
Your choice: 1
MENU
1.Add
2.Subtract
3.Multiply
4.Saddle point
5.Transpose
6.Exit
Your choice: 3
Multiplition of matrices is :

196 70 82
505 217 277
814 364 472
Do you wish to continue?
Press 1 to continue or else any key to exit
Your choice: 1
MENU
1.Add
2.Subtract
3.Multiply
4.Saddle point
5.Transpose
6.Exit
Your choice: 4
1:For Saddle in Matrix A
2:For Saddle in Matrix B
1
In Matrix A (3,1) is saddle point having value 7.
Do you wish to continue?
Press 1 to continue or else any key to exit
Your choice: 1
MENU
1.Add
2.Subtract
3.Multiply
4.Saddle point
5.Transpose
6.Exit
Your choice: 4
1:For Saddle in Matrix A
2:For Saddle in Matrix B
2
Do you wish to continue?
Press 1 to continue or else any key to exit
Your choice: 1
MENU
1.Add
2.Subtract
3.Multiply
4.Saddle point
5.Transpose
6.Exit
Your choice: 5
For
Transpose of A:1
Transpose of B:2
1
Transpose of Matrix A is :
1 4 7
2 5 8
3 6 9
Do you wish to continue?
Press 1 to continue or else any key to exit
Your choice: 1
MENU
1.Add
2.Subtract
3.Multiply
4.Saddle point
5.Transpose
6.Exit
Your choice: 5
For
Transpose of A:1
Transpose of B:2
2
Transpose of Matrix B is :
12 89 2
34 9 6
56 1 8
Do you wish to continue?
Press 1 to continue or else any key to exit
Your choice: 1
MENU
1.Add
2.Subtract
3.Multiply
4.Saddle point
5.Transpose
6.Exit
Your choice: 6

Comments
comments powered by Disqus