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

I\'m doing a c++ program to create two 4x4 matrices then (a) tranpose them (b) m

ID: 3820181 • Letter: I

Question

I'm doing a c++ program to create two 4x4 matrices then (a) tranpose them (b) multiply the matrices together. But I keep on getting error on my program.

#include <iostream>
#include <ctime>
#include <cmath>
#include <cstdlib>
using namespace std;

int main()
{
   {

   int i, j, k, sum, A[4][4], B[4][4], trans_matrix[4][4], matrix[4][4];
      
   srand (time(NULL));
  
   cout << "Array A is: " << endl;
   for (int i=0; i<4; i++)
{
   cout<<endl<<" ";
   for(int j=0; j<4; j++)
{
   A[i][j]=( rand() %11 ) ;
   cout << " " << A[i][j];
}
       }
       cout << endl;
      
//transpose the A matrix
  
    for (int i=0; i<4; i++)
    {
        for (int j=0; j<4; j++)
        {
         trans_matrix A[i][j]=matrix A[i][j];
        }
    }
    cout << "Transpose of the A Matrix: " << endl;

    for (int i=0; i<4; i++)

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

        {
        cout << trans_matrix A[i][j] << " ";
        }
       cout << endl;
    }


   cout << "Array B is: " << endl;
   for (i=0; i<4; i++)
   {
       for(j=0; j<4; j++)
       {
           B[i][j] = rand() % 11;
           cout << " " << B[i][j];
          
       }
       cout << endl;
      
   }
    for (int i=0; i<4; i++)
    {
        for (int j=0; j<4; j++)
        {
         trans_matrix B[i[j]=matrix B[i][j];
        }
    }
    cout << "Transpose of the B Matrix: " << endl;

    for (int i=0; i<4; i++)

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

        {
        cout << trans_matrix B[i][j] << " ";
        }
       cout << endl;
    }      
}
}
   cout << endl;
   system("pause");
   return 0;
  
   }

Explanation / Answer

//code for transpose and multiplication of matrices:

// please try this code.It works.

#include <iostream>
using namespace std;

int main() {
   // your code goes here
   int a[4][4], b[4][4], trans_matrixA[4][4],trans_matrixB[4][4],mul_matrix[4][4], rows, cols, i, j,k,sum;
  
   //code to enter the elements of a matrix
   cout << endl << "Enter elements of matrix A : " << endl;
for(i = 0; i < 4; ++i){
for(j = 0; j < 4; ++j)
{
cout << "Enter elements a" << i + 1 << j + 1 << ": ";
cin >> a[i][j];
}
}
  
// code for Displaying the matrix a[][]
cout << endl << "Entered Matrix A: " << endl;
for(i = 0; i < 4; ++i){
for(j = 0; j < 4; ++j)
{
cout << " " << a[i][j];
if(j == 3)
cout << endl << endl;
}
}
  
//code to enter the elements of b matrix
   cout << endl << "Enter elements of matrix B : " << endl;
for(i = 0; i < 4; ++i){
for(j = 0; j < 4; ++j)
{
cout << "Enter elements b" << i + 1 << j + 1 << ": ";
cin >> b[i][j];
}
}
  
// code for Displaying the matrix b[][]
cout << endl << "Entered Matrix B: " << endl;
for(i = 0; i < 4; ++i){
for(j = 0; j < 4; ++j)
{
cout << " " << b[i][j];
if(j == 3)
cout << endl << endl;
}
}
  
  
// code for Finding transpose of matrix a[][] and storing it in array trans_matrixA[][].
for(i = 0; i < 4; ++i){
for(j = 0; j < 4; ++j)
{
trans_matrixA[j][i]=a[i][j];
}
}
  
// code for Finding transpose of matrix b[][] and storing it in array trans_matrixB[][].
for(i = 0; i < 4; ++i){
for(j = 0; j < 4; ++j)
{
trans_matrixB[j][i]=a[i][j];
}
}
  
//code for Displaying the transpose,i.e, Displaying array trans_matrix[][].
cout << endl << "Transpose of Matrix A: " << endl;
for(i = 0; i < 4; ++i){
for(j = 0; j < 4; ++j)
{
cout << " " << trans_matrixA[i][j];
if(j == 3)
cout << endl << endl;
}
}

cout << endl << "Transpose of Matrix B: " << endl;
for(i = 0; i < 4; ++i){
for(j = 0; j < 4; ++j)
{
cout << " " << trans_matrixB[i][j];
if(j == 3)
cout << endl << endl;
}
}
  
//code for multiplying matrices a and b.
for(i=0; i<4; i++){
       for(j=0; j<4; j++){
           sum=0;
           for(k=0; k<4; k++){
               sum = sum + a[i][k] * b[k][j];
           }
           mul_matrix[i][j] = sum;
       }
   }

// Displaying the multiplication of two matrix.
cout << endl << "Multiplication Matrix: " << endl;
for(i = 0; i < 4; ++i){
for(j = 0; j < 4; ++j)
{
cout << " " << mul_matrix[i][j];
if(j == 3)
cout << endl;
}
}

   return 0;
}

comments:

I think you are using  trans_matrix B[i[j]=matrix B[i][j] instead of trans_matrix B[j][i]=matrix B[i][j]

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