Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

It is required to write a Matrix Calculator program using C Programming Language

ID: 3865367 • Letter: I

Question

It is required to write a Matrix Calculator program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should enable the user to select a matrix operation from a list of options that contain the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices (use input validation loop to make sure that the user entered approximate matrix dimensions for the selected operation). Next, your program should prompt the user to enter the elements of each matrix row-wise. Your program should perform the selected matrix operation on operand matrices and display the result on the console.

Assuming that: 2.2 5.6 -8.9 25.3 17.5 11.8 5.3 9.4-6.7 120.1 -12.2 38.7 1.2 26.7 14.3 1 12 12 18.2 2.5 12.3 -95.2 7.9 68.2 15-12.5 , and C19.2 22 -30 17 Test your program by computing the following: · D=A+B E=A-B, and ·

Explanation / Answer

copyable code:

#include <stdio.h>
#include <stdlib.h>
void Addition(int r1,int c1,int r2,int c2)
{
int x,y,z,i;
float sum=0;
float **mat1 = (float **)malloc(r1 * sizeof(float *));
for (i=0; i<r1; i++)
mat1[i] = (float *)malloc(c1 * sizeof(float));

float **mat2 = (float **)malloc(r2 * sizeof(float *));
for (i=0; i<r2; i++)
mat2[i] = (float *)malloc(c2 * sizeof(float));
float **add = (float **)malloc(r1 * sizeof(float *));
for (i=0; i<r1; i++)
add[i] = (float *)malloc(c1 * sizeof(float));

printf(" Enter the value for the Matrix1 rowise ");

for (x = 0; x < r1; x++)
for (y = 0; y < c1; y++)
scanf("%f", &mat1[x][y]);

printf(" Enter the value for the Matrix2 rowise ");

for (x = 0; x < r2; x++)
for (y = 0; y < c2; y++)
scanf("%f", &mat2[x][y]);
printf("Resultant matrix after Performing addition ");
for (x = 0; x < r1; x++) {
for (y = 0 ; y < c1; y++) {
add[x][y] = mat1[x][y] + mat2[x][y];
printf("%f ", add[x][y]);
}
printf(" ");
}
for(i=0;i<r1;i++)
free(mat1[i]);
for(i=0;i<r2;i++)
free(mat2[i]);
for(i=0;i<r1;i++)
free(add[i]);
free(mat1);
free(mat2);
free(add);

}

void Subtraction(int r1,int c1,int r2,int c2)
{
int x,y,z,i;
float **mat1 = (float **)malloc(r1 * sizeof(float *));
for (i=0; i<r1; i++)
mat1[i] = (float *)malloc(c1 * sizeof(float));
float **mat2 = (float **)malloc(r2 * sizeof(float *));
for (i=0; i<r2; i++)
mat2[i] = (float *)malloc(c2 * sizeof(float));
float **diff = (float **)malloc(r1 * sizeof(float *));
for (i=0; i<r1; i++)
diff[i] = (float*)malloc(c1 * sizeof(float));

printf(" enter the value for the Matrix1 rowise ");

for (x = 0; x < r1; x++)
for (y = 0; y < c1; y++)
scanf("%f", &mat1[x][y]);

printf(" enter the value for the Matrix2 rowise ");

for (x = 0; x < r2; x++)
for (y = 0; y < c2; y++)
scanf("%f", &mat2[x][y]);
printf("Resultant matrix after Performing subtraction ");
for (x = 0; x < r1; x++) {
for (y = 0 ; y < c1; y++) {
diff[x][y] = mat1[x][y] - mat2[x][y];
printf("%f ", diff[x][y]);
}
printf(" ");
}
for(i=0;i<r1;i++)
free(mat1[i]);
for(i=0;i<r2;i++)
free(mat2[i]);
for(i=0;i<r1;i++)
free(diff[i]);
free(mat1);
free(mat2);
free(diff);

}

void Multiplication(int r1,int c1,int r2,int c2)
{

int x,y,z,i;
float sum=0;
float **mat1 = (float **)malloc(r1 * sizeof(float *));
for (i=0; i<r1; i++)
mat1[i] = (float *)malloc(c1 * sizeof(float));
float **mat2 = (float **)malloc(r2 * sizeof(float *));
for (i=0; i<r2; i++)
mat2[i] = (float *)malloc(c2 * sizeof(float));
float **mul = (float **)malloc(r1 * sizeof(float *));
for (i=0; i<c2; i++)
mul[i] = (float *)malloc(c2 * sizeof(float));

printf(" enter the value for the Matrix1 rowise ");

for (x = 0; x < r1; x++)
for (y = 0; y < c1; y++)
scanf("%f", &mat1[x][y]);
printf(" enter the value for the Matrix2 rowisex ");
for (x = 0; x < r2; x++)
for (y = 0; y < c2; y++)
scanf("%f", &mat2[x][y]);
printf("Resultant matrix after Performing Multiplication ");
for (x = 0; x < r1; x++) {
for (y = 0; y < c2; y++) {
for (z = 0; z < r2; z++) {
sum = sum + mat1[x][z]*mat2[z][y];
}
mul[x][y] = sum;
printf("%f ", sum);
sum = 0;
}
printf(" ");
}
for(i=0;i<r1;i++)
free(mat1[i]);
for(i=0;i<r2;i++)
free(mat2[i]);
for(i=0;i<r1;i++)
free(mul[i]);
free(mat1);
free(mat2);
free(mul);

}

int main()
{
int choice,r1,c1,r2,c2;
printf(" Matrix Calculator");
printf(" choose an opertion to perform ");
printf("1.Addition(+) ");
printf("2.mul(-) ");
printf("3.Mutipication(*) ");
scanf("%d",&choice);
printf("Enter the number of rows and columns of first matrix ");
scanf("%d %d", &r1, &c1);
printf("Enter the number of rows and columns of second matrix ");
scanf("%d %d", &r2, &c2);
  
if(choice==1)
{

if(r1==r2&&c1==c2)
{
Addition(r1,c1,r2,c2);
}
else
printf("Not a valid dimension ");
}

else if(choice==2)
{
if(r1==r2&&c1==c2)
{
Subtraction(r1,c1,r2,c2);
}
else
printf("Not a valid dimension ");
}
else if(choice==3)
{
if(c1==r2)
{
Multiplication(r1,c1,r2,c2);
}
else
printf("Not a valid dimension ");

}
  
else
printf(" please ,enter a valid option!!! ");
return 0;
}

output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                             

sh-4.2$ main                                                                                                                                                                                        

                                                                                                                                                                                                   

Matrix Calculator                                                                                                                                                                                   

choose an opertion to perform                                                                                                                                                                       

1.Addition(+)                                                                                                                                                                                       

2.subtraction(-)                                                                                                                                                                                   

3.Mutipication(*)

1

Enter the number of rows and columns of first matrix                                                                                                                                                

4                                                                                                                                                                                                   

3                                                                                                                                                                                                  

Enter the number of rows and columns of second matrix                                                                                                                                              

4                                                                                                                                                                                                   

3                                                                                                                                                                                                   

                                                                                                                                                                                                   

Enter the value for the Matrix1 rowise                                                                                                                                                             

2.2                                                                                                                                                                                                 

5.6                                                                                                                                                                                                 

-8.9                                                                                                                                                                                               

-25.3                                                                                                                                                                                              

17.5                                                                                                                                                                                                

11.8                                                                                                                                                                                                

5.3                                                                                                                                                                                                 

9.4                                                                                                                                                                                                

-6.7                                                                                                                                                                                                

120.1                                                                                                                                                                                               

-12.2                                                                                                                                                                                               

38.7                                                                                                                                                                                               

   Enter the value for the Matrix2 rowise                                                                                                                                                             

1.2                                                                                                                                                                                                 

26.7                                                                                                                                                                                                

14.3                                                                                                                                                                                                

16.1                                                                                                                                                                                               

-12.5                                                                                                                                                                                               

7.2                                                                                                                                                                                                 

18.2                                                                                                                                                                                                

2.5                                                                                                                                                                                               

12.3                                                                                                                                                                                               

-95.2                                                                                                                                                                                               

7.9                                                                                                                                                                                                 

68.2                                                                                                                                                                                                

Resultant matrix after Performing addition                                                                                                                                                         

3.400000        32.299999       5.400001                                                                                                                                                           

-9.199999       5.000000        19.000000                                                                                                                                                           

23.500000       11.900000       5.600000                                                                                                                  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote