Structured Programming Revision Kit
Structured Programming Revision Kit
1. Prove that the correct output is produced if the program successfully completes.
2. Compiling and Executing.
White box and / or Black box testing Only Black box testing techniques are
techniques are involved involved
Build released for Alpha Testing is called Build released for Beta Testing is called
Alpha Release Beta Release
4) Beta Testing
Beta Testing is a formal type of software testing which is carried out by the customer. It is
performed in Real Environment before releasing the product to the market for the actual end
users. Beta testing is carried out to ensure that there are no major failures in the software or
product and it satisfies the business requirements from an end-user perspective. Beta testing is
successful when the customer accepts the software.
-Outline a function for each of the following software as used in programming. (2 marks)
a) Editor; ii. Compiler
A source code editor is a text editor program designed specifically for editing source code of
computer programs. It may be a standalone application or it may be built into an integrated
development environment (IDE) or web browser.
-Explain the role of each of the following header files in a C program. (2 marks)
i. Stdio.h; ii Math.h.
Stdio.h is the header file for standard input and output. This is useful for getting the input from
the user (Keyboard) and output result text to the monitor (screen). Without this header file, one
cannot display the results to the users on the screen or cannot input the values through the
keyboard.
The math.h header defines various mathematical functions and one macro. All the functions
available in this library take double as an argument and return double as the result. Let us
discuss some important functions one by one.
A flowchart can be incredibly difficult to interpret and only captures the direction of
your code, not the more difficult thought process.
-Draw a flow chart for a C program that will print Odd numbers from 2 to 6. (4 marks)
-Define the term variable scope and explain the two types of variable scope. (4 Marks)
Scope refers to the visibility of variables. In other words, which parts of your
program can see or use it. Normally, every variable has a global scope. Once
defined, every part of your program can access a variable.
The scope of the local variable- the function where that var was defined. Any
code inside that function can access (read and change) this variable. Any
code outside it cannot. It's local, so it's invisible from outside. A variable is said to
have global scope if they can be used anywhere but within the same class in which
it is declared. An instance variables are the variables that have global scope.
-Explain two approaches to programming that are used to implement stepwise refinement.
In this style of programming, there is a great risk that implementation details of many data
structures have to be shared between modules, and thus globally exposed. This in turn makes it
tempting for other modules to use these implementation details, thereby creating unwanted
dependencies between different parts of the application.
Modular programming is the process of subdividing a computer program into separate sub-
programs.
A module is a separate software component. It can often be used in a variety of applications and
functions with other components of the system. Similar functions are grouped in the same unit
of programming code and separate functions are developed as separate units of code so that the
code can be reused by other applications.
-Outline three circumstances that would make a programmer to use a compiler during program
writing. (3 marks)
Compiling
Compiling is the process of transforming a high level language into a low level
language. A high level language is closer to English. A low level language is
closer to what the computer understands.
Compiling a C Program
-A program is required to prompt a user to enter two non-zero integers. The program then checks
and outputs the larger integer.
Write a pseudo code and draw the flowchart to represent the logic. (4 marks)
Start
else:
print(input_one + “ is the largest number”)
Stop
Using a diagram, explain the two types of connectors as used in flow charts (2 marks)
Off-page A labeled connector for use when the target is on another page.
Connector Represented as a home plate-shaped pentagon.
-Write operator order of precedence for evaluating mathematical expressions as used in Pascal
programming. (3 marks)
~, not, Highest
|, !, +, -, or,
Z=a+b % c * (d^2)
Given that a=10, b=23, c =7 and d=5. (4 marks)
=10+23%7*(5*5)
=10+2*(25)
=10+50
=60
-Given that x is a variable in C program that stores a numeric value, distinguish between x++ and
++x as used in the program operations. (4 marks)
++x i.e. pre-increment operator uses the principle ‘change-then-use’ while, x++ i.e.
post-increment operator uses the principle ‘use-then-change’.
4 It makes debugging far easier in another way, because if you accidentally misspell a variable
. name somewhere, this will be picked up as an undeclared variable if you compile your project
(Debug menu).
-Using the syntax, differentiate between float and int data types with examples in C. (4 marks)
Integers and floats are two different kinds of numerical data. An integer (more commonly
called an int) is a number without a decimal point. A float is a floating-point number, which
means it is a number that has a decimal place. Floats are used when more precision is needed.
int a = 0; // Create a variable "a" of the datatype "int"
float b = 0.0; // Create a variable "b" of the datatype "float "
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition,
subtraction and multiplication on numerical values (constants and variables).
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most
common assignment operator is =
C Relational Operators
C Logical Operators
Translates high-level
Temporarily executes high-level Translates low-level assembly
languages into machine
languages, one statement at a time code into machine code
code
An executable file of
No executable file of machine code is An executable file of machine
machine code is
produced (no object code) code is produced (object code)
produced (object code)
Code description
Comments can be used to summarize code or to explain the programmer's intent.
Algorithmic description
Sometimes source code contains a novel or noteworthy solution to a specific problem. In such
cases, comments may contain an explanation of the methodology. Such explanations may
include diagrams and formal mathematical proofs.
-Write a Pascal program that would accept a positive integer. The program should then
determine the square and cube of the value and output the results. (6 marks)
Program PRODNUM;
Var
Num1, square, Prod : Integer;
Begin {no semicolon}
Writeln('Input number 1:');
Readln(Num1);
square := Num1 * Num1;
cube := Num1 * Num1 * Num1;
Writeln('The square of', Num1’, square);
Writeln('The square of', Num1’, cube);
Readln;
End.
-Write a C program that accepts the base and height of a right angled triangle and also computes
and outputs the area of the triangle. (5 marks)
1. /*
2. * C Program to Find Area of a Right Angled Triangle
3. */
4. #include <stdio.h>
5. int main()
6. {
-Write a C program that prompt a user to enter three integers and displays the largest of them.
Make use of ternary operator. (5 marks)
#include <stdio.h>
main()
{
int a, b, c, big ;
printf("Enter three numbers :\n") ;
scanf("%d %d %d", &a, &b, &c) ;
big = (a < b) ? (a < c ? a : c) : (b < c ? b : c) ;
printf("\nThe smallest number is : %d\n", big) ;
system ("Pause");
return 0;
}
-Write a C code that determine and display the area the area of the trapezium.
Hint: Area of Trapezium = ½ (a+b)h (6 marks)
1. /*
2. * C Program to Find Area of Trapezium
3. */
4. #include <stdio.h>
5. int main()
6. {
7. float a, b, h;
8. float area;
9. printf("Enter the value for two bases & height of the trapezium: \n");
10. scanf("%f%f%f", &a, &b, &h);
11. area = 0.5 * (a + b) * h ;
-Write a C program that would accept a positive integer. The program should then determine the
square of the number and display the number and its square. (5 marks)
/* C Program to Calculate Square of a Number */
#include<stdio.h>
int main()
{
int number, Square;
return 0;
Solutions
Solutions to abnormal program terminations vary based on the source of the error. If a software
conflict is at fault, identifying the two pieces of software and avoiding running them
simultaneously is the best course of action. A good way to identify if a software or driver conflict
is at fault is to perform a clean boot, which will start your computer with minimal drivers and
auto-launching applications. Instructions for performing a clean boot are included in the
Resources section. If other resource errors are at fault, you may need to consult your company's
IT professional for a case-specific plan. If you suspect a software glitch, consider uninstalling the
software and performing a clean re-install. This may remedy issues with corrupted files and other
faults that have occurred over time. Software that is inherently defective will likely need to be
replaced with a comparable program.
Causes of run time error.
• division by zero
• performing an operation on incompatible types
• using an identifier which has not been defined
• accessing a list element, dictionary value or object attribute which doesn’t exist
• trying to access a file which doesn’t exist
-A newly constructed computer lab is 15metres long and 10 meters wide. The lab is to be fitted
with floor tiles. Write a program in C language that prompts a user to enter the length and width
of the lab. The program then computes the number of the tiles required. One tile has an Ares of
0.36M2. (6 marks)
#include<stdio.h>
#include<conio.h>
int main() {
int length, breadth, area,tiles;
-Write a C program that prompt a user to enter three integers and displays the largest of them.
Make use of ternary operator. (5 marks)
# include <stdio.h>
void main()
{
int a, b, c, big ;
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
big = a > b ? (a > c ? a : c) : (b > c ? b : c) ;
printf("\nThe biggest number is : %d", big) ;
}
-A newly established bank converts Kenya shillings to US dollar at the rate of 1US dollar=102
Kenya shillings. Write a program in C programming language that prompts a user to enter the
currency either Ksh or USD. The program then outputs the equivalent amount. Use functions.
(6 marks)
/*Program to demonstrate the working of user defined function*/
#include <stdio.h>
float CON1(int amount); //function prototype(declaration)
float CON2(int amount); //function prototype(declaration)
int main()
{
float pesa,answer;
char opt;
printf("Enters Character D or K\n");
scanf("%c",&opt);
if (opt == 'D')
{
printf("Enters Dollar to convert to Ksh=\n");
scanf("%f",&pesa);
answer=CON1(pesa);
printf("Answer After Convertion %.2f\n",answer);
}
else
{
printf("Enters Ksh to convert to Dollar=\n");
scanf("%f",&pesa);
answer=CON1(pesa);
for the three dimensions of a triangle, computes the area and displays the results to the nearest 3
decimal places. (8 marks)
Program geo;
Var
a,b,c: Integer;
var s, ans:real;
Syntax:
if (condition1) {
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
(a) In an athletics competition, athletes were awarded money based on the ranking as shown
below.
Rank Amount Allocated
1 1000000
2 500000
3 250000
Any Other 0
Write a C program that would accept the rank. The program should then determine the award
through the use of if statement
(6 marks)
#include <stdio.h>
int main()
{
int rank;
printf("Enter score\n:");
scanf("%d",&score);
if (score==1)
{
printf("Amount allocated is 1000000\n");
}
else if (score ==2)
else
{
printf("Amount allocated is 0\n");
}
system("pause");
return 0;
}
-Using flow charts, explain the difference between repeat until and while-do loop structures.
(4 marks)
Difference between WHILE …DO & REPEAT…….UNTIL Loop Structure
WHILE …DO
REPEAT…….UNTIL
1. The condition is tested after the body is executed.
2. The body is always executed at least once.
3. The loop is exited when the condition is true.
-Write a C program that will allow the user to specify the number of elements to enter in an array
then the program sorts the entered values in ascending order using a bubble sort algorithm.
(7 marks)
1. #include <stdio.h>
2. void main()
3. {
4. int i, j, a, n, number[30];
5. printf("Enter the value of N \n");
6. scanf("%d", &n);
7. printf("Enter the numbers \n");
8. for (i = 0; i < n; ++i)
9. scanf("%d", &number[i]);
1. [Underflow ?]
if (TOP <= 0)
write Stack is empty.
return.
2. [Deleting Element]
item <-- stack[top]
3. [Decrementing top]
top <-- top - 1
4. [FINISH]
return (top).
Arrays a kind of data structure that can store a fixed-size sequential collection of elements of
the same type. An array is used to store a collection of data, but it is often more useful to
Measuring Efficiency: The most obvious way to determine the efficiency of an algorithm, data
structure, or application is to write the necessary code and measure the running time and space
requirements.
-Write an algorithm using structured English that could be used to implement linear search in
Structured Programming. (3 marks)
Level (of a node): The number of parent nodes corresponding to a given a node of the tree. For
example, the root, having no parent, is at level 0.
TOPIC 7: SUBPROGRAMS
-Explain two ways through which parameters can be passed to a function. (4 marks)
Parameter Passing
There are different ways in which parameter data can be passed into and out of methods and
functions. It is beyond the scope of these notes to describe all such schemes, so we will consider
only the two most common methods used in C++ and Java: "pass by value" and "pass by
reference".
First some important terminology:
Definition: Formal Parameter
A variable and its type as they appear in the prototype of the function or method.
Definition: Actual Parameter
The variable or expression corresponding to a formal parameter that appears in the function or
method call in the calling environment.
Pass by Value
Using "pass by value", the data associated with the actual parameter is copied into a separate
storage location assigned to the formal parameter. Any modifications to the formal parameter
variable inside the called function or method affect only this separate storage location and will
therefore not be reflected in the actual parameter in the calling environment. For example (this
could be either C++ or Java):
void cribellum(int x) // Formal parameter is "x" and is passed by value.
{
x = 27;
}
void someCaller()
{
int y = 33;
cribellum(y); // Actual parameter is "y"
// Actual parameter "y" still has 33 at this point.
...
}
Pass by Reference
Using "pass by reference", the formal parameter receives a reference (or pointer) to the actual
data in the calling environment, hence any changes to the formal parameter are reflected in the
actual parameter in the calling environment. Here is a modified version of this example (now
strictly a C++ example since Java does not allow primitive types to be passed by reference):
void cribellum(int& x) // Formal parameter "x" is now passed by reference.
{
void someCaller()
{
int y = 33;
cribellum(y);
// Actual parameter "y" now has 27 at this point.
...
}
Recursion and iteration both repeatedly executes the set of instructions. Recursion is when a
statement in a function calls itself repeatedly. The iteration is when a loop repeatedly executes
until the controlling condition becomes false. The primary difference between recursion and
iteration is that is a recursion is a process, always applied to a function. The iteration is applied
to the set of instructions which we want to get repeatedly executed.
A linked list is a dynamic data structure and therefore the size of the linked list can grow or
shrink in size during execution of the program. A linked list does not require any extra space
therefore it does not waste extra memory. It provides flexibility in rearranging the items
efficiently.
The limitation of linked list is that it consumes extra space when compared to a array since each
node must also contain the address of the next item in the list to search for a single item in
avg = sum/4;
printf("Average of entered number is: %d", avg);
return 0;
}
• Records are stored and accessed in a particular order sorted using a key
field.
• Retrieval requires searching sequentially through the entire file record by
record to the end.
• Because the record in a file are sorted in a particular order, better file
searching methods like the binary search technique can be used to reduce
the time used for searching a file .
Almost similar to sequential method only that, an index is used to enable the
computer to locate individual records on the storage media. For example, on
a magnetic drum, records are stored sequential on the tracks. However, each
record is assigned an index that can be used to access it directly.
r Open for reading. If the file does not exist, fopen() returns NULL.
-Describe two documents which could be created during program development. (2 marks)
Internal documentation is the one that talks in detail about how the code does whatever it
function is. It describes the data structures, algorithms, and control flow in the programs.
1. Name, type, and purpose of each variable and data structure used in the code
2. Brief description of algorithms, logic, and error-handling techniques
3. Information about the required input and expected output of the program
4. Assistance on how to test the software
5. Information on the upgrades and enhancements in the program.
External documentation, on the other hand, focuses on what the problem to be solved is, and
which approach is used in solving it. This can be a graph detailing the execution, a list of
algorithms, dependencies, as well as meta-data such as the author, the date,
The group of Hellerstein addresses the challenge of consistency and parallelism in distributed
systems by applying declarative/logic programming and monotonicity analyses.