1.1 Determine the output of the following block of code (5 pts).

 

 

 

int x = 4, y = 9;

 

x = x * 10;

 

y = x - y;

 

x = y/x;

 

printf(“%d\t%d\t%f”, x, y, y/2.0);

 

 

 

 

 

1.2 Write a block of code equivalent to the code below using a while loop instead of a for loop. Determine the values of i, x, N after the loop has terminated (5 pts).

 

int i, x = 4, N 5;

 

 

 

for (i = 1; i < N; i +=2)

 

{

 

x += 1;

 

}

 

 

 

1.3 Determine the value of j after the following code is executed. Please explain your answer! (5 pts).

 

 

 

double x = 2.4;

 

int i = 3, j = 4;

 

 

if ( 2 * j > 4 * x )

 

j = 10;

 

else if ( i%j )

 

j = 20;

 

else

 

j = 30;

 

 

1.4. Hand trace all values of i and m during the execution of the following for loop (5 pts)

 

int i = 4, m = 8; i m

 

initial value:

 

while ( i > 0 ) 1st iteration:

 

{ 2nd iteration:

 

m /= 2; ...

 

i -= 1; ...

 

}

 

 

 

 

 

2. Code execution

 

 

 

2.1 Determine the output of the following program (5 pts).

 

 

 

#include <stdio.h>

 

 

 

int main(void)

 

{

 

int i, j;

 

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

 

for (j = 0; j < i; j++) {

 

printf("%d ", i*j);

 

}

 

printf("\n");

 

}

 

return 0;

 

}

 

1.3 Convert the following while loop to a for loop

 

i = 10;

 

 

 

while ( i > 0)

 

{

 

printf("%d, %d\n", i, 10*i);

 

i-=2;

 

}

 

3. Code composition

 

3.1 A geometric series is defined as a series of numbers with a constant ratio between successive terms. For example, in the series ½+1/4+1/8…, the ratio between two successive numbers is equal to ½.

 

Write a function named series_sum that computes the finite sum of the first n terms. Your function shall receive as input the ratio between successive terms and the number of terms to be added, and returns the series sum.

 

    • 12 years ago
    Solutions
    NOT RATED

    Purchase the answer to view it

    blurred-text
    • attachment
      c_outputs.docx