Please explain every answer here because I am trying to understand it. thanks Fo
ID: 3675862 • Letter: P
Question
Please explain every answer here because I am trying to understand it.
thanks
For questions 7 below, assume we have the following C variable declarations: int i,j, sum = 0, count = 0; int x[3] = { 1,2,3 } ; int y[3]= {2,3,1 }; int z[3] = {3,1,2}; 7. (15 points) Give the value of sum and count after the following for statements are executed: for (i-0; i 1) Il (i>1)l (li]>1) count++ Value of sum: Value of count: 8. (10 points) Assume we have the following declaration: Which one of the following two-dimensional arrays will result from the above declaration? a. c. *2?Explanation / Answer
7)
WHen we enter the loop
i =0 ,
sum will be sum + x[0] + y[0] +z[0]
sum = 0 (as we have initialized the value of sum as zero) + 1 + 2 + 3
sum = 0 +1 +2 +3
sum = 6
now value of sum is 6
checking the if statement, if any of the inequality is true we will increment the count
x[0] > 1 false
y[0] > 1 true so count++
therefore count =1
i=2
sum will be sum + x[1] + y[1] +z[1]
sum = 6 (we calculated in first iteration) + 2 + 3 + 1
sum = 6 + 2+ 3 +1
sum = 12
now value of sum is 12
checking the if statement, if any of the inequality is true we will increment the count
x[1] > 1 is true so count ++
therefore count =2 now
i=2
sum will be sum + x[2] + y[2] +z[2]
sum = 12 (we calculated in first iteration) + 3 + 1 + 2
sum = 12 + 3 +1+2
sum = 18
now value of sum is 18
checking the if statement, if any of the inequality is true we will increment the count
x[2] > 1 true so count++
therefore count =3 now
loop ends now
as we can see.
final value of count = 3 and sum =18
8)
M[5][4] represents a matrix with 5 rows and 4 columns
each entry in the first line represent the element of matirix.
this is quite straighforward.
9)
A[10] = { 3,5,1,8,0,4,6,9,7,2}
when we enter the for loop
i = 0 , A[0] gets added to s and the sum is stored in s.
s = 0 + 3
s = 3
now values to s is 3
i = 1, A[1] gets added to s and the sum is stored in s
s = 3 + 5
s = 8
now value of s is 8
i = 2 , A[2] gets added to s and the sum is stored in s.
s = 8 + 1
s = 9
now values to s is 9
i = 3, A[3] gets added to s and the sum is stored in s
s = 9 + 8
s = 17
now value of s is 17
i = 4 , A[4] gets added to s and the sum is stored in s.
s = 17 + 0
s = 17
now values to s is 17
i = 5, A[5] gets added to s and the sum is tored in s
s = 17 + 4
s = 21
now value of s is 21
i = 6 , A[6] gets added to s and the sum is stored in s.
s = 21 + 6
s = 27
now values to s is 27
i = 7, A[7] gets added to s and the sum is tored in s
s = 27 + 9
s = 36
now value of s is 36
i = 8 , A[8] gets added to s and the sum is stored in s.
s = 36 + 7
s = 43
now values to s is 43
i = 9, A[9] gets added to s and the sum is tored in s
s = 43 + 2
s = 45
now value of s is 45
loop ends
a = s /10;
a = 45/10 = 4
again the fow loop . in this loop the count will be incremented if A[i] is greater than a, i.e A[i] is greater than 4
we can see the array A = {3,5,1,8,0,4,6,9,7,2}
In this array A[2],A[3],A[6], A[7], A[8] are greater than 4 so the count will be incremented 5 times.
therefore the count is 5 , and this is output of C program
10)
when k =5 is passed to function unknown , and for loop is entered with
i = 2, j=1 and k =5
At first iteration, i=2
j = i*j => j = 2*1 => j =2
now value of j is 2
second iteration , i =3
j = i*j => j = 3*2 => j =6
now value of j is 6
third iteration , i =4
j = i*j => j = 6*4 => j =24
now value of j is 24
fouth iteration , i =5
j = i*j => j = 24*5 => j =120
now value of j is 120
k==5 now and the loop ends
value of j is returned which is 120 = answer of part (a)
as we can observe
at i =2 j =2 which is 2 factorial
at i =3 j =6 which is 3 factorial
at i =4 j =24 which is 4 factorial
at i =5 j =120 which is 5 factorial
SO this function calculates the factorial of number k passed to the function.