Upload
franklin-carter
View
214
Download
0
Embed Size (px)
DESCRIPTION
Question 2 Write the output of the following program. #include int main() { int i, j, sum=0; for(i = 3 ; i < 17 ; i += 7) { for(j = 9 ; j > 2; j -= 3) { printf("%d x %d =%d ",i,j,i*j ); sum += i + j; } printf("The sum is %d ",sum); return 0; } [8 marks]
Citation preview
Principle Prog Revision
Question 1(a) Identify errors in the following program segment and how the errors
can be corrected.void main(){ constant int INITIAL = 0; int auto,num1,num2; num1 = 2 % INITIAL; printf(“The value of INITIAL is &d\n”,INITIAl) } (b)Write the C codes to perform the following tasks:Assigning an integer value 5 to a variable named numberTemp. [1 mark]Assigning a value 0 to a variable named numberConst using #define
preprocessor directive. [1 mark]Display character ‘A’ using placeholder %c. [1 mark]
Question 2Write the output of the following program.#include<stdio.h>int main(){
int i, j, sum=0;for(i = 3 ; i < 17 ; i += 7){
for(j = 9 ; j > 2; j -= 3){
printf("%d x %d =%d\n",i ,j ,i*j );sum += i + j;
}}printf("The sum is %d\n",sum);return 0;
}[8 marks]
Question 3(a) List THREE (3) types of selection control structure in C programming
language? [3 marks]
(b) Study on the following code: #include <stdio.h>int main(){
int marks; printf("Enter marks: "); scanf("%d", &marks); //get marks if (marks >50) // if marks >50 display message
printf("You have passed the exam!");return 0;
}Modify program above, so that it displays two messages “You have failed
the exam!” and “Please study to improve your grade”, if marks are less than or equal to 50. [5 marks]
Question 4(a)What is infinite loop? Give one example? [3 marks](b)Study the code below and determine the exact output when
this program is executed. #include <stdio.h>int main(){ int marks; for (int cnt = 0; cnt <=5; cnt=cnt + 1) {
if (cnt != 3) continue; printf("cnt = %d\n",cnt);
}printf(“for loop process\n”);return 0;
} [2 marks]
(c) Study on the following table:
Link each of the above repetition type to its appropriate feature.
Repetition
types
Features
(I) do…while (A) The statement(s) inside body of loop will
always get executed
(I) while (A)The statement(s) inside body of loop will be
executed once no matter what
(III) for (C) It has three expressions and each expression is
separated by a semicolon (;)
Question 5(a) What is the output of the following program segment? int main(void){
int a = 10, b = 2, c = 5, d;d = compute(a, b, c);printf (“Result of compute = %d\n”, d);return 0;
} int compute(int m, int x, int c){ return m*x + c;} [3 marks]
(b) What is the output of the following program segment? int main(void){
int num1 = 100, num2 = 200;printf (“num1 = %d, num2 = %d\n”, num1, num2);
myFunc(num1, num2);printf (“num1 = %d, num2 = %d\n”, num1, num2);return 0;
} void myFunc(int num1, num2){
num1 = num1 + 5;num2 = num2 – 10;printf (“num1 = %d, num2 = %d\n”, num1, num2);
} [6 marks]
Question 6The program below is demonstrating how to calculate sum of numbers that
could be divided by 7 in an array that consists of 20 numbers. Please fill in the EIGHT (8) blanks as labeled (i) to (viii) below:
#include<stdio.h>int main(){
int i,a[20],sum__[i]__;
for(i=__[ii]__; i<_[iii]__; i__[iv]___) {
printf("Please enter number:"); scanf("__[v]____",__[vi]____); if(__[vii]______==0) sum=__[viii]_____
}printf("The sum is %d\n",sum);return 0;
}
The C program below makes use of THREE (3) functions: getNumbers(), printNumbers() and sumNumbers().
int main(void){ int myArray[100], numint, result; printf(“How many numbers to be entered? ”); scanf(“%d”, &numint); getNumbers(myArray, numint); printNumbers(myArray, numint); result = sumNumbers(myArray, numint); printf(“Sum of numbers entered = %d”, result); return 0;}Based on the program written above, write down the function prototypes for the THREE (3) functions.[3
marks] (a) Write the function getNumbers(). This function is supposed to ask user for a list of integers and store
them in the array passed as the first argument. The number of integers to be entered is passed as the second argument. [4 marks]
(b) Write the function printNumbers(). This function is supposed to print out the integers inside the array passed in the first argument. The number of integers stored in the array is passed as the second argument. [4 marks]
(c) Write the function sumNumbers(). This number is supposed to sum up all the integers inside the array passed as the first argument. The number of integers stored in the array is passed as the second argument. The function must return an integer which is the result of the summation. [4 marks]