Tuesday, October 4, 2011

Data Structure Lab Programs - Write a program following operation on tables using functions only a)Addition b)Subtration c)Multiplication d)Transpose

Practical # 4

 

/* Write a program following operation on tables using functions only  a)Addition b)Subtration c)Multiplication d)Transpose  */

 

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<ctype.h>

void main()

{

int mat1[3][3],mat2[3][3],m,n,i,j,ch;

int mat_add(int mat1[3][3],int mat2[3][3]);

int mat_sub(int mat1[3][3],int mat2[3][3]);

int mat_multi(int mat1[3][3],int mat2[3][3]);

int mat_trans(int mat1[3][3],int mat2[3][3]);

char ch1;

clrscr();

printf("\n Enter the elements of the matrix1:\n");

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

{

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

{

scanf("%d",&mat1[i][j]);

}

}

printf("\n Enter the elements of the matrix2:\n");

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

{

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

{

scanf("%d",&mat2[i][j]);

}

}

printf("\nThe entered matrix1 is \n");

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

{

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

{

printf("%d  ",mat1[i][j]);

}

printf("\n");

}

printf("\nThe entered matrix2 is \n");

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

{

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

{

printf("%d  ",mat2[i][j]);

}

printf("\n");

}

getch();

do

{

clrscr();

printf("\n\n\tMENU");

printf("\n1. Addition");

printf("\n2. Subtraction");

printf("\n3. multiplication");

printf("\n4. Transpose");

printf("\n0. Exit\n");

printf("Enter your choice");

fflush(stdin);

scanf("%d",&ch);

switch(ch)

{

case 1:

mat_add(mat1,mat2);

break;

case 2:

mat_sub(mat1,mat2);

break;

case 3:

mat_multi(mat1,mat2);

break;

case 4:

mat_trans(mat1,mat2);

break;

default:

exit(1);

}

printf("Do you want to continue(y/n)?");

fflush(stdin);

scanf("%c",&ch1);

}while(toupper(ch1)=='Y');

}

int mat_add(int mat1[3][3],int mat2[3][3])

{

int sum_mat[3][3],i,j;

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

{

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

{

sum_mat[i][j]=mat1[i][j]+mat2[i][j];

}

}

printf("\n\n\n Addition of two matrix= \n");

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

{

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

{

printf("%d  ",sum_mat[i][j]);

}

printf("\n");

}

return 0;

}

int mat_sub(int mat1[3][3],int mat2[3][3])

{

int sub_mat[3][3],i,j;

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

{

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

{

sub_mat[i][j]=mat1[i][j]-mat2[i][j];

}

}

printf("\n\n\n Subtraction of two matrix= \n");

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

{

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

{

printf("%d  ",sub_mat[i][j]);

}

printf("\n");

}

return 0;

}

int mat_multi(int mat1[3][3],int mat2[3][3])

{

int prod_mat[3][3],i,j,k;

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

{

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

{

prod_mat[i][j]=0;

}

}

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

{

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

{

for(k=0;k<3;k++)

{

prod_mat[i][j]=prod_mat[i][j]+(mat1[j][k]*mat2[k][j]);

}

}}

printf("\n\n multiplicaton of matrixes is:\n");

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

{

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

{

printf("%d  ",prod_mat[i][j]);

}

printf("\n");

}

return 0;

}

 

int mat_trans(int mat1[3][3],int mat2[3][3])

{

int trans_mat1[3][3],trans_mat2[3][3],i,j;

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

{

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

{

trans_mat1[i][j]=mat1[j][i];

trans_mat2[i][j]=mat2[j][i];

}

}

printf("\n\n\n The transpord matrix1 is = \n");

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

{

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

{

printf("%d  ",trans_mat1[i][j]);

}

printf("\n");

}

printf("\n\n\n The transpord matrix2 is = \n");

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

{

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

{

printf("%d  ",trans_mat2[i][j]);

}

printf("\n");

}

return 0;

}

 

Output:-

 

 Enter the elements of the matrix1:

9 8 7

6 5 4

3 2 1

 

 Enter the elements of the matrix2:

1 2 3

4 5 6

7 8 9

 

The entered matrix1 is

9  8  7

6  5  4

3  2  1

 

The entered matrix2 is

1  2  3

4  5  6

7  8  9

 

        MENU

1. Addition

2. Subtraction

3. multiplication

4. Transpose

0. Exit

Enter your choice1

Addition of two matrix=

10  10  10

10  10  10

10  10  10

Do you want to continue(y/n)?y

 

 

 

Enter your choice2

Subtraction of two matrix=

  8     6     4

  2     0    -2

 -4    -6   -8

 

 

Do you want to continue(y/n)?y

Enter your choice4

 The transpord matrix1 is =

9  6  3

8  5  2

7  4  1

The transpord matrix2 is =

1  4  7

2  5  8

3  6  9

 

No comments :

Post a Comment