Question

write C program that receives n from the user and creates two n x n matrices...

write C program that receives n from the user and creates two n x n matrices (e.g. A and B). The program receives the data values of each matrix from the user and performs matrix multiplication and addition (e.g. C = A x B and D = A + C). The values of result matrices ( C and D) are shown on the screen.

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

Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.

CODE

#include <stdio.h>   

//main function
int main()
{
   //variables and arrays
   int n, i, j, k, first_matrix[100][100], second_matrix[100][100], sum[100][100], product[100][100], temp = 0;
  
   //prompt for n
   printf("Enter n: ");
  
   //reading n
   scanf("%d", &n);
  
   //prompt for first matrix values
   printf("Enter the elements of first matrix\n");   
  
   //rows
   for(i = 0; i < n; i++)
   {
       //columns
       for(j = 0; j < n; j++)
       {
           //reading matrix data
           scanf("%d", &first_matrix[i][j]);
       }
   }
  
   //prompt for first matrix values
   printf("Enter the elements of second matrix\n");   
  
   //rows
   for(i = 0; i < n; i++)
   {
       //columns
       for(j = 0; j < n; j++)
       {
           //reading matrix data
           scanf("%d", &second_matrix[i][j]);
       }
   }
  
   //performing addition
   for(i = 0; i < n; i++)
   {
       for(j = 0; j < n; j++)
       {
           //adding data and store in matrix sum
           sum[i][j] = first_matrix[i][j] + second_matrix[i][j];
       }
   }
  
   //performing multiplication
   for(i = 0; i < n; i++)
   {
       for(j = 0; j < n; j++)
       {
           for(k = 0; k < n; k++)
           {
               //multiplying data
               temp = temp + first_matrix[i][k] * second_matrix[k][j];
           }
           //store in matrix product
           product[i][j] = temp;
           temp = 0;
       }
   }
  
   //printing added matrix
   printf("Addition:\n");
  
   //rows
   for(i = 0; i < n; i++)
   {
       //columns
       for(j = 0; j < n; j++)
       {
           //printing data
           printf("%d\t", sum[i][j]);
       }
       printf("\n");
   }
  
   //printing multiplied matrix
   printf("Multiplication:\n");
  
   //rows
   for(i = 0; i < n; i++)
   {
       //columns
       for(j = 0; j < n; j++)
       {
           //printing data
           printf("%d\t", product[i][j]);
       }
       printf("\n");
   }
  
   return 0;
}

OUTPUT

CODE SCREEN SHOT

Add a comment
Know the answer?
Add Answer to:
write C program that receives n from the user and creates two n x n matrices...
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
  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • Write a method to multiply two matrices. The header of the method is: public static double[][]...

    Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double[][] b) To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume the column size of matrix a is n. Each element is For example, for...

  • Write a program that receives a positive integer n from the user and then calculates the...

    Write a program that receives a positive integer n from the user and then calculates the sum below by using a loop. Your program needs to ask the user to re-enter a different number if the user enters a non-positive one. Display the result. 1 SUM 1 2 3 ... n x x n = = = + + + +

  • in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices...

    in c++ Develop a program (name it AddMatrices) that adds two user provided matrices. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your code, and...

  • 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...

  • It is required to write a Matrix Calculator Program using C Programming Language that could only...

    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 matrix row-wise. Your program...

  • Develop a program in c++ (name it AddMatrices) that adds two matrices that a user inputs....

    Develop a program in c++ (name it AddMatrices) that adds two matrices that a user inputs. The matrices must of the same size. The program defines method Addition() that takes two two-dimensional arrays of integers and returns their addition as a two-dimensional array. The program main method defines two 3-by-3 arrays of type integer. The method prompts the user to initialize the arrays. Then it calls method Addition(). Finally, it prints out the array retuned by method Addition(). Document your...

  • 6. Consider a C program that reads two real numbers from the keyboard followed by a character where the character can b...

    6. Consider a C program that reads two real numbers from the keyboard followed by a character where the character can be one of the operators +,-, *, 1, or % providing the addition, subtraction, multiplication, division, or remainder respectively. Then the result is displayed on the screen For example, if the user types 2.0 3.0 % then your code will display: Note: In the above example the inputs are real numbers but the remainder only performs an integer operation....

  • 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...

  • Here's the main function of a C program that Reads in two matrices by prompting the...

    Here's the main function of a C program that Reads in two matrices by prompting the user for their dimensions, dynamically allocating memory from the heap, and reading the values into the memory using row major ordering for the matrix values. Multiplies the two matrices together and puts the result into another dynamically allocated piece of memory (after checking that the dimensions are appropriate for matric multiplication). Outputs the two input and the result matrices. Write the implementations of the...

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