Cmp 332 Structured Programming
Cmp 332 Structured Programming
(b) State and explain any four (4) of the benefits of good programming.
i. Readability
ii. Extensibility
iii. Maintainability
iv. Modularity
Readability: All developers were in a team able to understand the code.
Extensibility: Change requests will come at any time. So, our code is always easy to extend without
breaking existing business rules.
Maintainability: It’s easy to maintain by the development team and the production support team too
because the application is loosely coupled.
Modularity: Divide a program into reusable pieces: functions, modules, libraries.
COMPILED BY EWE
(b) State and explain any four (4) of the rules of structured programming.
DRY (Don’t-Repeat-Yourself)
A piece of logic should only be represented once in an application.
o Duplication is the root of all software evils.
o Duplication is a waste.
KISS (Keep It So Simple)
Most systems work best if they are kept simple rather than making them complex
Therefore, simplicity should be a key goal in design
Unnecessary complexity should be avoided.
YAGNI (You Aren’t Gonna Need It)
YAGNI states that "don’t implement something until it is necessary".
There are two main reasons for this:
You save time because you avoid writing code that you turn out not to need
Your code is better because you avoid polluting it with 'guesses' that turn out to be more or less
wrong but stick around anyway.
SOLID
SOLID principle supports good object-oriented design and programming.
COMPILED BY EWE
The while loop executes certain statements as long as the condition is met
Syntax
initialize the condition variable
while (condition) {
statement1;
statement2;
...
increment_statement;
}
(b) Structured programming discourages the use of goto statement. Hence, rewrite the following
pseudocode segment in either C/C++:
k = (j + 13) / 27
LOOP:
If k > 10 then goto out
k=k+1
i=3*k-1
goto LOOP
out: ...
COMPILED BY EWE
3(b) (i) Explain abstraction as applied in structured programming.
Abstraction means code simplification and cleaning in order to make programming more efficient and
effective; i.e, “displaying” only the relevant attributes of objects and “hiding” the unnecessary details
Structured programming languages use a set of rules to model real-world features such as:
Using data types to perform data abstraction, separating usage from working representations of
data structures within programs
Concept of subprograms that represent a specific way of implementing control flow in programs
Reorganizing common behavior from non-abstract classes into abstract classes using inheritance
as seen in object-oriented programming languages.
Control Abstraction
CA collects all the control statements that are a part of the application and exposes them as a unit
CA forms the main unit of structured programming
using CA simple functions can be defined in complex frameworks
COMPILED BY EWE
4(a)(i) Differentiate between entry-level and exit-level conditional structures with suitable diagrams.
Entry Level:
The loops in which condition is checked first and then the loop body get executed are called entry control
loops. They are also called pretested loops e.g., for, while.
As the program flow reaches an entry-controlled loop, the loop condition is tested before the first iteration of
the Loop.
If the condition is met, then the loop body will be executed. If it does not, the loop body will be skipped
entirely, and the program will continue execution from the first statement following the Loop.
After running the loop for the required iterations (when the condition is not met), the program exits the loop.
As the program flow reaches an exit-controlled loop, the loop body is executed first, and then the
loop condition is tested.
If the condition is met, the Loop will continue to run, and the body's code will perform again. If it
does not, the Loop will exit, and the program will continue execution from the first statement
following the Loop.
After running the loop for the required iterations (when the condition is not met), the program exits
the loop.
COMPILED BY EWE
(ii) Use a switch...case structure to implement the following table on score:
70–100 5 A
60–69 4 B
50–59 3 C
45–49 2 D
40–44 1 E
0–39 0 F
COMPILED BY EWE
4(b) (i) State two (2) advantages and disadvantages of structured programming.
The primary advantages of structured programming are:
It promotes code reuse: internal modules can be extracted and made independent, residents in
libraries, described in directories and referenced by many other applications.
Disadvantages:
COMPILED BY EWE
5(a) Briefly explain the three (3) types of structured programming available.
i. Procedural programming. Defines modules as "procedures" called with a set of parameters
to perform a task, subdivided into the following:
Service-oriented programming simply defines reusable modules as "services" with advertised
interfaces
Microservice programming focuses on creating modules that do not store data internally, and so
are scalable and resilient in cloud deployment
Functional programming modules are written from functions whose outputs are derived only
from their inputs. Designed for serverless computing but has since expanded to be largely
synonymous with microservices
COMPILED BY EWE
6. Discuss the following popular principles in structured programming
(i) Single Responsibility
any single object in a code should be made for one specific function
COMPILED BY EWE