Nick Cat I_structured Programming

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

A)Differentiate between the following pairs of terms:

i. Source code and object code


ii. Pseudo code and flowcharts
i. Source code and object code:

.Source code is the original, human-readable programming instructions that a programmer writes to
create a program or application. It is written in a high-level programming language, such as Java, Python,
or C++, and is used to instruct a computer on how to perform specific tasks. On the other hand, object
code is the machine-readable version of the source code, which has been translated into a lower-level
language that a computer can understand and execute. Object code is specific to the computer
architecture and operating system, and is used to create an executable program that can be run on a
computer.

ii. Pseudo code and flowcharts:

Pseudo code is a way of representing an algorithm or program in a human-readable form that is not
specific to any programming language. It uses natural language-like statements to describe the steps and
logic of a program, making it easier for programmers and non-programmers alike to understand and
analyze the process. Pseudo code is often used as a first step in the programming process, before the
actual implementation in a specific programming language.

B) Describe any three types of errors in C giving an example in each case (3marks)

(i) Syntax Errors: These occur when the code violates the syntax rules of the C programming language.
For example, if you forget to close a parenthesis or use the wrong keyword, it can result in a syntax error.

(ii) Logical Errors: These occur when the code does not produce the expected output due to a logical
mistake. For example, if you write a program to calculate the sum of two numbers but forget to add the
second number, it will result in a logical error.

(iii) Runtime Errors: These occur when the program encounters an error while it is running. For example,
if you try a number by zero, it will result in a runtime error.

C)Write a program that calculates the area and the perimeter of a rectangle given that the
length and the width are entered from the keyboard.The results should be outputted on the screen.

#include
#include

int main() {

float length, width, area, perimeter;

// Input the length and width of the rectangle

printf("Enter the length: ");

scanf("%f", &length);

printf("Enter the width: ");

scanf("%f", &width);

// Calculate the area and perimeter of the rectangle

area = length * width;

perimeter = 2 * (length + width);

// Output the results

printf("The area of the rectangle is: %.2f\n", area);

printf("The perimeter of the rectangle is: %.2f\n", perimeter);

return 0;

D)Define any four qualities of a good algorithm .

(i)Correctness: A good algorithm should produce the correct output for any valid input. It should follow
the specified rules and logic to solve the problem.

(i) Efficiency: A good algorithm should be efficient in terms of time and space complexity. It should use
the minimum number of resources required to solve the problem.
(iii) Simplicity: A good algorithm should be simple and easy to understand. It should be written in a clear
and concise manner, with minimal unnecessary complexity.

(iv)Readability: A good algorithm should be readable and maintainable. It should be written in a way that
makes it easy for others to understand and modify the code.

E) Write a program that calculates the area and circumference of a circle given the radius of the
circle is of float data type and is entered from the keyboard.PI is given a 3.4.The results should be output
on the screen.

#include

#include

const float PI = 3.14;

int main() {

float radius, area, circumference;

// Input the radius of the circle

printf("Enter the radius: ");

scanf("%f", &radius);

// Calculate the area and circumference of the circle

area = PI * radius * radius;

circumference = 2 * PI * radius;

// Output the results

printf("The area of the circle is: %.2f\n", area);

printf("The circumference of the circle is: %.2f\n", circumference);


return 0;

You might also like