For each sample of code given below, indicate precisely how many times each line
ID: 3858986 • 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.
Q5. //matrix multiplication of A[m][n] and BInl[p]. The product is saved into CIm][pl. void mult_matricies( double A[n], double BO[p], double COLp], int m, int n , int p ){ for (int i-0; iExplanation / Answer
for(int i = 0; i < m; i++) => This line will run m times
for(int j =0; j < p; j++) => This line will rum m x p times.
C[i][j] = 0 => This line will run m x p times.
for(int k =0; k < n; k++) => This line will run m x p x n times.
C[i][j] += A[i][k] * B[k][j] => This line will again run m x p x n times.
EXPLANATION:
Look at the loop conditions and the number of loops. The first loop will run m times, The second loop will run m x p times and The third loop will run m x p x n times. Please note that condition starts from initial value 0 so the number of looping times is equal to m, p, n respectively but if the initial value would have been any other number other than 0 (say 1) than number of times would have been changed to m - 1, p - 1, n - 1 respectively.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.