Q4 matrix multiplication of Amn and Bnp The product is saved

Q4. //matrix multiplication of A[m][n] and B[n][p]. The product is saved
into C[m][p].
void mult_matricies( double A[][n], double B[][p], double C[][p], int m,
int n , int p )
{
for (int i=0; i<m; i++) {
for (int j=0; j<p; j++){
C[i][j] = 0;
for ( int k=0; k<n; k++) {
C[i][j] += A[i][k] * B[k][j];
}//for-k
}//for-j
}//for-i
}

Solution

Please let me know in case of any issue:

into C[m][p].

void mult_matricies( double A[][n], double B[][p], double C[][p], int m, int n , int p )
{
   for (int i=0; i<m; i++) { // this line iterates for m times : takes O(m) time, executes m times

       for (int j=0; j<p; j++){ // this line iterates for p times for each value of i : it takes O(p) time
                               // it executes m*p times
           C[i][j] = 0; // it takes O(1) tims
                       // it executes m*p times

           for ( int k=0; k<n; k++) { //this line takes : O(k) time
                                       // it executes m*p*k times

               C[i][j] += A[i][k] * B[k][j]; // this line takes O(1) time, this line executes m*p*k times

           }//for-k
       }//for-j
   }//for-i
}

TOtal time : O(m*p*k)

Q4. //matrix multiplication of A[m][n] and B[n][p]. The product is saved into C[m][p]. void mult_matricies( double A[][n], double B[][p], double C[][p], int m,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site