Skip to content
August 17, 2012 / ukrocks007

C program to do matrix operations using functions and pointers

C program to do matrix operations using functions and pointers

 

#include<stdio.h>

#include<stdlib.h>

void display(int **a,int m ,int n);

void transpose(int **a,int m ,int n);

int** add(int **a,int m1,int n1 ,int **b,int m2,int n2);

int** sub(int **a,int m1,int n1 ,int **b,int m2,int n2);

int** multi(int **a,int m1,int n1 ,int **b,int m2,int n2);

int** create(int m ,int n);

int saddle(int **a,int m,int n);

void** diagonal(int** a,int m1,int n1);

void** lower(int** a,int m1,int n1);

void** upper(int** a,int m1,int n1);

void main()

{

int **a,**b,**c,m1,n1,m2,n2,m3,n3,adddia;
int choice;
printf(“\n\tEnter the size of first matrix\n”);
printf(“\t”);

scanf(“%d”,&m1);

printf(“\t”);

scanf(“%d”,&n1);

a=create(m1,n1);

printf(“\n\tEnter the size of second matrix\n”);

printf(“\t”);

scanf(“%d”,&m2);

printf(“\t”);

scanf(“%d”,&n2);

b=create(m2,n2);

do

{

printf(“\n\tMenu\n\t1.Transpose of a matrix\n\t2.Addition of two matrices\n\t3.Multiplication of two
matrices\n\t4.Subtraction of two matrices.\n\t5.Addition of diagonal elements.\n\t6.Addition of lower triangular elements
\n\t7.Addition of upper triangular elements.\n\t8.Exit”);

printf(“\n\tEnter your choice”);

printf(“\t”);

scanf(“%d”,&choice);

switch(choice)

{

case 1:

if(m1==n1)

{

transpose(a,m1,n1);

printf(“\n\tResult=\n”);

display(a,m1,n1);

}

else

printf(“\n\tNot a square matrix”);

break;

case 2:

if(m1==m2 && n1==n2)

{

c=add(a,m1,n1,b,m2,n2);

printf(“\n\tResult=\n”);

display(c,m1,n1);

}

else

printf(“\n\tCannot be added “);

break;

case 3:

if(n1==m2)

{

c=multi(a,m1,n1,b,m2,n2);

printf(“\n\tResult=\n”);

display(c,m1,n2);

}

else

printf(“\n\tCan not multiply”);

break;

case 4:

if(m1==m2 && n1==n2)

{

c=sub(a,m1,n1,b,m2,n2);

printf(“\n\tResult=\n”);

display(c,m1,n1);

}

else

printf(“\n\tCannot be added\n”);

break;

case 5:

diagonal(a,m1,n1);

case 6:

lower(a,m1,n1);

case 7:

upper(a,m1,n1);

}

}while(choice<8);

return;

}

int ** create(int m ,int n)

{

int i,j;
int **a;

a=(int**)malloc(m*sizeof(int*));

for(i=0;i<m;i++)

*(a+i)=(int*)malloc(n*sizeof(int));

printf(“\n\tEnter the elements\n”);

for(i=0;i<m;i++)

for(j=0;j<n;j++)

{

printf(“\t”);

scanf(“%d”,(*(a+i)+j));

}

return(a);

}

void display(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 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 ** add(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);

}

int** multi(int **a,int m1,int n1 ,int **b,int m2,int n2)

{

int i,j,k,temp;
int **c;

c=(int**)malloc(m1*sizeof(int*));

for(i=0;i<m1;i++)

*(c+i)=(int*)malloc(n2*sizeof(int));

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;

}

}

return(c);

}

int ** sub(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 ** diagonal(int **a,int m1,int n1)

{

int i,j,diaadd;

for(i=0;i<m1;i++)

{

for(j=0;j<n1;j++)

{

if(i==j)

diaadd+=*(*(a+i)+j);

}

}

printf(“\n\tThe addition is:%d”,diaadd);

}

 

void ** lower(int **a,int m1,int n1)

{

int i,j,diaadd;

for(i=0;i<m1-1;i++)

{

for(j=0;j<n1-1;j++)

{

if(i>j)

diaadd+=*(*(a+i)+j);

}

}

printf(“\n\tThe addition is:%d”,diaadd);

}

void ** upper(int **a,int m1,int n1)

{

int i,j,diaadd;

for(i=0;i<m1-1;i++)

{

for(j=0;j<n1-1;j++)

{

if(i<j)

diaadd+=*(*(a+i)+j);

}

}

printf(“\n\tThe addition is:%d”,diaadd);

}

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: