Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

1

STPM 2011 – ICT

1. a) Write declaration statements in C for each of the variables according to the descriptions
(3) given in the table below.

Variable Description
name Character string with a maximum of 30
characters
studentNumber Beginning with two letters followed by four
digits
classCode One letter
cgpaValue CGPA value with one decimal place
semester Current semester
[5 marks]

(b) Rewrite the C statements in (a) using a structure declaration named student.
[5 marks]

Answer

a)
char name, studentNumber, classCode;
float cgpaValue;
int semester;

b)
struct student {
char name[30];
char studentNumber[6];
char classCode[1];
float cgpaValue;
int semester;
};

2. A formula to calculate the value of num is given as follows:


(4)
3x + y
num = + x
y + 3

Convert the formula to its equivalent statement in C, where variables num, x and y are
integers. [2 marks]

Using the C statement that you have obtained in (a), show the operation involved in
calculating the value of num if x = 10 and y = 5. [4 marks]

Answer
a)
int num, x, y;
num = ((3*x + y)/(y + 3)) + x;

b)
x = 10, y = 5
2

num = ((3*x + y)/(y + 3)) + x


= ((3*10 + 5)/(5 + 3)) + 10
= ( 35 / 8) + 10
= 4 + 10
num = 14

3. Draw a flow chart which reads and prints the first number that is multiple of 5 and is greater
(5) than 500. Assume that the number is named multipleNum and an initial value of integer 5 is
read. Hence, state the value of multipleNum. [7 marks]

Answer
#include <stdio.h>
void main() {
int multipleNum, i;

multipleNum = 5;

for(i=1; i <= 5; i++) {


multipleNum *= 5;
if (multipleNum >= 500)
printf("%d ", multipleNum);
}
}

4. An element in an array is referred to by the name of the array followed by a subscript. Thus, the
first element of an array has a subscript zero.

Write statement(s) in C to accomplish each of the following:


a) Print the value of the eighth element of a one-dimensional character array A.
[1 marks]
b) Read a value into the eleventh element of a one-dimensional floating point array B.
[1 marks]
c) Initialise all the elements of a one-dimensional 10-element integer array C to 10.
[2 marks]
d) Copy the elements of an array D into an array E, where each of the arrays is a one-
dimensional 12-element integer array.
[2 marks]
e) Find and print the smallest and the largest values in a one-dimensional 30-element integer
array F.
[5 marks]

Answer
a)
printf(“%c”, A[7]);

b)
scanf(“%f”,&B[10]);
3

c)
for (i = 0; i < 10; i++)
C[i] = 10;

d)
for (j = 0; j < 12; j++)
E[j] = D[j];

e)
#include <stdio.h>

void main ()
{
int F[30], i, SmallestValue, LargestValue;

for (i=0; i<30; i++)


scanf ("%d",&F[i]);
SmallestValue = F[0];
for (i=1; i<30; i++) {
if(SmallestValue > F[i])
SmallestValue = F[i];
}
LargestValue = F[0];
for (i=1; i<30; i++) {
if(LargestValue < F[i])
LargestValue = F[i];
}
printf("SmallestValue : %d\n", SmallestValue);
printf("LargestValue : %d\n", LargestValue);
}

You might also like