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

Write a function matrix_multiplication() to multipy two matrices and put the res

ID: 3567711 • Letter: W

Question

Write a function matrix_multiplication() to multipy two matrices and put the result into a third matrix The function takes six arguments altogether i e pointers to the matrices to be multiplied and the resulting matrix and three values for the number of rows columns and the shared dimension We want to assume that all values are of type integer This said the prototype of the function is matrix_multiplication(ine left_matrix. int* right_matrix. int* result_matrix. int rows. int cols. int shared): Write a quick program that makes use of matrix_multiplication(). You are allowed to hard -code the values of the example matrices to be multiplied.

Explanation / Answer

#include <iostream>
using namespace std;

void matrix_multiplication(int* left_matrix, int* right_matrix, int* result_matrix, int rows, int cols, int shared)
{
for(int i=0; i<rows; i++)           //traverses through the rows
   {
       for(int j=0; j<cols; j++)       //traverses through the columns
       {
           int sum = 0;               //calculates the multiplication of result matrix of each position which is default to 0 before calculation
           for(int k=0; k<shared; k++) //traverses through the shared datas of both matrix
           {
               sum += (*((left_matrix + i*shared) + k)) * (*((right_matrix + k*cols)+j)); //stores multiplication of each rows and cols of both matrix
           }
           (*((result_matrix + i*cols) + j)) = sum; //now stores the sum to the actual position of the resultant matrix

       }
   }
}


int main()
{
   int lmatrix[2][3] = {1,2,3, //first matix
                       4,4,5};

   int rmatrix[3][4] = {1,2,3,4, //second matrix
                       5,6,7,8,
                       1,2,3,4};

   int res[2][4]; //resulta matrix

   matrix_multiplication((int*)lmatrix, (int*)rmatrix, (int*)res, 2, 4,3); //calls for matrix multiplication


   cout<<"Resultant matrix after multiplication: ";
   int i, j;                       //prints the data stored in res which is the resultant data after multiplication
for (i = 0; i < 2; i++)
   {
for (j = 0; j < 4; j++)
cout<< res[i][j]<<" ";
  
       cout<<" ";
   }

return 0;
}

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