2100/201
STRUCTURED PROGRAMMING
PAPER 1
Time: 3 hours

THE KENYA NATIONAL EXAMINATIONS COUNCIL
DIPLOMA IN INFORMATION COMMUNICATION TECHNOLOGY
MODULE I
STRUCTURED PROGRAMMING
3 hours

INSTRUCTIONS TO CANDIDATES

You should have the following for this exam:
Answer booklet provided.
This paper consists of Six Questions.
Answer any Five questions.
All questions carry 20 marks.
1.

a) Explain the term modular design as used in programming.
Modular design is a programming approach that divides a program into separate sub-programs or modules, each responsible for a specific part of the overall functionality. This makes the program easier to manage, debug, and maintain. (2 marks)

b) Explain how the following basic constructs are applied when writing a computer program:

i) Sequence: This is the execution of statements in a program one after the other, in a specific order. (2 marks)

ii) Selection: This allows a program to make decisions and execute different code based on conditions (e.g., if, else). (2 marks)

iii) Repetition: This allows a set of instructions to be executed repeatedly until a certain condition is met (e.g., for, while loops). (2 marks)

c) Write a program in a Pascal language that prompts a user to enter their username. The program should then check if the username entered matches the preset value “admin”. If the username matches, the program should display the message “Welcome admin”. If it does not match, the program should display the message “Invalid username”.

program CheckUsername;
var
username: string;
begin
writeln(‘Enter your username:’);
readln(username);
if username = ‘admin’ then
writeln(‘Welcome admin’)
else
writeln(‘Invalid username’);
end.
(6 marks)

d) Explain the function of each of the following constructs as used in C programming language:

i) #include <stdio.h>: This is a preprocessor directive that tells the compiler to include the Standard Input Output library, which is essential for functions like printf and scanf. (2 marks)

ii) return 0;: This statement ends the execution of the main() function and returns a value of 0 to the operating system, indicating that the program has run successfully. (2 marks)

e) Write a program in C language that prompts a user to enter their age. The program should display the message “You are eligible to vote” if the age is 18 or above. If the age is below 18, the program should display the message “You are not eligible to vote”.

#include <stdio.h>

int main() {
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);

if (age >= 18)
printf(“You are eligible to vote\n”);
else
printf(“You are not eligible to vote\n”);

return 0;
}
(2 marks)

2.

a) Outline the importance of comments in programming.

Comments make code easier to understand by explaining the purpose of code segments.
They help in debugging by allowing developers to leave notes on why certain solutions were implemented.
Comments assist other developers who might work on the code in the future.
They can be used to temporarily disable parts of the code without deleting it. (4 marks)
b) Describe the term debugging as used in programming.
Debugging is the process of finding and fixing errors or bugs in a computer program to ensure it runs correctly and efficiently. (2 marks)

c) Write a program in C language that calculates the area of a rectangle. The program should prompt the user to enter the width and height of the rectangle, then display the area.

#include <stdio.h>

int main() {
float width, height, area;
printf(“Enter the width of the rectangle: “);
scanf(“%f”, &width);
printf(“Enter the height of the rectangle: “);
scanf(“%f”, &height);

area = width * height;
printf(“The area of the rectangle is: %.2f\n”, area);

return 0;
}
(4 marks)

d) Write a program in Pascal that calculates the factorial of a number entered by a user.

pascal
Copy code
program Factorial;
var
num, i, fact: integer;
begin
writeln(‘Enter a number:’);
readln(num);
fact := 1;
for i := 1 to num do
fact := fact * i;
writeln(‘Factorial of ‘, num, ‘ is ‘, fact);
end.
(6 marks)

e) Explain the difference between syntax errors and logical errors in programming.

Syntax Errors: These are mistakes in the code that violate the rules of the programming language, such as missing semicolons or misspelled keywords. The program will not run until these errors are corrected.
Logical Errors: These occur when the code runs without syntax errors but produces incorrect results due to a flaw in the logic, such as incorrect use of operators or control structures. (4 marks)
3.

a) State three advantages of high-level programming languages.

Easier to read, write, and maintain compared to low-level languages.
Abstracts the complexity of machine code, allowing developers to focus on logic rather than hardware specifics.
Portable across different computer systems with little or no modification. (3 marks)
b) Differentiate between Source code and Object code as used in C language.

Source Code: The human-readable code written by a programmer in a high-level language before it is compiled.
Object Code: The machine-readable code that is generated by the compiler after the source code has been processed. (3 marks)
c) The following table shows variables used in a C language program:

Variable Name Type Initial Value
height float 0.0
age int 0
gender char ‘\0’
Write a C program that uses the above variables to calculate and display the BMI of a person using the formula:
BMI = weight / (height * height).

c
Copy code
#include <stdio.h>

int main() {
float weight, height, BMI;
printf(“Enter your weight in kilograms: “);
scanf(“%f”, &weight);
printf(“Enter your height in meters: “);
scanf(“%f”, &height);

BMI = weight / (height * height);
printf(“Your BMI is: %.2f\n”, BMI);

return 0;
}
(8 marks)

d) Explain the term Scope of a variable as used in programming.
The scope of a variable refers to the region or part of a program where the variable is accessible. Variables can have local scope (only accessible within a specific function or block) or global scope (accessible throughout the entire program). (3 marks)

e) List three basic data types used in Pascal programming language.

Integer: Used for whole numbers.
Real: Used for floating-point numbers (numbers with decimals).
Char: Used for single characters. (3 marks)
4.

a) Explain the purpose of the following statements in a C program:

i) for: A loop that repeats a block of code a specified number of times, using a counter variable. (2 marks)

ii) while: A loop that continues to execute as long as a given condition is true. The condition is checked before the loop begins each iteration. (2 marks)

iii) do-while: Similar to a while loop, but the condition is checked after the loop has executed, ensuring that the loop runs at least once. (2 marks)

b) Write a C program that calculates and displays the sum of the first 10 odd numbers.

c
Copy code
#include <stdio.h>

int main() {
int i, sum = 0;
for (i = 1; i <= 19; i += 2) {
sum += i;
}
printf(“The sum of the first 10 odd numbers is: %d\n”, sum);

return 0;
}
(6 marks)

c) Identify and explain any four library functions used in C programming language.

i) printf(): Used to print formatted output to the screen.
ii) scanf(): Used to take input from the user.
iii) strlen(): Returns the length of a string.
iv) strcpy(): Copies one string to another. (8 marks)
5.

a) Explain the concept of arrays as used in programming.
An array is a data structure that stores a collection of elements, typically of the same data type, in a contiguous block of memory. Each element in an array can be accessed using an index. (4 marks)

b) Write a C program that stores and displays 10 integers in an array. The program should calculate and display the sum of the elements in the array.

#include <stdio.h>

int main() {
int arr[10], i, sum = 0;

printf(“Enter 10 integers:\n”);
for (i = 0; i < 10; i++) {
scanf(“%d”, &arr[i]);
sum += arr[i];
}

printf(“The sum of the elements is: %d\n”, sum);
return 0;
}
(6 marks)

c) Describe the differences between a one-dimensional array and a two-dimensional array.

One-dimensional array: A linear structure that stores a list of elements, accessible with a single index.
Two-dimensional array: A table-like structure that stores elements in rows and columns, accessible with two indices. (4 marks)
d) Explain the term recursion as used in programming and provide an example.
Recursion is a programming technique where a function calls itself in order to solve a problem. It continues to call itself with a smaller portion of the problem until it reaches a base case. For example, calculating the factorial of a number:int factorial(int n) {
if (n == 1)
return 1;
else
return n * factorial(n – 1);
}
(6 marks)

6.

a) Write a program in Pascal that uses a for-loop to calculate and display the factorial of numbers from 1 to 10.

pascal
Copy code
program FactorialTable;
var
i, j, fact: integer;
begin
for i := 1 to 10 do
begin
fact := 1;
for j := 1 to i do
fact := fact * j;
writeln(‘Factorial of ‘, i, ‘ is ‘, fact);
end;
end.
(8 marks)

b) Explain the term pointers as used in C programming language.
Pointers are variables in C that store the memory address of another variable. They are used for dynamic memory allocation, passing functions by reference, and handling arrays and strings efficiently. (4 marks)

c) Write a C program that sorts an array of integers in ascending order using a selection sort algorithm.

#include <stdio.h>

int main() {
int arr[5], i, j, minIndex, temp;

printf(“Enter 5 integers:\n”);
for (i = 0; i < 5; i++) {
scanf(“%d”, &arr[i]);
}

for (i = 0; i < 4; i++) {
minIndex = i;
for (j = i + 1; j < 5; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}

printf(“Sorted array in ascending order:\n”);
for (i = 0; i < 5; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);

return 0;
}
(8 marks)

END OF PAPER