Monday, December 6, 2010

Matrix Manipulations

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define n 3
int matA[n][n],matB[n][n],matAns[n][n]={0},matD[n][n];
int i,j,k;
char mat;
void input();
void sum();
void multiply();
void display();
void transpose();


void main()
{
clrscr();
int opt;
do
{
printf("~~~~~~~~~\n");
printf("MAIN MENU\n");
printf("---------\n");
printf("INPUT:1\n");
printf("SUM:2\n");
printf("PRODUCT:3\n");
printf("TRANSPOSE:4\n");
printf("EXIT:5\n");
printf("~~~~~~~~~\n");
printf("Enter your option:");
scanf("%d",&opt);
switch(opt)
{
case 1:input();break;
case 2:sum();break;
case 3:multiply();break;
case 4:transpose();break;
case 5:exit(0);break;
}
}
while(opt<7);
}


void input()
{
int row,column;
printf("\nTo which matrix, you want to INPUT(A:A,B:B): ");
cscanf("%c",&mat);


if((mat=='A')||(mat=='a'))
{
printf("\n\nMatrix A:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the number at [%d][%d] in matA: ",i,j);
scanf("%d",&matA[i][j]);
}
}
}
else if((mat=='B')||(mat=='b'))
{
printf("\n\nMatrix B:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter the number at [%d][%d] in matB: ",i,j);
scanf("%d",&matB[i][j]);
}
}
}
printf("\n\n\n");
}


void sum()
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matAns[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matAns[i][j]=matA[i][j]+matB[i][j];
printf("\n\nMatrix A+B:\n\n");
display();
}


void multiply()
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matAns[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
matAns[i][j]+=matA[i][k]*matB[k][j];
printf("\n\nMatrix AxB:\n\n");
display();
}


void transpose()
{
int temp;
printf("\nWhich matrix, you want to TRANSPOSE(A:A,B:B,Ans:any other char): ");
cscanf("%c",&mat);
if((mat=='A')||(mat=='a'))
{
for(i=0;i<n;i++)
for(j=i;j<n;j++)
matAns[j][i]=matA[i][j];
printf("\n\nMatrix A':\n\n");
}
else if((mat=='B')||(mat=='b'))
{
for(i=0;i<n;i++)
for(j=0;j<n;j++)
matAns[j][i]=matB[i][j];
printf("\n\nMatrix B':\n\n");
}
else
{
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
temp=matAns[i][j];
matAns[i][j]=matAns[j][i];
matAns[j][i]=temp;
}
}
printf("\n\nMatrix Ans':\n\n");
}
display();
}


void display()
{
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",matAns[i][j]);
}
printf("\n");
}
printf("\n\n\n");
}

No comments:

Post a Comment