Anda di halaman 1dari 3

1

Question 1 [3 marks]
Identify the following as integer constant, floating-point constant, character constant, string constant, or
invalid constant.

a) UTM_SKUDAI  invalid
b) 0.4e-5  floating-point
c) 1234.  floating-point

Question 2 [4 marks]
A C program contains the following declarations and initial assignments:
int i = 10, j = 3, k, c, z;
float x = 0.5, y = 0.1;
Determine the value for each of the following assignment expressions. Use the values initially assigned to
the variables for each expression.

a) k = 0.09*i;  0
b) c = (x*y < 2) && (i >= j);  1
c) y = y + 5.0/i;  0.6
d) z = y/x + pow(2,2);  4

Question 3 [4 marks]
a) Write a printf statement to generate the message
Enter real numbers x and y:
Then write a scanf statement to read x and y in the same line with the message. Assign the data
to floating point variables x and y.

printf(“Enter real numbers x and y:”);


scanf(“%f %f”, &x, &y);

b) Suppose that x1 is a character variable with an initial value of ‘A’ and x2 is a floating-point
variable with an initial value of 1234. Write a single printf statement to generate the
following screen output:
x1 = A
x2 = 1.234e+003

printf(“ x1 = %c\nx2 = %.3e“, x1, x2);

c) A C program contains the following declarations and initial assignments:


float x = 3, y= 4;

Using only x and y as the variables, write a printf statement to display the following output,
where length of vector is x 2  y 2 :
Length of vector is: 5.00

printf(“Length of vector is: %.2f”, sqrt(x*x + y*y));


2

Question 4 [4 marks]
The following C program to calculate the area of a circle cannot be compiled and would not display the output correctly.
There are four errors in it. Identify the errors and write the correct statement underneath the original statements.
*/ computation of the area of a circle /* <- /* … */
include <stdio.h> <- #include
main()
{
float radius, area: <- ;
printf(“Radius = ? “);
scanf("%f \n",&radius); <- \n not allowed
area=3.14*radius*radius;
printf("Area = %6.2f", &area); <- & not allowed
}

Question 5 [5 marks]
The volume of a solid sphere of radius r is, V = 4r 3 / 3 and the surface area is, A = 4r 2 . Write a complete C
program that reads the radius, calculates V and A, and displays any of these messages based on V and A: “V greater than
A”, “V equals A”, “V smaller than A”.

#include <stdio.h>
main()
{
float r, v, a;
scanf(“%f”, &r);
v = 4*3.14*r*r*r/3;
a = 4*3.14*r*r;
if (v>a)
printf(“V greater than A”);
else if (v == a)
printf(“V equals A”);
else
printf(“V smaller than A”);
}

Question 6 [5 marks]
Write a complete C program to display the results of the following computation, z = x 2 + y2 for all combination of x =
0.2, 0.4, 0.6, 0.8 and y = 0.25, 0.50, 0.75, 1.00. The output must display x values in the first column, y values in the
second column and z values in the third column.

#include <stdio.h>
main()
{
float z, x, y;
for (x=0.2; x <= 0.8; x = x+0.2)
{
for (y=0.25; y<=1; y = y+0.25)
{ z = x*x + y*y;
printf(“%f %f %f\n”, x, y, z) }
}
3