0% found this document useful (0 votes)
6 views9 pages

Cmp 332 Structured Programming

The document discusses structured programming, emphasizing its paradigm that promotes readable code and modular design. It outlines the benefits of good programming, rules of structured programming, and the concepts of abstraction and modularity. Additionally, it covers various programming types, principles, and the advantages and disadvantages of structured programming.

Uploaded by

giftoluwagabello
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)
6 views9 pages

Cmp 332 Structured Programming

The document discusses structured programming, emphasizing its paradigm that promotes readable code and modular design. It outlines the benefits of good programming, rules of structured programming, and the concepts of abstraction and modularity. Additionally, it covers various programming types, principles, and the advantages and disadvantages of structured programming.

Uploaded by

giftoluwagabello
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/ 9

CMP 332 – STRUCTURED PROGRAMMING

1(a) Explain the concept of structured programming paradigm.


 Structured programming is a programming paradigm that facilitates the creation of programs with
readable code and reusable components
 Encourages dividing an application program into a hierarchy of modules or autonomous elements,
which may, in turn, contain other such elements
 Code may be further structured using blocks of related logic designed to improve readability and
maintainability
 May include case, which tests a variable against a set of values; repeat, while and for, which
construct loops that continue until a condition is met.

(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.

2(a)Discuss the purpose of structured programming.


Structured programming aims to encourage the following paradigm shift in programming:
 Easy code digestion through the use of standard industry-wide coding conventions that reduce
errors arising from unstructured monolithic codes
 Team culture through appropriate division of programming tasks into several manageable chunks,
each of which can be independently undertaken by individual teams
 Development of code libraries aided by thorough code testing, which in turn shortens application
development time

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.

3(a) Using a schematic diagram, explain the execution of a while loop.

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.

(ii) Discuss the two (2) types of abstraction in structured programming.


Data Abstraction
 Data abstraction creates complex data types and hide their implementation
 Only the operations to manipulate these data types are exposed without going into the details of
their implementation
 An advantage of this approach is that the implementation can be changed anytime without changing
the behaviour that is exposed to the user

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.

Exit Control Loop:


The loops in which loop body gets executed first and then the condition is checked are called exit control
loops. They are also called post tested loops e.g., do-while

 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:

Score Point Grade

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 encourages top-down implementation, which improves both readability and maintainability of


code.

 It promotes code reuse: internal modules can be extracted and made independent, residents in
libraries, described in directories and referenced by many other applications.

 It improves development time and code quality

Disadvantages:

 Reduction in execution efficiency.

 Greater memory usage.


Both these problems arise from the introduction of calls to a module or process. All structured programming
languages are at risk to "over-structuring" and loss of efficiency

 Selecting the wrong type of structure for the task at hand.


E.g., RPL is an efficient way to state and solve a math problem as it eliminates the need to explicitly state
execution order and eliminates recursion in code. However, posing such problem in structured
programming procedural or object form, the resulting code would be much less efficient than the RPL
version

(ii) Write a program in C++ to illustrate the concept of control abstraction.

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

ii. Object-oriented programming (OOP)


 Defines a data resource and sends it to process commands. E.g., procedural programmer might say
"Print(object)" while OOP might say "Tell Object to Print".)
 Defines a program as a set of objects or resources to which commands are sent

iii. Model-based programming


 Example of this is database query languages.
 Units of code are associated with steps in database access and update or run when those steps occur
 The database and database access structure will determine the structure of the code
 Another example of a model-based structure is Reverse Polish Notation (RPN), a math-problem
structure that lends itself to efficient solving of complex expressions and Quantum computing

5(b) Discuss the concept of modules as a key component of structured programming.


Modules in structured programming are self-contained, reusable units of code designed to perform specific
tasks, promoting clarity, maintainability, and reusability. They follow the principle of encapsulation by
hiding implementation details while exposing only necessary interfaces, ensuring that each module has a
single responsibility. By breaking down a program into independent modules, structured programming
reduces complexity, enhances debugging, and allows for easier collaboration and scalability. Examples
include functions in C or classes in Python, where each module handles a distinct part of the program logic
while minimizing dependencies on other modules.

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

(ii) Open Close


Objects or entities should be open for extension but closed for modification; i.e., a class or method should
be extendable without modifying the class or method itself.
(iii) Liskov Substitution
every subclass or derived class should be substitutable for their base or parent class.
(iv) Interface Segregation
A client should never be forced to implement an interface that it doesn’t use, or clients shouldn’t be forced
to depend on methods they do not use.
(v) Dependency Inversion
Entities must depend on abstractions, not on concretions. It states that the high-level module must not
depend on the low-level module, but they should depend on abstractions.

COMPILED BY EWE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy