Question

To understand the value of counting loops, write a program in C++ that implements matrix multiplication...

To understand the value of counting loops, write a program in C++ that implements matrix multiplication using counting loop constructs. Let the user input the size/dimensions of the matrix. Then write the same program using only logical loops—for example, while loops.

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

#include <iostream>
using namespace std;
int main()
{
//variable declaration
int a[10][10], b[10][10], c[10][10], row1, col1, row2, col2, i, j, k;
  
//get input from the user
cout<<"Enter rows and columns for first matrix: ";
cin>>row1>>col1;
cout<<"Enter rows and columns for second matrix: ";
cin>>row2>>col2;

//check if multiplication is possible or not
if(col1!=row2)
{
cout<<"Matrices multiplication not possible.";
return 0;
}

//get the elements of first matrix
cout<<endl<<"Enter elements of first matrix:"<< endl;
for(i = 0; i < row1; ++i)
{
for(j = 0; j < col1; ++j)
{
cin >> a[i][j];
}
}

//get the elements of second matrix
cout<<endl<<"Enter elements of second matrix:"<< endl;
for(i = 0; i < row2; ++i)
{
for(j = 0; j < col2; ++j)
{
cin >> b[i][j];
}
}

//multiply the matrix and initialize in the third 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];
}
}
}
  
//display the resultant matrix
cout<<endl<<"The resultant matrix is: "<<endl;
for(i = 0; i < row1; ++i)
{
for(j = 0; j < col2; ++j)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
To understand the value of counting loops, write a program in C++ that implements matrix multiplication...
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 4: (7 pts) To understand the value of recursion in a programming language, write a...

    Problem 4: (7 pts) To understand the value of recursion in a programming language, write a Program in C++ or Python that implements quicksort, first using recursion and then without recursion. Submit your program source code and  screenshot(s) of the output in a separate file (pdf or .docx) Problem 5: (7 pts) To understand the value of counting loops, write a program in C++ or Python that implements matrix multiplication using counting loop constructs. Let the user input the size/dimensions of...

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

  • In C++ The Multiplication Program Step 1: Write a program that stores a multiplication table in...

    In C++ The Multiplication Program Step 1: Write a program that stores a multiplication table in a 9-by-9 two-dimensional array. Generate the multiplication table with two loops. (So you will have a nested loop that will iterate 9 times and fill the 9x9 array with the values.) Step 2: Display the table for the user to see it. a 9x9 table Step 3: Create a function that returns the product of two numbers between 1 and 9by looking up the...

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

  • Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D...

    Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D arrays. a. Get the number of rows and columns of both matrix A and B from the user, and check if multiplication is possible with matrix A and B. b. If matrices multiplication is possible, input the matrix A and B using arrays and multiply matrix A and B.

  • 1. (30 Points) Write a MATLAB program that displays "The multiplication of 10 multiplied by integers...

    1. (30 Points) Write a MATLAB program that displays "The multiplication of 10 multiplied by integers of 1 through 15. Display your result back to the user with 2 decimal places (yes, 2 decimal places). You must implement your code using a for loop. The result should have the following format: The multiplication of 10*1 =10.00 The multiplication of 10*2 =20.00 The multiplication of 10'3=30.00 The multiplication of 10*15=150.00 2. (35 Points) Write MATLAB code to prompt a user for...

  • OBJECTIVES: To understand repetition structures To use while loops in a program To understand break and/or...

    OBJECTIVES: To understand repetition structures To use while loops in a program To understand break and/or continue instructions Write a Python program (with comments) to do the following: Create a variable count and initialize it to 0 Create a variable colors and initialize it to [] (i.e. an empty list) Ask the user if they would like to enter up to 3 favorite colors. They should enter ‘y’ or ‘n’. Assume they will enter a valid input. Save the response...

  • Using two for loops write a program to print a multiplicationtable. Ask the user to...

    Using two for loops write a program to print a multiplication table. Ask the user to input a number between 1 and 10 and print the multiplication table.

  • matlab progam A commonly used matrix operation in linea alpebrais matrix matrix multiplication. Write a script...

    matlab progam A commonly used matrix operation in linea alpebrais matrix matrix multiplication. Write a script that reads a matrix M from the that this is different from the clement wise Squaring of the matrix ( M 2). To do this, follow these steps income che of them (M2) Note a. Read a 2x2 matrix from the user and store it in M. Make sure you let the user know what to input b. Print the dimensions of the matrix,...

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