#include <stdio.h>

int matadd();
int matmul();
int transpose();
int scalar();
int matequal();
int deter();

int main()
{
  int choice;

  while (choice!=7)
    {
      printf("\n");
      printf("Enter your choice.\n");
      printf("1 - Transpose\n");
      printf("2 - Add, Subtract\n");
      printf("3 - Multiply\n");
      printf("4 - Multiplication by Scalar\n");
      printf("5 - Equality of Matrix\n");
      printf("6 - Determinant\n");
      printf("7 - Exit\n");
      scanf("%i",&choice);

      switch(choice)
	{
	case 1: 
	  transpose();
	  break;
        case 2:
	  matadd();
	  break;
	case 3:
	  matmul();
	  break;
        case 4:
	  scalar();
	  break;
	case 5:
	  matequal();
	  break;
	case 6:
	  deter();
	  break;
        case 7:
	  return(1);
	  break;
        default:
	  printf("Enter properly");

	} // switch
    } // while

  return(0);

}

int matadd()                   // Matrix Addition
{

  int r3,c3;
  int r1,r2,c1,c2;
  int i,j,k,flag;

  printf("Enter the number of rows of the first matix.");
  scanf("%i",&r1);
  printf("Enter the number of columns of the first matrix.");
  scanf("%i",&c1);
  printf("\n\nEnter the number of rows of the second matix.");
  scanf("%i",&r2);
  printf("Enter the number of columns of the second matrix.");
  scanf("%i",&c2);


  r3=r1;
  c3=c2;
  
  
  if(r1!=r2)
    {
      printf("\nCannot Add/Sub\n");
      return(1);
    }
  if(c1!=c2)
    {
      printf("\nCannot Add/Sub\n");
      return(1);
    }

  int mat1[r1][c1];
  int mat2[r2][c2];

 printf("First Matrix\n");
  for(i=0;i<r1;i++)
  {
      for(j=0;j<c1;j++)
	{
	  printf("Enter %i row, %i column element: ",i,j);
	  scanf("%i",&mat1[i][j]);
	}
    }

  for(i=0;i<r1;i++)    //display first matrix
   {
     for(j=0;j<c1;j++)
       {
	 printf("%i\t",mat1[i][j]);
       }
     printf("\n");
   }


 printf("\nSecond Matrix\n");
 for(i=0;i<r2;i++)
   {
     for(j=0;j<c2;j++)
       {
	 printf("Enter %i row, %i column element: ",i,j);
	 scanf("%i",&mat2[i][j]);
       }
   }

 for(i=0;i<r2;i++)         // display  second matrix
   {
     for(j=0;j<c2;j++)
       {
         printf("%i\t",mat2[i][j]);
       }
     printf("\n");
   }

 int mat3[r3][c3];
 
 for(i=0;i<c1;i++)     // Add operation
   {
     for(j=0;j<r1;j++)
       {
	 mat3[i][j]=mat1[i][j]+mat2[i][j];
       }
   }

 printf("\nAddtion");
 for(i=0;i<r3;i++)     // display
   {
     for(j=0;j<c3;j++)
       {
         printf("%i\t",mat3[i][j]);
       }
     printf("\n");
   }

 for(i=0;i<c1;i++)   // Sub Operation
   {
     for(j=0;j<r1;j++)
       {
	 mat3[i][j]=mat1[i][j]-mat2[i][j];
       }
   }

 printf("\nSubtraction");
 for(i=0;i<r3;i++)    //display
   {
     for(j=0;j<c3;j++)
       {
         printf("%i\t",mat3[i][j]);
       }
     printf("\n");
   }
 


} // matadd

int matmul()          // matrix multiplication
{

  int r3,c3;
  int r1,r2,c1,c2;
  int i,j,k,flag;
  int m,l,n,o;

  printf("Enter the number of rows of the first matix.");
  scanf("%i",&m);
  printf("Enter the number of columns of the first matrix.");
  scanf("%i",&l);
  printf("\nEnter the number of rows of the second matix.");
  scanf("%i",&o);
  printf("Enter the number of columns of the second matrix.");
  scanf("%i",&n);

  r3=m;
  c3=n;
  flag=0;
  
  if(l!=o)
    {
      printf("\nCannot multiply\n");
      return(1);
    }

  int mat1[m][l];
  int mat2[o][n];

 printf("First Matrix\n");
  for(i=0;i<m;i++)
  {
      for(j=0;j<l;j++)
	{
	  printf("Enter %i row, %i column element: ",i,j);
	  scanf("%i",&mat1[i][j]);
	}
    }

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


 printf("\nSecond Matrix\n");
 for(i=0;i<o;i++)
   {
     for(j=0;j<n;j++)
       {
	 printf("Enter %i row, %i column element: ",i,j);
	 scanf("%i",&mat2[i][j]);
       }
   }

 for(i=0;i<o;i++)
   {
     for(j=0;j<n;j++)
       {
         printf("%i\t",mat2[i][j]);
       }
     printf("\n");
   }
  
 printf("\nMultiply\n");
 
 int mat3[r3][c3], sum;

 
 for(i=0;i<m;i++)
   {
     for(j=0;j<n;j++)
       {
	 sum=0;
	 for(k=0;k<o;k++)
	   {
	     sum=sum+(mat1[i][k]*mat2[k][j]);
	     mat3[i][j]=sum;
	   }
       }
   }

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

} //matmul



int transpose()           // matrix transpose
{
  int r1,c1,i,j,m,n;

  printf("Enter the number of rows of the matix.");
  scanf("%i",&r1);
  printf("Enter the number of columns of the matrix.");
  scanf("%i",&c1);

  int mat1[r1][c1];


  for(i=0;i<r1;i++)          // Enter the matrix
  {
      for(j=0;j<c1;j++)
	{
	  printf("Enter %i row, %i column element: ",i,j);
	  scanf("%i",&mat1[i][j]);
	}
    }

    
  for(i=0;i<r1;i++)       // Display the matrix
    {
      for(j=0;j<c1;j++)
      {
         printf("%i\t",mat1[i][j]);
      }
      printf("\n");
    }
    m=0;
    n=0;

    int mat2[c1][r1];
    
    for(i=0;i<r1;i++)    // Transpose operation
    {
      for(j=0;j<c1;j++)
	{
	  mat2[m][n]=mat1[i][j];
	  m++;
	}
      m=0;
      n++;
    }

    printf("\n");
    for(i=0;i<c1;i++)      // Display the transpose
    {
       for(j=0;j<r1;j++)
       {
          printf("%i\t",mat2[i][j]);
       }
    printf("\n");
    }

   
} //transpose



int deter()  // Determinant of a matrix
{
  int mat1[3][3], i,j, result;
  printf("\nEnter a 3x3 matrix\n");
  for(i=0;i<3;i++)
    {
      for(j=0;j<3;j++)
	{
	printf("Enter %i row, %i column.\n",i,j);
	scanf("%i",&mat1[i][j]);
	}
    }

  

  result=(mat1[0][0]*(mat1[1][1]*mat1[2][2] - mat1[2][1]*mat1[1][2]))-(mat1[0][1]*(mat1[1][0]*mat1[2][2] - mat1[2][0] * mat1[1][2])) + (mat1[0][2]*( mat1[1][0]*mat1[2][1] - mat1[2][0]*mat1[1][1]));
  //  result=result*mat1[0][0];

  printf("\n%i\n",result);

  

} // End Determinant
  
int scalar()                       // Scalar multiply
{
  int r1,c1,i,j,m,n, scalar;

  printf("Enter the number of rows of the matix.");
  scanf("%i",&r1);
  printf("Enter the number of columns of the matrix.");
  scanf("%i",&c1);

  int mat1[r1][c1];


  for(i=0;i<r1;i++)          // Enter the matrix
    {
      for(j=0;j<c1;j++)
	{
	  printf("Enter %i row, %i column element: ",i,j);
	  scanf("%i",&mat1[i][j]);
	}
    }
  
  
  for(i=0;i<r1;i++)       // Display the matrix
    {
      for(j=0;j<c1;j++)
	{
	  printf("%i\t",mat1[i][j]);
	}
      printf("\n");
    }
  

    printf("\nEnter the scalar\n");
    scanf("%i",&scalar);
    
    int mat2[c1][r1];
    
    for(i=0;i<r1;i++)
      {
	for(j=0;j<c1;j++)
	  mat2[i][j]=mat1[i][j]*scalar;
      }
    
    for(i=0;i<r1;i++)       // Display the matrix
      {
	for(j=0;j<c1;j++)
	  {
	    printf("%i\t",mat2[i][j]);
	  }
	printf("\n");
      }
    printf("\n");
   
}

int matequal()  // Equality of matrix
{
  
	int r3,c3;
  int r1,r2,c1,c2;
  int i,j,k,flag;

  printf("Enter the number of rows of the first matix.");
  scanf("%i",&r1);
  printf("Enter the number of columns of the first matrix.");
  scanf("%i",&c1);
  printf("\n\nEnter the number of rows of the second matix.");
  scanf("%i",&r2);
  printf("Enter the number of columns of the second matrix.");
  scanf("%i",&c2);


  r3=r1;
  c3=c2;
  
  
  if(r1!=r2)
    {
      printf("\nMatrices are not equal\n");
      return(1);
    }
  if(c1!=c2)
    {
      printf("\nMatrics are not equal\n");
      return(1);
    }

  int mat1[r1][c1];
  int mat2[r2][c2];

 printf("First Matrix\n");
  for(i=0;i<r1;i++)
  {
      for(j=0;j<c1;j++)
	{
	  printf("Enter %i row, %i column element: ",i,j);
	  scanf("%i",&mat1[i][j]);
	}
    }

  for(i=0;i<r1;i++)    //display first matrix
   {
     for(j=0;j<c1;j++)
       {
	 printf("%i\t",mat1[i][j]);
       }
     printf("\n");
   }
  
  
  printf("\nSecond Matrix\n");
  for(i=0;i<r2;i++)
    {
      for(j=0;j<c2;j++)
	{
	  printf("Enter %i row, %i column element: ",i,j);
	  scanf("%i",&mat2[i][j]);
	}
    }
  
  for(i=0;i<r2;i++)         // display  second matrix
    {
      for(j=0;j<c2;j++)
	{
	  printf("%i\t",mat2[i][j]);
	}
      printf("\n");
    }
  
  for(i=0;i<r1;i++)
    {
      for(j=0;j<c1;j++)
	if(mat1[i][j]!=mat2[i][j])
	  {
	    printf("\nMatrices are not equal.");
	    return(1);
	  }
    }	

  printf("\nMatrices are equal.\n\n");
  
}

