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

Q. Implement the following code as a ‘for’ loop. for( ; ; ) printf(“%d ”, i); i

ID: 3874024 • Letter: Q

Question

Q. Implement the following code as a ‘for’ loop.

for( ; ; )

printf(“%d ”, i);

i = 0; // initialization

while(i < n){ // condition check

printf("%d ", i);

i++; // update

}

Q. What is the output of the following code?

char c = ‘a’;

int i=’A’;

putchar(c+2);

putchar(i+2);

Q.Complete the definition of the following exchange function for two integers.

void exchange(int *x, int *y)

{

int temp;

}

Q. With the ‘exchange’ function defined in the previous question, exchange two elemnts array[3] and array[5].

int main()

{

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// call exchange

_________________________

}

Q. Complete the output of the following code segment.

double average(_____________)

{

int sum = 0;

for(int i = 0; i < 3; i++)

for(int j = 0; j < 5; j++)

sum += array[i][j];

return (sum / 15.);

}

int main()

{

double avg = 0.;

int base[3][5];

ave = average(base);

printf(“ave = %f ”, avg);

return 0;

}

for( ; ; )

printf(“%d ”, i);

Explanation / Answer

1) for(int i =0;i<n;i++)

{

printf("%d",i);

}

Explanation: We have i = 0 as initialization and condition check as i<n and i++ as update hence the for loop becomes

for(int i =0;i<n;i++)

2) cC

Explanation: putChar() function writes the character to the console and it will write the character which is passed as argument to it.

Now the first putChar where we are passing character variable c+2 where c is who is holding 'a' and the ascii value is 97 now 97+2 becomes 99 which is ascii of character c hence it will display c.

Next is putChar()we are passing i+2 where i is holding character A whose ascii value is 65 and 65+2 is 67 which is ascii value of C hence putChar will display C.

3)void exchange(int *x, int *y)

{temp = *y;

*y = *x;

*x = temp;}

Explanation: Here we are strong *y to temp and then in second assigning *x to *y which means now y have value of x and then as we stored value of y in temp variable we will assign that to *x so that x will have value of y. So swapped/exchanged.

4) int main()

{   

int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

exchange(&array[3],&array[5]);

return 0;

}

Explanation: Now rewriting the exchange function defined above.

We just call the function in main with argument passed address of index 3 and 5 to whom the parameters in function argument list will point to.

5) double average(int array[3][5])

Explanation: The only incomplete code was the argument of function average. As we are passing array from main to this function so the argument list will have an array and the name is written "array" because in the code definition of average function we are using the same name.

Also to point there is a mistake at line

ave = average(base);

It should be

avg = average(base); //as we have declared variable avg and not ave, must be a typo.