0% found this document useful (0 votes)
15 views8 pages

Document 9

The document discusses two programming approaches: top-down and bottom-up. The top-down approach breaks complex algorithms into smaller modules, while the bottom-up approach builds from basic components to create a complete system. It also covers programming concepts such as sequence, selection, iteration, functions, and variable scope, along with examples and a test plan for user-defined functions.

Uploaded by

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

Document 9

The document discusses two programming approaches: top-down and bottom-up. The top-down approach breaks complex algorithms into smaller modules, while the bottom-up approach builds from basic components to create a complete system. It also covers programming concepts such as sequence, selection, iteration, functions, and variable scope, along with examples and a test plan for user-defined functions.

Uploaded by

zainrandom0031
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Structured PROGRAM

Zain

Student Number
258826
TOP-DOWN APPROACH IN COMPUTER PROGRAMMING

A complicated algorithm is divided into smaller pieces, or "modules," in the top-down


method. After that, these modules are further divided into smaller and smaller pieces until
they can no longer be divided. We refer to this procedure as "modularization." But the
algorithm's originality and integrity must always be maintained during the modularization
process. Furthermore, when the software needs to be created from scratch and certain
details are unknown, a top-down approach makes more sense.

By dividing a larger problem into smaller parts, the top-down method reduces the usual
complexities that occur during algorithm construction. Additionally, with this method, every
function in a code is distinct and operates apart from other functions. The C programming
language makes extensive use of the top-down methodology.

Advantages:

 Breaking problems into parts help us to identify what needs to be done.


 At each step of refinement, new parts will become less complex and therefore
easier to solve.
 Parts of the solution may turn out to be reusable.
 Breaking problems into parts allows more than one person to solve the problem.

1
BOTTOM-UP APPROACH IN COMPUTER PROGRAMMING

Contrary to the top-down approach, bottom-up programming focuses on designing an


algorithm by beginning at the very basic level and building up as it goes. In this approach,
the modules are designed individually and are then integrated together to form a complete
algorithmic design. Moreover, the bottom-up approach is more suitable when a system needs
to be created from some existing components.
So, in this method, every module is built and tested at an individual level (unit testing) prior
to integrating them to build a concrete solution. The unit testing is performed by leveraging
specific low-level functions.

Advantages:

 Make decisions about reusable low-level utilities then decide how there will be put
together to create high-level construct.

2
Basis Top-Down Approach Bottom-Up Approach

Top-Down Approach is Theory- Bottom-Up Approach is Data-


Approach
driven. Driven.

Significanc Emphasis is on doing things Emphasis is on data rather than


e (algorithms). procedure.

Large programs are divided into Programs are divided into what
Focus smaller programs which is known are known as objects is called
as decomposition. Composition.

Communication is less among the Communication is a key among


Interaction
modules. the modules.

Widely used in debugging,


Areas Widely used in testing.
module documentation, etc.

The bottom-up approach is


The top-down approach is mainly
used by Object-Oriented
Language used by Structured programming
programming languages like
languages like C, Fortran, etc.
C++, C#, Java, etc.

May contains redundancy as we This approach contains less


Redundanc break up the problem into smaller redundancy if the data
y fragments, then build that section encapsulation and data hiding
separately. are being used.

3
Sequence: The execution of statements in a specific order, from top to
bottom. This ensures that instructions are executed one after the other,
sequentially.

Ex . Print (“Hello”)
Print(“World”)

Selection: Is a decision or a question. At some point, a program may need


to ask a question because it has reached a step where one or more options
are available. Depending on the answer given, the program will follow a
certain step and ignore the others.

Iteration: In Programming means repeating steps, that can be performed


by a computer processor , over and over again. This is often called a 'loop'.
Algorithms consist of instructions that are carried out (performed) one after
another.

Ex.
i=0

while i < 5:

print(i)

i += 1

Functions: Functions are self-contained modules of code that accomplish a


specific task. Functions usually "take in" data, process it, and "return" a
result. Once a function is written, it can be used over and repeatedly.

Ex.
def greet(name):

print("Hello,", name)

greet("Alice")

4
Arguments/Parameters: In programming, a value that is passed between
programs, subroutines or functions. Arguments are independent items, or
variables, that contain data or codes.

Ex.
def add_numbers(a, b)

return a + b

result = add_numbers(5, 3)

print("Result:", result)

Return Values: Return values are just what they sound like — the values
that a function returns when it completes.

Ex.

Add_numbers(a,b)

Return a + b

Calling: In computer programming, "calling" or "executing" a function


means to run the code contained within that function.

Naming Conventions: Consistent naming conventions for variables,


functions, and other identifiers. Good naming conventions improve code
readability and maintainability.

Variable Scope (Global & Local): The visibility and lifetime of variables
within a program. Global variables are accessible from anywhere in the
program, while local variables are only accessible within the scope in which
they are defined.

CODE FOR 4 USER-DEFINED FUNCTIONS

5
#Multiplying 2 Numbers

def multiply_numbers(x,y):
sum = x*y
return sum

num1 = 5
num2 = 5

print("The Sum is", multiply_numbers(num1 , num2))

#Find the average of two numbers

def average(a,b):
sum = a + b
print (a, b, 'average =' , sum/ 2)

average (25,50)
average (10, 30)

#Convert kilos to pounds weight

def convert_weight(kilos=0):
pounds = 2.205 * kilos
return pounds
i=1
while i<= 3:
print(i, 'kilo(s) is', convert_weight(i), 'pounds')
i=i+1

#Check if denominator is zero

def check_divide(numerator, denominator):


if denominator == 0:
raise ValueError("Division by zero is not allowed.")
return numerator / denominator

try:
result = check_divide(10,0)
print("Result of division:", result)

except ValueError as e:
print("Error:", e)

6
Test Plan (add additional rows as required)
Purpose Of Test Data Expected Actual Comments
Test
Test Results Result
Numbe
1 Check if it Multiply 2 Multiply Numbers Achieved
multiplies numbers
2 Find average of 2 Find average Find average of 2 Achieved
numbers numbers
3 Convert kilos to Convert Kilos to Converting kilos to Achieved
pound pounds pounds weight
4 Check if it says Check if Code says error is Achieved
error if the denominator is the denominator is

You might also like