Question

It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtractio

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the code in C for the above question:

#include <stdio.h>

// function to find difference between two matrix
void Add(int row1,int col1,int row2,int col2,double A[][col1],double B[][col2]){
   // resultant matrix C.
   double C[row1][col1];
   int i,j;
  
   // finding sum.
   for(i=0;i<row1;i++){
       for(j=0;j<col1;j++){
           C[i][j] = A[i][j] + B[i][j];
       }
   }
  
   // displaying the result on console
   printf("\n Sum of matrices is :\n");
   for(i=0;i<row1;i++){
       for(j=0;j<col1;j++){
           printf("%.2lf ",C[i][j]);
       }
       printf("\n");
   }
}

// function to find difference between two matrix
void Sub(int row1,int col1,int row2,int col2,double A[][col1],double B[][col2]){
   // resultant matrix C.
   double C[row1][col1];
   int i,j;
  
   // finding difference
   for(i=0;i<row1;i++){
       for(j=0;j<col1;j++){
           C[i][j] = A[i][j] - B[i][j];
       }
   }
  
   // displaying the result on console
   printf("\n Difference of matrices is :\n");
   for(i=0;i<row1;i++){
       for(j=0;j<col1;j++){
           printf("%.2lf ",C[i][j]);
       }
       printf("\n");
   }
}

// function to find multiplication oftwo matrix A and B.
void Mul(int row1,int col1,int row2,int col2,double A[][col1],double B[][col2]){
   // resultant matrix C.
   double C[row1][col2];
   int i,j,k;
  
   // finding multiplication of two matrix
   for(i=0;i<row1;i++){
       for(j=0;j<col2;j++){
           C[i][j] = 0;
           for(k=0;k<col1;k++){
               C[i][j] += A[i][k]*B[k][j];
           }
       }
   }
  
   // displaying the result on console
   printf("\n Multiplication of matrices is :\n");
   for(i=0;i<row1;i++){
       for(j=0;j<col2;j++){
           printf("%.2lf ",C[i][j]);
       }
       printf("\n");
   }
}

int main()
{
   // choice variable of type integer.
   int choice;
  
   // loop validation for user choice
   while(1){
       printf("\nSelect and Operation:\n\n1. Addition\n2. Subtraction\n3. Multiplication\n\n\nYour Choice: ");
       scanf("%d",&choice);
      
       if(choice != 1 && choice != 2 && choice != 3){
           printf("\nError:\tInvalid choice!!\n");
           continue;
       }
       else break;
   }
  
   int row1,col1,row2,col2;
   // loop validation for dimension
   while(1){
       printf("Enter the dimension of matrix A: ");
       scanf("%d%d",&row1,&col1);
      
       printf("Enter the dimension of matrix B: ");
       scanf("%d%d",&row2,&col2);
      
       if(row1<0 || row2<0 || col1<0 || col2<0){
           printf("\nError:\tInvalid dimension!!\n");
           continue;
       }
       else break;
   }
  
   // creating matrix A and B of dimensions row1 x col1 and row2 x col2 resp.
   double A[row1][col1],B[row2][col2];
   int i,j;
  
   // getting elements of matrix A.
   printf("\nEnter the elements of matrix A\n");
   for(i=0;i<row1;i++){
       for(j=0;j<col1;j++){
           scanf("%lf",&A[i][j]);
       }
   }
  
   // getting elements of matrix B.
   printf("\nEnter the elements of matrix B\n");
   for(i=0;i<row2;i++){
       for(j=0;j<col2;j++){
           scanf("%lf",&B[i][j]);
       }
   }
  
   // using switch case for appropriate choice
   switch(choice)
   {
       case 1:
           // checks if dimesnions are same or not
           if(row1 == row2 && col1 == col2)
               Add(row1,col1,row2,col2,A,B);
           else
               printf("\nError:\tDimension should be equal!!\n");
           break;
       case 2:
           // checks if dimesnions are same or not
           if(row1 == row2 && col1 == col2)
               Sub(row1,col1,row2,col2,A,B);
           else
               printf("\nError:\tDimension should be equal!!\n");
           break;
       case 3:
           // checks the condition for multiplication of two matrix
           // i.e. column of first matrix must be equal to row of second matrix
           if(col1 == row2)
               Mul(row1,col1,row2,col2,A,B);
           else
               printf("\nError:\tDimension Mismatch!!\n");
           break;
   }
  
   return 0;
}


Refer to the screenshot attached below to better understand the code and indentation:
On 1 #include <stdio.h> 2 3 // function to find difference between two matrix 4 void Add(int rowl, int coli, int row, int col
도로 نہا 00 } } 33 for(i=0;i<rowl;i++){ 34 for(j=0; j<coll;j++){ 35 C[i][j] - A[i][j] - B[i][j]; 36 } 37 38 39 // displaying th
} } 65 // displaying the result on console 66 printf(\n Multiplication of matrices is :\n); 67 for(i=0;i<rowl;i++){ 68 for(
LE scanf(%d%d, &rowi,&coll); printf(Enter the dimension of matrix B: ); scanf(%d%d,&row2, &col2); if (rowl<0 || row2<0
// using switch case for appropriate choice switch(choice) ...... 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

Output:
Select an Operation: 1. Addition 2. Subtraction 3. Multiplication Your Choice: 1 Enter the dimension of matrix A: 4 3 Enter t

Select an Operation: 1. Addition 2. Subtraction 3. Multiplication Your Choice: 2 Enter the dimension of matrix A: 4 3 Enter t

select an Operation: 1. Addition 2. Subtraction 3. Multiplication Your Choice: 3 Enter the dimension of matrix A: 4 3 Enter t

If this answer helps you then please upvote,
for further queries comment below.
Thank you.

Add a comment
Know the answer?
Add Answer to:
It is required to write a Matrix Calculator Program using C Programming Language that could only...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming...

    Problem 3 (20 pts) 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 prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each...

  • Write a C program to implement matrix multiplication operations. First, you need to prompt the user...

    Write a C program to implement matrix multiplication operations. First, you need to prompt the user for the size of both matrix. Then take inputs for both matrices and perform matrix multiplication operation. Finally, print the resulting matrix into the output.

  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • 06) Write a C program to perform matrix operations based of threads. The program accepts from...

    06) Write a C program to perform matrix operations based of threads. The program accepts from the user a positive integer N. A menu is given to the user: 1. generate matrices: generates three NxN matrices A, B, C with random integer numbers between 0 and 9 2. add: matrix D- A+B+C 3. subtract: matrix D A-B-C 4. display matrices: A, B, C, D 5. display count of result values more than 8. 6. exit: terminate the program When the...

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • Problem: Design and write a C language program that can be used to calculate Voltage, Current...

    Problem: Design and write a C language program that can be used to calculate Voltage, Current and Total Resistance for a Series or Parallel or a Series Parallel circuit. The program should display the main menu that contains the three types of circuit calculations that are available and then the user will be prompted to select a circuit first. After the circuit has been selected the program should then display another menu (i.e., a submenu) requesting the necessary data for...

  • Write a program to create a set operation calculator applying object oriented programming principles discussed in...

    Write a program to create a set operation calculator applying object oriented programming principles discussed in the class so far. The set operation calculator need to be enclosed inside one class with different methods responsible for performing the different operations listed below. When the constructor (def __init__() ) is called during object instantiation, two separate objects of this class need to be instantiated two sets S1 and S2. These two set objects can be instantiated from two python lists taken...

  • **This is for the C Language. Trigonometric Calculator Specification You are required to write a program...

    **This is for the C Language. Trigonometric Calculator Specification You are required to write a program that calculates and displays values of the trigonometric functions sine, cosine, and tangent for user-supplied angular values. The basic program must comply with the following specification. 1. When the program is run, it is to display a short welcome message. TRIG: the trigonometric calculator 2. The program shall display a message prompting the user to enter input from the keyboard. Please input request (h-help,...

  • Trying to write this program using c language using functions, pointers and loops. "al 67% 1...

    Trying to write this program using c language using functions, pointers and loops. "al 67% 1 1 :14 AM importanu Name your TIies HW2 firstlastname.c Develop a solution to the following problem: A financial institution will only accept loan applications if the following conditions are met: The applicant must own his/her home with no mortgage payment and -The applicant must have a total annual household income of no less than $45000.00. The applicant must have a credit score of no...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT