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

For each sample of code given below, indicate precisely how many times each line

ID: 3686186 • Letter: F

Question

For each sample of code given below, indicate precisely how many times each line runs in terms of the variables given. Sometimes, you will be able to give an exact integral answer, like "10". Other times, your answer will be in terms of n or some other variable or combination of variables. If the snippet of code is a method, you do not have to write the number of times the top line (the method declaration itself) is executed.

double sum_matrix( double matrix[][], int m, int n ) {

//m: num of rows; n: num of cols

double sum=0;

for (int i=m-1; i>=0; i--) {

for ( int j=n-1; j>=0; j--) {

sum = sum + matrix[ i ][ j ];

}

}

return sum;

}

Explanation / Answer

double sum_matrix( double matrix[][], int m, int n ) {
   //m: num of rows; n: num of cols

1.   double sum=0;
2.   for (int i=m-1; i>=0; i--) {
3.       for ( int j=n-1; j>=0; j--) {
4.           sum = sum + matrix[ i ][ j ];
       }
   }
5.   return sum;

}

Line 1: executes 1 times
Line 2: executes m times
Line 3. for each value of i(outer loop),
           line 3 executes n times
       Total = mn
Line 4: Since it is part of line 3 loop,
       So, it executes same number of times as line 3 = mn
      
Line 5: executes 1 times