Structure of a C Program
Q1.Given the following pseudocode, write a program that executes it. Use floating-point types for all values.
Algorithm Problem1
- Read x
- Read y
- Compute p = x - y
- Compute s = x / y
- Total = s2 + p * (s + x) * (p - y)
- Print the total
End Problem1
Q2.If originally, x = 3, y = 1 and z = 2, what will get printed? (Make sure that you use the same original x, y and z values for each questions)
a)printf( “ the value is : %d “, x++ + y++ );
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
b)printf( “ the value is : %d “, ++x - --z );
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
c)printf( “ the value is : %d “, --x + y++ );
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
d)printf( “ the value is : %d “, x-- + x-- - y-- );
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
e)printf( “ the value is : %d “, x + y-- - x + x++ - --y );
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
f)printf( “ the value is : %d “, x + 2 / 6 + y );
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
g)printf( “ the value is : %d “, y - 3 * z + 2);
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
h)printf( “ the value is : %d “, z - (x + z ) % 2 + 4);
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
i)printf( “ the value is : %d “, x - 2 * ( 3 + z) + y );
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
j)printf( “ the value is : %d “, y % 2 * z % x );
printf( “ Now the values of x, y and z are %2d %2d %2d”, x, y, z );
Q3.Evaluate the following expressions to true or false. (false is 0, and true is non-zero) Show how you arrived at your answer by first adding the default parentheses.
a)!( 3 + 3 >= 6)
b)1 + 6 == 7 || 3 + 2 == 1
c)1 > 5 || 6 < 50 & 2 < 5
d)6 < 7 > 5
Q4.If originally, x = 0, y = 3 and z = 1, what is the value of each x, y and z after executing the following code? Make sure that you use the original values for each question.
a)if ( x & y )
x = 3;
else
y = 2;
b)if ( x || y || z )
y = 1;
else
x = 4;
c)if ( x == 0 || x & !y )
if ( !z)
y = 1;
else
x = 4;
d)if ( z = y )
{
y++;
z—
}
else
--x;
Q5.Write a code segment for the following flow chart: