For each sample of code given below, indicate precisely how many times each line
ID: 3833615 • 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.
Q1.
//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
}
Q2.
or (int i=0; i<n; i++) { //assume n is divisible by 2 => n = 2*k
for (j=n/2; j>i; j--)
sum = i+j;
}
Q3.
for (int i=0; i<n; i++) {
for (int j=n; j>=i; j--)
cout << i << “,” << j <<endl;
}
Explanation / Answer
1)The first program having 6 lines of statement,the following is the number of time each line will be executed.
1
m
m*p
m*p
m*p*n
m*p*n
2)i)n
ii)n/2+n/2-1+n/2-2...for n/2 times.
eg:if n=6
then 3+2+1=6 because here n=6 so n/2 times means 3 times.so 3+2+1 takes
iii)also same as ii)
3)i)n
ii)
(n+1)+n+(n-1)+(n-2)...for n times.eg:n=3
then 4+3+2=9,because here n=3.
iii)same as like ii)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.