Matrix Problems in C
In lab courses we tried to build a program which solves the matrix equation ( transpose, addition, multiplication). Here is the simple sample of the code it could be the first step for you. If you can try by yourself, it will be good for you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void scalar_mul(int dz[3][2],int k)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
dz[i][j]*=k;
}
}
}
void add(int dz[3][2],int dz1[3][2],int toplam[3][2])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
toplam[i][j]=dz[i][j]+dz1[i][j];
}
}
}
void transpose(int dz[3][2],int swap[2][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
swap[j][i]=dz[i][j];
}
}
}
int main()
{
int matrix1[2][3]={{1,0,0},{0,0,1}},i,j,matrix2[3][2]={{-1,3},{6,-6},{-3,-1}},transpos[2][3];
int matrix3[3][2]={{1,-2},{-4,4},{2,1}}, matrix4[3][2]={{-1,3},{6,-6},{-3,-1}},ad[3][2],s,n;
int mult[3][2];
char c;
start:
printf("Enter the transaction code\n");
printf("1-Scalar Multiplication\n2-Addition\n3-Transpose\n");
scanf("%d",&s);
system("cls");
if(s==1)
{
printf("Enter an integer number\n");
scanf("%d",&n);
printf("First Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
mult[i][j]=matrix3[i][j];
printf(" %d ",mult[i][j]);
}
if(i==1)
printf(" x %d",n);
printf("\n");
}
scalar_mul(mult,n);
printf("\nFinal Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",mult[i][j]);
}
printf("\n");
}
}
else if(s==2)
{
printf(" First Matrixes\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",matrix2[i][j]);
}
if(i==1)
printf("+");
for(j=0;j<2;j++)
{
printf(" %d ",matrix3[i][j]);
}
printf(" ");
printf("\n");
}
add(matrix2,matrix3,ad);
printf("\nFinal Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",ad[i][j]);
}
printf("\n");
}
}
else if(s==3)
{
printf("First Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",matrix4[i][j]);
}
printf("\n");
}
transpose(matrix4,transpos);
printf("\nFinal Matrix\n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",transpos[i][j]);
}
printf("\n");
}
}
else
printf("ERROR!!!\n");
printf("\n\t(E)xit/(C)ontinue\n");
scanf("%c",&c);
scanf("%c",&c);
c=toupper(c);
while(c)
{
if(c=='C')
{
system("cls");
goto start;
}
else if(c=='E')
{
system("cls");
break;
}
}
printf("Good Bye\n");
system("PAUSE");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void scalar_mul(int dz[3][2],int k)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
dz[i][j]*=k;
}
}
}
void add(int dz[3][2],int dz1[3][2],int toplam[3][2])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
toplam[i][j]=dz[i][j]+dz1[i][j];
}
}
}
void transpose(int dz[3][2],int swap[2][3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
swap[j][i]=dz[i][j];
}
}
}
int main()
{
int matrix1[2][3]={{1,0,0},{0,0,1}},i,j,matrix2[3][2]={{-1,3},{6,-6},{-3,-1}},transpos[2][3];
int matrix3[3][2]={{1,-2},{-4,4},{2,1}}, matrix4[3][2]={{-1,3},{6,-6},{-3,-1}},ad[3][2],s,n;
int mult[3][2];
char c;
start:
printf("Enter the transaction code\n");
printf("1-Scalar Multiplication\n2-Addition\n3-Transpose\n");
scanf("%d",&s);
system("cls");
if(s==1)
{
printf("Enter an integer number\n");
scanf("%d",&n);
printf("First Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
mult[i][j]=matrix3[i][j];
printf(" %d ",mult[i][j]);
}
if(i==1)
printf(" x %d",n);
printf("\n");
}
scalar_mul(mult,n);
printf("\nFinal Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",mult[i][j]);
}
printf("\n");
}
}
else if(s==2)
{
printf(" First Matrixes\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",matrix2[i][j]);
}
if(i==1)
printf("+");
for(j=0;j<2;j++)
{
printf(" %d ",matrix3[i][j]);
}
printf(" ");
printf("\n");
}
add(matrix2,matrix3,ad);
printf("\nFinal Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",ad[i][j]);
}
printf("\n");
}
}
else if(s==3)
{
printf("First Matrix\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",matrix4[i][j]);
}
printf("\n");
}
transpose(matrix4,transpos);
printf("\nFinal Matrix\n\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf(" %d ",transpos[i][j]);
}
printf("\n");
}
}
else
printf("ERROR!!!\n");
printf("\n\t(E)xit/(C)ontinue\n");
scanf("%c",&c);
scanf("%c",&c);
c=toupper(c);
while(c)
{
if(c=='C')
{
system("cls");
goto start;
}
else if(c=='E')
{
system("cls");
break;
}
}
printf("Good Bye\n");
system("PAUSE");
return 0;
}
Comments
Post a Comment