Question

1) Implement a C program that multiplies two matrices with dimension n x m and m...

1) Implement a C program that multiplies two matrices with dimension n x m and m x r (n, m, r are provided by the user as argument to the main function. The elements of the matrix are generated randomly. Test the program.

example: matmult 2 3 4 will multiply two matrices of dimensions 2 x 3 and 3 x 4, the elements are generated randomly.

2) Determine the highest dimension(s) for which the program will crash

3) Please Explain the solution do you propose to solve this problem?

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

#source code:

#include <stdio.h>

#include <stdlib.h>

int main(){

printf("Enter n,m,r values and give space between:");

int n,m,r;

scanf("%d %d %d",&n,&m,&r);

int a[n][m],b[m][r];

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

a[i][j]=rand()%50;

}

}

for(int i=0;i<m;i++){

for(int j=0;j<r;j++){

b[i][j]=rand()%50;

}

}

int c[n][r];

for(int i=0;i<n;i++){

for(int j=0;j<r;j++){

c[i][j]=0;

}

}

for(int i=0;i<n;i++) {

for (int j=0;j<r;j++) {

for(int k=0;k<m;k++) {

c[i][j]+=a[i][k]*b[k][j];

}

}

}

for(int i=0;i<n;i++){

for(int j=0;j<r;j++){

printf("%d\t",c[i][j]);

}

printf("\n");

}

return 0;

}

#output:

user@user-Latitude - 3490:-/Desktops gcc 1.C -0 samp user@user-Latitude - 3490:-/Desktop$ ./samp Enter n,m,r values and give

#if you have any doubt or more information needed comment below..i will respond as possible as soon..thanks...

Add a comment
Know the answer?
Add Answer to:
1) Implement a C program that multiplies two matrices with dimension n x m and m...
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
  • 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...

  • 15.2-1 -- Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is (5,...

    15.2-1 -- Find an optimal parenthesization of a matrix-chain product whose sequence of dimensions is (5, 10, 3, 12, 5, 50, 6). 5. -- Implement Matrix-ChainMultiply(A,s,i,j) using algorithm Matrix-Chain-Order and Matrix-Multiply, where Matrix-Multiply(X,Y, p,q,r) multiplies matrices X and Y, X and Y have pxq and qxr demension, respectively. Given a chain of 6 matrices whose dimensions are given in 15.2-1, and elements are random real numbers from -10 to 10, use Matrix-Chian-ultiply to calculate the product of these matrices.

  • Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt...

    Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt and multiplies them together to obtain matrix C such that the element C(i,j) ofmatrix C is given by: C(i, j) =  ∑A(i, k) ∗ B(k, j) where n = number of columns in A = number of rows in B. Display the matrix C in defaultMATLAB format.
You cannot use the MATLAB matrix multiply or other inbuilt MATLAB functions forarithmetic operations. You must implement it....

  • Write a C++ program to read two matrices with any size. Your program should have at...

    Write a C++ program to read two matrices with any size. Your program should have at least the following functions: Main() Read a matrix Add two matrices Subtract two matrices Multiply two matrices Divide two matrices Display a matrix

  • MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds...

    MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds them (if compatible) and prints their sum, 2) subtracts them (if compatible) and prints their difference, and 3) multiplies them (if compatible) and prints their product. Prompt the user for the names of two matrix input files (see format below) and place the output in a file of the user’s choosing. The format of the input files should contain the number of rows, followed...

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

  • Develope a CPP program that multiplies two square matrixes. Your program will follow the steps below....

    Develope a CPP program that multiplies two square matrixes. Your program will follow the steps below. 1. Print a statement welcoming the user to the program. 2. Ask the user the size of the matrix. (You only need one number since it will be square.) 3. Ask the user for the values of the first matrix. 4. Aks the user for the values of the second matrix 5. Display the result of multiplying the two matrixes.

  • Please code in MatLab or Octave Output should match Sample Output in the second picture Thank...

    Please code in MatLab or Octave Output should match Sample Output in the second picture Thank You 4. In this problem we will investigate using loops to calculate the product of two matrices. Given an mxn matrix A and an n x p matrix B, the matrix product C = AB is the m xp matrix with Cij = R=1 Qikbky. Write a program which calls a function get matrix.dimensions which should • print a description of the purpose of...

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

  • MATLAB HELP!!! Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and on...

    MATLAB HELP!!! Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p, in which case C is an m × q matrix. 5. Recall that if A is an mx n matrix and B is a px q matrix, then the product C-AB is defined if and only if n = p, in which case C is...

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