a) What is the following function do? - assume that nis a positive integer and t
ID: 3937900 • Letter: A
Question
a) What is the following function do?
- assume that nis a positive integer and then Give a small example (n = 4).
Hint: don not interprete this question as explaining each line of the code, without understanding what the piece of code does as a whole. (explain at a higher level of abstraction).
void f_a(int n, int a[n][n], int b[[n][n], int c[n][n]) {
int i, j;
for (i=0; i<n; i++)
for (j=0; j<n; j++) {
if (i > j) {
b[i][j] = a[i][j]; c[i][j] = 0;
} else if (i == j)
b[i][j] = c[i][j] = a[i][j];
else {
b[i][j] = 0; c[i][j] = a[i][j];}
}
Explanation / Answer
Receive size of square matrix (n),3 matrices a b c of size n*n
Using a double for loop
- if i>j i.e assign lower diagonal elements of matrix "a" to matrix "b" and set matrix "c" elements to 0
- if i==j if diagonal elements then assign elements of matrix a to matrix b and c
- for remaining elements i.e upper diagonal elemnts set matrix b elements to 0 and set matrix a elements to matrix c.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.