/*
Title: Represent matrix using two dimensional arrays and perform following operations:
  1) Addition using pointers
  2) multiplication without pointers
  3) transpose using pointers
*/

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

void read(int a[10][10],int r,int c)
{
 int i,j;
 printf("\nenter row[%d]: ",r);
 for(i=0;i<r;i++)
  for(j=0;j<c;j++)
   scanf("%d",&a[i][j]);
}

void disp(int a[10][10],int r,int c) 
{
 int i,j;
 for(i=0;i<r;i++)
 {
  printf("\n");
  for(j=0;j<c;j++)
  {
   printf(" %d",a[i][j]);
  }
 }
}

void mul(int a[10][10],int b[10][10],int c[10][10],int r1,int rc,int c2)
{
 int i,j,k;
 for(i=0;i<r1;i++)
 {
  for(j=0;j<c2;j++)
  {
   c[i][j]=0;
   for(k=0;k<rc;k++)
    c[i][j]+=a[i][k]*b[k][j];
  }
 }
}


int **accept(int r,int c)//using pointers
{
 int **p,i,j;
 
 p= (int **)malloc(r*sizeof(int *));
 
 for(i=0;i<r;i++)
  *(p+i)= (int *)malloc(c*sizeof(int ));
 
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   scanf("%d",*(p+i)+j);
  }
 }
 return p;
}

void display(int **p,int r,int c)// using pointers
{
 int i,j;
 for(i=0;i<r;i++)
 {
  printf("\n");
  for(j=0;j<c;j++)
  {
   printf("\t%d",*(*(p+i)+j));
  }
 }
}

int **add(int **p1,int **p2,int r,int c) //using pointers
{
 int i,j,**p3;
 p3= (int **)malloc(r*sizeof(int *));
 
 for(i=0;i<r;i++)
  *(p3+i)= (int *)malloc(c*sizeof(int ));
 
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   *(*(p3+i)+j)=*(*(p1+i)+j) + *(*(p2+i)+j);
  }
 }
 return p3;
}

int **transpose(int **p,int r,int c)//using pointers
{
 int i,j,**p3;
 p3= (int **)malloc(r*sizeof(int *));
 
 for(i=0;i<r;i++)
  *(p3+i)= (int *)malloc(c*sizeof(int ));
 
 for(i=0;i<r;i++)
 {
  for(j=0;j<c;j++)
  {
   *(*(p3+i)+j)=*(*(p+j)+i);
  }
 }
 return p3;
}



int main()
{
 int ch,**p1,**p2,**p3,r1=0,c1=0,r2=0,c2=0;
 int a[10][10],b[10][10],c[10][10];
 do
 {
  system("clear");
  printf("\n\n********MENU***********");
  printf("\n1.Addition(with pointer)\n2.Transpose(with pointer)\n3.Multiplication(without pointer)\n4.Exit\nEnter ur choice: ");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:
    printf("\nEnter rows & cols of matrix P1: ");
    scanf("%d%d",&r1,&c1);
    printf("\nEnter matrix P1:");
    p1=accept(r1,c1);
    printf("\nEnter rows & cols of matrix P2: ");
    scanf("%d%d",&r2,&c2);
    printf("\nEnter matrix P1:");
    p2=accept(r2,c2);
    system("clear");
    printf("\nMatrix P1: ");
    display(p1,r1,c1);
    printf("\n\nMatrix P2: ");
    display(p2,r2,c2);
    
    if(r1==r2 && c1==c2)
    {
     p3=add(p1,p2,r1,c1);
     printf("\nAddition(P1+P2): ");
     display(p3,r1,c1);
    }
    else
     printf("\naddition not possible");
    getchar();
    break;
   case 2:
    printf("\nEnter rows & cols of matrix P1: ");
    scanf("%d%d",&r1,&c1);
    printf("\nEnter matrix P1:");
    p1=accept(r1,c1);
    system("clear");
    printf("\nBefore Transpose: ");
    display(p1,r1,c1);
       
    if(r1==c1)
    {
     p2=transpose(p1,r1,c1);
     printf("\n\nAfter Transpose: ");
     display(p2,r1,c1);
    }
    else
     printf("\nTranspose not possible");
    getchar();
    break;
   case 3:
    printf("\nEnter rows & cols of matrix P1: ");
    scanf("%d%d",&r1,&c1);
    printf("\nEnter matrix P1:");
    read(a,r1,c1);
    printf("\nEnter rows & cols of matrix P2: ");
    scanf("%d%d",&r2,&c2);
    printf("\nEnter matrix P1:");
    read(b,r2,c2);
    system("clear");
    printf("\nMatrix P1: \n");
    disp(a,r1,c1);
    printf("\n\nMatrix P2: \n");
    disp(b,r2,c2);
    if(c1==r2)
    {
     mul(a,b,c,r1,c1,c2);
     printf("\n\nMUL= ");
     disp(c,r1,c2);
    }
    else
     printf("\nMultiplication not possible");
    getchar(); 
    break;
   case 4:
    exit(0);
  }
  getchar();
 }while(ch!=5);
 return 0;
}


//END OF PROGRAM