Structured Programming July 2019
1. Write a program in C language that would prompt a user to enter an integer and determine whether the integer entered is a prime number or not and display appropriate messages.
ANSWER:
#include
int main() {
int n, i, flag = 0;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);
for (i = 2; i <= n / 2; ++i) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
printf(“1 is neither prime nor composite.”);
}
else {
if (flag == 0)
printf(“%d is a prime number.”, n);
else
printf(“%d is not a prime number.”, n);
}
return 0;
}
2. Write a program in Pascal language that prompts a user to enter a positive integer. The program then computes the sum of all integers from 1 to the integer.
ANSWER:
program SumOfIntegers;
var
n, sum, i: integer;
begin
sum := 0;
writeln(‘Enter a positive integer: ‘);
readln(n);
for i := 1 to n do
sum := sum + i;
writeln(‘Sum of integers from 1 to ‘, n, ‘ is: ‘, sum);
end.
3. Outline two types of utility programs used in program translation.
ANSWER:
Compiler: Converts the entire source code of a program into machine language before execution.
Interpreter: Converts high-level language into machine code line by line during program execution.
4. An author created a program that would save data into a file. Describe the steps followed in writing to the data file.
ANSWER:
Open the file for writing.
Write the data to the file using appropriate file handling functions.
Close the file after the operation is complete.
5. The following is a C language program. Study and use it to answer the question that follows.
#include
int main() {
int A[5] = {1, 2, 10, 20, 40};
int K = 2;
printf(“%d”, A[K]);
return 0;
}
Interpret the program line by line.
ANSWER:
The program declares an array A of five integers with the values {1, 2, 10, 20, 40}.
It declares an integer variable K and initializes it to 2.
It prints the value of A[2], which is the third element of the array, i.e., 10.
6. Describe the term reserved word used in Pascal programming language.
ANSWER: A reserved word is a keyword in Pascal that has a special meaning and cannot be used for variable names, e.g., begin, end, program.
7. Figure 1 shows the three rows of a rectangular table that has been fitted with a circular carpet. Use the figure to answer the question that follows.
(Skip question as per instruction.)
8. Write a program in Pascal language to compute the area not covered by the carpet.
(Skip question as per instruction.)
9. Explain the term module as used in programming.
ANSWER: A module is a separate and self-contained unit of code that can perform a specific task and can be integrated with other modules to form a complete program.
10. State the difference between a function and a procedure as used in Pascal language.
ANSWER:
A function returns a value after execution, while a procedure does not return a value.
A function is called within expressions, while a procedure is called as a standalone statement.
11. Write a procedure that could be used to sort items in a list using bubble sorting technique.
ANSWER:
Certification
procedure BubbleSort(var A: array of integer; N: integer);
var
i, j, temp: integer;
begin
for i := 0 to N-1 do
for j := 0 to N-i-2 do
if A[j] > A[j+1] then
begin
temp := A[j];
A[j] := A[j+1];
A[j+1] := temp;
end;
end;
12. Outline the use of each of the following C language file functions: (a) fopen() (b) fgets() (c) fputs() (d) fclose()
ANSWER:
fopen(): Opens a file for reading, writing, or appending.
fgets(): Reads a string from a file.
fputs(): Writes a string to a file.
fclose(): Closes an open file.
13. Write a program in C language that could be used to generate the following output.
(Skip question as per instruction.)
14. Outline two reasons for using data structures in a program.
ANSWER:
Efficient data management: Data structures organize data to allow for efficient operations such as retrieval, insertion, deletion, and modification.
Memory optimization: Data structures help optimize memory usage by arranging data efficiently.
15. Distinguish between primitive and user-defined data structures as used in Pascal.
ANSWER:
Primitive data structures: Basic structures built into the language, such as integer, char, and boolean.
User-defined data structures: Structures created by the programmer, such as records, arrays, and sets.
16. The data items Pen, Eraser, Glue, Tape, Pencil, Ruler, Paper, Watch, Joana and Box are to be stored in a library system.
(Skip question as per instruction.)
17. Explain the use of each of the following reserved words in structured programming: (a) continue (b) break (c) const (d) record
ANSWER:
continue: Skips the remaining code in the loop iteration and moves to the next iteration.
break: Exits from a loop or switch statement.
const: Defines a constant value that cannot be changed during program execution.
record: Defines a user-defined data structure that can hold related variables of different data types.
18. Distinguish between readln and writeln functions as used in Pascal programming language.
ANSWER:
readln: Reads input from the user or a file until a newline character is encountered.
writeln: Outputs text to the console or a file and moves the cursor to a new line after printing.
19. Outline two advantages of expert sort algorithms.
ANSWER:
Faster sorting times: Expert sort algorithms like quicksort and mergesort are efficient with large datasets.
Low memory usage: Some sorting algorithms are optimized to use minimal additional memory.
20. Figure 2 shows a list of data items in a data structure. Use it to answer the question that follows.
(Skip question as per instruction.