0% found this document useful (0 votes)
67 views12 pages

July 2019 Structured Marking Scheme

Uploaded by

bwibo Michael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views12 pages

July 2019 Structured Marking Scheme

Uploaded by

bwibo Michael
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

STRUCTURED PROGRAMMING JULY 2019

ANSWERS
1(a) (i)
• much easier to learn compared to machine language
• Coding took less time than coding using machine language
• Error correction was less cumbersome
(ii) Procedural languages
They require the programmer to specify step by step how the computer will accomplish a
specific task. Program execution follows the exact sequence laid down by the programmer
during coding
Non procedural
They allow the programmer to specify the desired result without having to specify the detailed
procedure needed to achieve the result

1(b)
The main problem is identified first and its outline developed. This enables the programmer to
work from thee known to unknown.
The management has specific goals that must be met by the programmer when the problem to be
solved is known in advance
1(c)
Step 1-sqr (a)=sqr(4)=16
Step 2-b*c=6*10=60
Step 3-6/d=6/3=2
Step 4-b*c mod 4/d=60/20=0
Step 5-sqr (a) + ((b*c) mod (6/d) =16+0=16

Michael Bwibo 0725327856 Page 1 of 12


1d)

2: (a) (i)
A user-defined data type (UTD) is a data type that derived from an existing data type
(ii) record/struct
Type
Patient=record
Patient_ no, age: integer
Patient_ name, gender: string;
End.

Michael Bwibo 0725327856 Page 2 of 12


Q2B (i)
Push () - add an item into the stack
Pop () - remove an item from the stack
Empty () - check whether the stack is empty
(II)
Queue Linked list
It is a dynamic data structure that is made up It is a data structure consisting of a group of
of elements whose position in the queue is nodes which together represent a sequence.
determined by the order of entry Under the simplest form, each node is
composed of data and a reference(a link) to
the next node in the sequence

C:
#include<stdio.h>;
#include<stdlib.h>;
Int main();
{
Int points
Printf(“enter the points obtained by a student”);
Scanf(“%d”,,&points);
Switch (points);
{
Case 1:printf(“distinction”)
Break;
Case 2: printf(“credit”)
Break;
Case 3: printf (“pass”)
Break;
Default: printf(“fail”);
Break
}

Michael Bwibo 0725327856 Page 3 of 12


Return 0
}
3. (a) i) Explain two characteristics of algorithm.
• precision –the steps are precisely stated (defined)
• Uniqueness –results of each are uniquely defined and only depend on the input and the
result of the preceding steps.
• Finiteness –the algorithm stops after a finite number of instructions are executed.
(ii) Write an algorithm that could be used to implement a quick sort.
Algorithm quicksort (A,lo,hi)is
If lo <hi then
P:=partition (A,lo,hi)
Quicksort (A, lo,p-1)
Quicksort (A,p+1, hi)
Algorithm partition (A, lo, hi) is
Pivot; =A[hi]
I: =lo-1
For j: =lo to hi- 1do
If A[j]<pivot then
I:=i|+1
Swap A[i] with A [j]
Swap A[i+1 with A[hi]
Return i+1
3 (b). write a program in c language that would prompt a user to enter .the program then checks
wether the integer entered is a prime number or not and displays an appropriate message .
# include <stdio.h>
Main ()
{
Int num, index =2;
Printf (“enter num,ber to check if it is a prime\n”);
Scanf (“%d”, &num);
For (index =2; index <=num -1; index++);

Michael Bwibo 0725327856 Page 4 of 12


{
If (num%index==0)
{
Printf (“%d is not prime \n, num”, num);
Break;
}
}
If (index ==num);
Printf (“%d is prime \n);
Return 0;
}

3(.c)
Program gets sum (input, output);
Var num, sum value: integer;
Begin
Sum: =0;
Writeln (‘enter a positive integer);
Readln (num);
For value: =0 to num do
Begin
Sum: =sum +value;
End;
Writeln (‘the sum of the integer is ‘, sum );
End.
4) (a). Assembler
This is a program that translate a source program written in assembly language into its equivalent
machine code (object code)

Michael Bwibo 0725327856 Page 5 of 12


Compiler
A compiler reads through the source program statements by statement usually written in high
level programming language, converting each correct statement into multiple machine code
instructions
b) (i)
Sequential file organization
Records are stored and accessed in a particular order sorted using a key field
Random or direct fi9le organization
Records are stored randomly but accessed one after another
Indexed sequential file organization method
Records are stored in a particular order but an index is used to enable the computer to locate
individual record on the storage media.

Q4B(ii)
Line 1-calls an header file stdio.h
Line 2-creates a main function that returns an integer value
Line 3-opening braces indicating the start of the main function
Line 4-declares an array named Myarray of size 4 and initializes the array elements
Line 5-declares two variables I and j of the type integer
Line 6-creates a for loop and initializes it from 3 down to 0
Line 7-opening brace indicating the beginning of the for loop
Line 8-outputs the elements in the array in the format element[j]=j
Line 9-closing brace indicating the end of the for loop
Line 10-return specifier
Line 11-closing brace indicating the end of the main function

Q4C) (i) A comment is a programmer –readable explanation or annotation in the source code of
a computer program. They are added with a purpose of making the source code easier for
humans to understand, and are generally ignored by compilers and interpreters.

Michael Bwibo 0725327856 Page 6 of 12


ii) program carpet(input, output);
const pie=3.142;
var length, width, diameter, roomarea: integer;
CArea, EArea:real;
Begin
Writeln(‘enter the width and length of the room’);
Readln(length, width);
Writeln(‘enter the diameter of the carpet’);
Readln(diameter);
RoomArea:=length*width;
CArea:=pie*sqr((diameter/2));
EArea:=RoomArea-CArea;
Writeln(‘the area not covered by carpet is’,EArea:5:2);
End.

5a(i)A module is an independent component/part of a program meant to solve a particular


problem. In some programming languages it is referred to as a subprogram or a subroutine.
(ii)
Function Procedure
Returns a value to the calling program Its not a must to return to the calling program
Can have only input parameters Has both input and output parameters
Can be called from precedence Can not be called from a function

5b)
procedure bubble sort(list: array of items
loop=list.count;
for i=0to loop-1 do:
swapped=false
for j=0 to loop-1do:
/*compare the adjacent elements*/
If list[j]>list[j+1]then

Michael Bwibo 0725327856 Page 7 of 12


/*swap them*/
Swap(list[j],list[j+1])
Swapped=true
End if
End for
/*if no number was swapped that means array is sorted now, break the loop.*/
If (not swapped)then
Break
End if
End for
End procedure return list

5c)put() writes a character to a file


fprintf() writes a set of data to a file

5d)#include <stdio.h>
#include<stdlib.h>
Int main()
{
Int I,j;
For(i=4;I<=6;i++)
{
For(j=4;j<=I;j++)
Printf(“%d\t”,j);
Printf(“\n”);
}
Return 0;
}
6 (a)To improve the speed of execution
To improve memory utilization
To reduce the lines of code

Michael Bwibo 0725327856 Page 8 of 12


6(b)i)User manual-is a document that guides the user of a program on how to use the program. It
may include installation procedure, error handling procedure etc
Technical manual-Is a document intended to other programmers. It may include design of the
program to guide them on program maintence.
ii)writeln()-function outputs the content and moves the cursor to a new line
write()-function outputs the content and does not move the cursor to new line.
6(c) (i) Peter

Tom
George

Joan Wayne
Beatrice Ray

ii) Beatrice, George, Joan, Peter ,Ray ,Tom, Wayne

6d)program password;
var code,x:interger;
begin
for x:=1 to 3 do
begin
writeln(‘enter your code’);
readln(code);
if code=1234 then
writeln(‘welcome’);
break;
else
writeln(‘ the code is incorrect’);
end;
end.

Michael Bwibo 0725327856 Page 9 of 12


7 a) I A break statement when encountered inside a loop it immediately terminates the loop and
the program control resumes at the next statement following the loop.
ii) the continue statements is used in a loop to force the next iteration of a loop to take place.

b) the realloc () function is used to allocate memory and has the following prototype
The free() function is used to release memory that had previously allocated using the malloc().

c) It can be applied to files of any size.


Reading of the input during the run-creation step is sequential (not much seeking)
Reading through each run during merging and writing the sorted record is also sequential. The
only seeking necessary is as we switch from run to run.

d) program mrarray;
var
a:array [0..3,0..3] of integer;
I,j:integer
Begin
For i:=0 to 3 do
For j:=0 to 3 do
A[I, j] :i* j;
For i:= 0 to 3 do
Begin
For j:= 0 to 3 do
Write (a[I, j]: 2,);
Writeln;
End;
End.

8(a) (i) indentation


White spaces
Variables with meaningful names

Michael Bwibo 0725327856 Page 10 of 12


ii) \t
b) (i) ‘\a’ is an ASCII BEL character it is a terminal feature it produces a “beep” sound or a notification
sound simultaneously displaying a terminal or command prompt(windows)

C(i) Stack underflow happens when we try to pop (remove) an item from the stack, when
nothing is actually there to remove. This will raise an alarm of sorts in the computer because we
told it to do something that cannot be done.

Stack overflow happens when we try to push one more item onto our stack than it can actually
hold. You see, the stack usually can hold only so much stuff. Typically, we allocate (set aside)
where the stack is going to be in memory and how big it can get. So, when we stick too much
stuff there or try to remove nothing, we will generate a stack overflow condition or stack
underflow condition, respectively.

C(ii) Different methods of Error handling in C

1. Global Variable errno: When a function is called in C, a variable named as errno is


automatically assigned a code (value) which can be used to identify the type of error that
has been encountered. Its a global variable indicating the error occurred during any
function call and defined in the header file errno.h.
Different codes (values) for errno mean different types of errors. Below is a list of few
different errno values and its corresponding meaning:

Michael Bwibo 0725327856 Page 11 of 12


2. perror() and strerror(): The errno value got above indicate the types of error encountered.
If it is required to show the error description, then there are two functions that can be used
to display a text message that is associated with errorno. The functions are:
o perror: It displays the string you pass to it, followed by a colon, a space, and then
the textual representation of the current errno value.
Syntax:
o void perror (const char *str)
o str: is a string containing a custom message
to be printed before the error message itself.

o strerror(): returns a pointer to the textual representation of the current errno value.
Syntax:
o char *strerror (int errnum)
errnum: is the error number (errno).

3. Exit Status: The C standard specifies two constants: EXIT_SUCCESS and


EXIT_FAILURE, that may be passed to exit() to indicate successful or unsuccessful
termination, respectively. These are macros defined in stdlib.h.

Q8 d
Program One;
Var status:string;
Begin
Writeln(‘Enter the status=’);
Readln(status)
If (status=’orphan’) then
Writeln(‘Amount Allocated is Ksh 15,000’)
elseIf (status=’needy’) then
Writeln(‘Amount Allocated is Ksh 13,000’)
elseIf (status=’Affirmative Action’) then
Writeln(‘Amount Allocated is Ksh 13,000’)
Else
Writeln(‘Amount Allocated is Ksh 0’).
Readln;
End.

Michael Bwibo 0725327856 Page 12 of 12

You might also like