100% found this document useful (2 votes)
9K views110 pages

Chapter 2 Problem Solving

This document discusses problem solving using computers. It covers the pre-programming phase of problem solving, which involves 5 steps: 1) analyzing the problem, 2) developing a hierarchy input process output (HIPO) chart, 3) developing an input-process-output (IPO) chart, 4) drawing program flowcharts, and 5) writing algorithms. It provides examples of how to create problem analysis charts, HIPO charts, and IPO charts to represent various problems, such as converting distances, calculating area of a circle, and estimating trip costs.

Uploaded by

shipporhino
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
9K views110 pages

Chapter 2 Problem Solving

This document discusses problem solving using computers. It covers the pre-programming phase of problem solving, which involves 5 steps: 1) analyzing the problem, 2) developing a hierarchy input process output (HIPO) chart, 3) developing an input-process-output (IPO) chart, 4) drawing program flowcharts, and 5) writing algorithms. It provides examples of how to create problem analysis charts, HIPO charts, and IPO charts to represent various problems, such as converting distances, calculating area of a circle, and estimating trip costs.

Uploaded by

shipporhino
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
  • Problem Solving with Computers: An introduction to problem-solving methodologies using computers, detailing essential concepts and phases of problem-solving in programming.
  • Pre-Programming Phase: Describes the preparatory steps in problem-solving before actual programming, including problem analysis and flowchart development.
  • Example Problems and Charts: Provides practical examples that apply problem-solving concepts using payroll and other scenarios, alongside relevant charts.
  • Input Process Output (IPO) Charts: Details the organization of information into IPO charts describing input-output data processing for different computing scenarios.
  • Flowchart Creation and Use: Discusses the flowcharting process, illustrating how to diagram solutions using examples like sales and average problems.
  • Algorithm Writing and Implementation: Introduces pseudocode and how it shapes the creation of actual program code, using payroll as a demonstration.
  • Program Structuring Techniques: Covers modular programming principles for building efficient, maintainable programs using sequential and decision logic.
  • Loop Structures in Programming: Explores different loop structures and how they manage repetitive tasks in a program, detailing various loop types.
  • Case Logic and Implementation: Describes handling multiple conditional instructions with case logic structures in programming, using real-life scenarios.
  • Implementation Phase: Covers transitioning pseudocode to executable program code, incorporating testing, debugging, and maintenance practices.
  • Advanced Programming Concepts: Discusses advanced software engineering principles such as cohesion and coupling for developing robust software modules.

CHAPTER 2

PROBLEM SOLVING
• This chapter will cover the following topics:
– Problem Solving Concepts for the Computer
– Pre-Programming Phase
– Programming Or Implementation Phase
What Problem Can Be Solved By
Computer
• When the solution can be produced by a set of
step-by-step procedures or actions.
• This step-by-step action is called an algorithm.
• The algorithm will process some inputs and
produced output.
• Solving problem by computer undergo two
phases:
– Phase 1:
• Organizing the problem or pre-programming phase.
– Phase 2:
• Programming phase.
PRE-PROGRAMMING PHASE
• This phase requires five steps:
– Analyzing the problem.
– Developing the Hierarchy Input Process
Output (HIPO) chart or Interactivity Chart (IC).
– Developing the Input-Process-Output (IPO)
Chart.
– Drawing the Program flowcharts.
– Writing the algorithms.
PRE-PROGRAMMING PHASE
• Analyzing The Problem
– Understand and analyze the problem to
determine whether it can be solved by a
computer.
– Analyze the requirements of the problem.
– Identify the following:
• Data requirement.
• Processing requirement or procedures that will be
needed to solve the problem.
• The output.
PRE-PROGRAMMING PHASE

Data Processing Output


given in the List of processing Output
problem or required or requirement.
provided by the procedures.
user
PRE-PROGRAMMING PHASE
• Example: Payroll Problem
– Calculate the salary of an employee who works by hourly basis. The formula to be
used is
Salary = Hour works * Pay rate

Data Processing Output


Hours work, Salary = Hours work * payrate Salary
Pay rate
Problem 1
Write a Problem Analysis Chart (PAC) to convert
the distance in miles to kilometers where 1.609
kilometers per mile.

Data Processing Output


Distance in Kilometers = Distance in
miles 1.609 x miles kilometers
Problem 2
• Write a Problem Analysis Chart (PAC) to find an
area of a circle where area = pi * radius * radius

Data Processing Output


radius area = 3.14 x radius x radius area
Problem 3
• Write a Problem Analysis Chart (PAC) to compute and display the
temperature inside the earth in Celsius and Fahrenheit. The relevant
formulas are
Celsius = 10 x (depth) + 20
Fahrenheit = 1.8 x (Celsius) + 32

Data Processing Output

depth celsius = 10 x (depth) + 20 Display celsius,


fahrenheit = 1.8 x (celsius) + Display fahrenheit
32
Problem 4
• Write a problem analysis chart (PAC) that
asks a user to enter the distance of a trip
in miles, the miles per gallon estimate for
the user’s car, and the average cost of a
gallon of gas. Calculate and display the
number of gallons of gas needed and the
estimated cost of the trip.
Data Processing Output
distance, gas needed = distance / Display gas needed
miles per gallon, miles per gallon.
cost per gallon Display estimated cost
estimated cost = cost per
gallon x gas needed
PRE-PROGRAMMING PHASE
• Developing the Hierarchy Input Process
Output (HIPO) or Interactivity Chart
– The problem is normally big and complex.
– Thus, requires big program.
– Thus, the processing can be divided into subtasks
called modules.
– Each module accomplishes one function.
– These modules are connected to each other to
show the interaction of processing between the
modules.
PRE-PROGRAMMING PHASE
• Main/control module controls the flow all
other modules.
• The IC is developed using top-down-
method: top to down left to right order
(also refer to order of processing).
• Modules are numbered, marked for
duplication, repetition or decision.
PRE-PROGRAMMING PHASE
• The interaction will form a hierarchy, called Hierarchy Input Process
Output Chart (HIPO) or Interactivity Chart (IC). Programming which
use this approach (problem is divided into subtasks) is called
Structured Programming .
Main Module

Module 1 Module 2 Module 3

Module 4 Module 5 Module 6


PRE-PROGRAMMING PHASE

PAYROLL
0000

READ CALCULATE PRINT


1000 2000 3000
PRE-PROGRAMMING PHASE
• Example 2.2: Extended Payroll Problem
• You are required to write a program to calculate both
the gross pay and the net pay of every employee of your
company. To determine the gross pay, you have to
multiply the accumulated total hours worked by the
employee, by the appropriate pay rate. The program
should print the cheque that tells the total net pay. The
net pay is calculated by subtracting the gross pay with
any deductions that may be incurred by the employee.
PRE-PROGRAMMING PHASE

Payroll
0000

Calculate Calculate Write


Gross Pay Net Pay Cheque
1000 2000 3000

Accumulate Determine Calculate


Hourly Pay rate Deductions
Worked 1200 2100
1100
Problem 1
Write a Hierarchy Input Process Output (HIPO) to convert
the distance in miles to kilometers where 1.609
kilometers per mile.

Convert the distance


0000

Kilometers = 1.609 Distance in


Distance in miles
x miles Kilometers
1000
2000 3000
Problem 2
• Write a Hierarchy Input Process Output (HIPO) to find an
area of a circle where area = pi * radius * radius

Area
0000

area = 3.14 x
radius Display area
radius x radius
1000 3000
2000
Problem 3
• Write a Hierarchy Input Process Output (HIPO) to compute and
display the temperature inside the earth in Celsius and Fahrenheit.
The relevant formulas are
Celsius = 10 x (depth) + 20
Fahrenheit = 1.8 x (Celsius) + 32

Temperature
0000

Calculate temperature Calculate temperature Display


in celsius In fahrenheit Temperature
1000 2000 3000

Get depth
1100
Problem 4
• Write a Hierarchy Input Process Output
(HIPO) that asks a user to enter the
distance of a trip in miles, the miles per
gallon estimate for the user’s car, and the
average cost of a gallon of gas. Calculate
and display the number of gallons of gas
needed and the estimated cost of the trip.
Estimation Cost
0000

Display gas needed


Calculate gas Calculate estimated
Display estimated
Needed Cost
Cost
1000 2000
3000

Get total distance Get miles Get average cost


in miles per gallon per gallon
1100 1200 2100
PRE-PROGRAMMING PHASE
• Developing the Input Process Output
(IPO) Chart
– Extends and organizes the information in the
Problem Analysis Chart.
– It shows in more detail what data items are
input, what are the processing or modules on
that data, and what will be the result or output.
– It combines information from PAC and HIPO
Chart.
PRE-PROGRAMMING PHASE

Input Processing Module Output


-Hours Worked -Enter Hourly Worked 1100 -Net pay
-Pay Rate -Enter Pay Rate 1200
-Deduction -Calculate Gross Pay 1000
-Enter Deductions 2100
-Calculate Net Pay 2000
-Print Cheque 3000
-End 0000
Problem 1
Write a Input Process Output (IPO) to convert
the distance in miles to kilometers where 1.609
kilometers per mile.

Input Processing Module Output

-Distance in Miles -Enter distance 1000 -Distance in


-Kilometers = 1.609 x 2000 kilometers
distance
-Display kilometers 3000
Problem 2
• Write a Input Process Output (IPO) to find an
area of a circle where area = pi * radius * radius

Input Processing Module Output


- radius - Enter radius 1000 - Area of a circle
- area = 3.14 x radius x radius 2000
-Display area 3000
-end 0000
Problem 3
• Write an Input Process Output (IPO) to compute and display the
temperature inside the earth in Celsius and Fahrenheit. The relevant
formulas are
Celsius = 10 x (depth) + 20
Fahrenheit = 1.8 x (Celsius) + 32
Problem 4
• Write an Input Process Output (IPO) that
asks a user to enter the distance of a trip
in miles, the miles per gallon estimate for
the user’s car, and the average cost of a
gallon of gas. Calculate and display the
number of gallons of gas needed and the
estimated cost of the trip.
Input Processing Module Output

- Distance in - Enter distance 1100 -Total gas


miles - Enter miles per gallon 1200 needed
- Miles per gallon - Calculate total gas 1000 - Estimated
- Cost gas per needed cost
gallon - Enter cost gas per gallon 2100
- Calculate estimated cost
- Display total gas and 2000
estimated cost 3000
- End
0000
PRE-PROGRAMMING PHASE
• Drawing the Program Flowcharts
– Flowchart is the graphic representations of the
individual steps or actions to implement a particular
module.
– The flowchart can be likened to the blueprint of a
building. An architect draws a blueprint before
beginning construction on a building, so the
programmer draws a flowchart before writing a
program.
– Flowchart is independent of any programming
language.
PRE-PROGRAMMING PHASE
– Flowchart is the logical design of a program.
– It is the basis from which the actual program
code is developed.
– Flowchart serves as documentation for computer
program.
– The flowchart must be drawn according to definite
rules and utilizes standard symbols adopted
internationally.
– The International Organization for Standardization
(IOS) was the symbols shown below (You can draw
the symbols using ready-made flowcharting template):
PRE-PROGRAMMING PHASE
Symbol Function
Show the direction of data flow or logical
solution.

Indicate the beginning and ending of a set of


actions or instructions (logical flow) of a module
or program.

Indicate a process, such as calculations, opening


and closing files.
PRE-PROGRAMMING PHASE
Indicate input to the program and output from the
program.

Use for making decision. Either True or False based


on certain condition.

Use for doing a repetition or looping of certain steps.

Connection of flowchart on the same page.

Connection of flowchart from page to page.


PRE-PROGRAMMING PHASE
• Example 2.3 : Sale Problem
– Draw a flowchart for a problem that to read two
numbers. The first number represents the unit price of
a product and the second number represents the
quantity of the product sold. Calculate and print the
total sale.
– Solution: Stepwise Analysis of the Sale Problem
• Start of processing
• Read the unit price
• Read the quantity
• Calculate total sale
• Print total sale
• Stop the processing
PRE-PROGRAMMING PHASE
START A

READ TOTAL SALE =


UNIT PRICE UNITPRICE × QUANTITY

READ PRINT
QUANTITY TOTALSALE

A STOP
PRE-PROGRAMMING PHASE
• Finding Average Problem
– Read a sequence of number, find the average of the
number and print the average.
– Solution: Stepwise Analysis of Average Problem
• Start the processing
• Read a number
• Add the number
• Repeat reading until last data
• Calculate the average
• Print the average
• Stop the processing
PRE-PROGRAMMING PHASE
START

READ
NUMBER

ACCUMULATE Repetition until end of data


TOTAL

CALCULATE
AVERAGE

PRINT
AVERAGE

STOP
PRE-PROGRAMMING PHASE
START

X
COUNTER
=∅
AVERAGE = TOTAL
READ NUMBER COUNTER

PRINT AVERAGE
TOTAL = TOTAL + NUMBER
COUNTER = COUNTER + 1

STOP

END OF
DATA

X
B
START

AVERAGE = TOTAL
COUNTER
COUNTER
=∅

PRINT
TOTAL AVERAGE
=∅
A
STOP
READ
NUMBER

NUMBER = B
-999999

COUNTER = COUNTER + 1

TOTAL = TOTAL + NUMBER

A
PRE-PROGRAMMING PHASE
• Writing the Algorithm (Pseudocode)
– Pseudocode means an imitation computer code.
– It is used in place of symbols or a flowchart to
describe the logic of a program. Thus, it is a set of
instructions (descriptive form) to describe the logic of
a program.
– Pseudocode is close to the actual programming
language.
– Using the Pseudocode, the programmer can start to
write the actual code.
START

READ
PRICE Algorithm:

Start
READ
QUANTITY Read price, quantity
Sale = price x quantity
Print Sale
SALE = PRICE × End
QUANTITY

PRINT
SALE

STOP
Example: Flowchart & Algorithm
Order of Execution of Instructions : Payroll System
Structuring a Program
• Develop efficient computer solution to problems:
1. Use Modules
2. Use four logic structures
a. Sequential structure
• Executes instructions one after another in a sequence.
a. Decision structure
• Branches to execute one of two possible sets of instructions.
a. Loop structure
• Executes set of instruction many times.
a. Case structure
• Executes one set of instructions out of several sets.
1. Eliminate rewriting of identical process by using modules.
2. Use techniques to improve readability including four logic
structure, proper naming of variables, internal documentation
and proper indentation.
Sequential Logic Structure
The Decision Logic Structure
• Implements using the IF/THEN/ELSE instruction.
• Tells the computer that IF a condition is true,
THEN execute a set of instructions, or ELSE
execute another set of instructions
• ELSE part is optional, as there is not always a set
of instructions if the conditions are false.
• Algorithm:
IF <condition(s)> THEN
<TRUE instruction(s)>
ELSE
<FALSE instruction(s)
Decision Logic Structure
Examples of conditional expressions
• A < B (A and B are the same data type
– either numeric, character, or string)
• X + 5 >= Z (X and Z are numeric data)
• E < 5 or F > 10 (E and F are numeric
data)
• DATAOK (DATAOK – logical datum)
Example
• Assume your are calculating pay at an
hourly rate, and overtime pay(over 40
hours) at 1.5 times the hourly rate.
– IF the hours are greater than 40, THEN the
pay is calculated for overtime, or ELSE the
pay is calculated in the usual way.
Example Decision Structure
NESTED IF/THEN/ELSE
INSTRUCTIONS
• Multiple decisions.
• Instructions are sets of instruction in which
each level of a decision is embedded in a
level before it.
NESTED IF/THEN/ELSE INSTRUCTIONS
The Loop Logic Structure
• Repeat structure
• To solve the problem that doing the same
task over and over for different sets of data
• Types of loop:
– WHILE loop
– Do..WHILE loop
– Automatic-Counter Loop
Loop Logic Structure
WHILE loop
WHILE loop
• Do the loop body if the condition is true.
• Example: Get the sum of 1, 2, 3, …, 100.
– Algorithm:
• Set the number = 1
• Set the total = 0
• While (number <= 100)
– total = total + number
– number = number + 1
• End While
• Display total
WHILE loop
Start

Set number = 1

Set total = 0

No
number <= 100

Yes
Display total
total =
total + number
End
number =
number + 1
DO…WHILE Loop
• The body of the loop will process first
before check the condition.
• Example: Get the sum of 1, 2, 3, …100.
DO…WHILE Loop
DO…WHILE Loop
Start

Set number = 1

Set total = 0

total = No
total + number

number = Display total


number + 1

Yes End
number <= 100
Automatic Counter Loop
• Use variable as a counter that starts
counting at a specified number and
increments the variable each time the loop
is processed.
• The beginning value, the ending value and
the increment value may be constant. They
should not be changed during the
processing of the instruction in the loop.
Automatic-Counter Loop
Automatic-Counter Loop
NESTED LOOP
NESTED LOOP
The Case Logic Structure
• Made up of several or many sets of instructions,
only one of which will be selected by the user and
executed by the computer
• Algorithm:
CASE OF VARIABLE
= constant1:
actions for VARIABLE = constant1
= constants2:
actions for VARIABLE = constant2

OTHERWISE:
Actions for VARIABLE = anything else
END-OF-CASE
Case Logic Structure
Case Logic Structure
• Example: A company has four different medical
plans. The programmer has given each plan a code
corresponding to the beginning initial of the
company: Plan 1 = F, Plan 2 = B, Plan 3 = K, Plan 4
= E.
The company pays for all of Plan 1. The individual
has to pay for part of the others. The payroll
deduction for Plan 2 = 4.65, for Plan 3 = 7.85, and
for Plan 4 = 5.50. Any other codes are considered in
error. Write the algorithm and draw the flowchart for
a module to determine the payroll deduction.
Example of Case Logic Structure
Programming Or Implementation
Phase
• Transcribing the logical flow of solution steps in
flowchart or algorithm to program code and run
the program code on a computer using a
programming language.
• Programming phase takes 5 stages:
• Coding.
• Compiling.
• Debugging.
• Run or Testing.
• Documentation and maintenance.
Programming Or Implementation
Phase
• Once the program is coded using one of the
programming language, it will be compiled to
ensure there is no syntax error. Syntax free
program will then be executed to produce output
and subsequently maintained and documented
for later reference.
CODING

COMPILE THE MAKE


PROGRAM CORRECTION

NO SYNTAX
ERROR

EXECUTE OR
RUN

DOCUMENTATION
OR MAINTENANCE
Problem 1
Write a Problem Analysis Chart (PAC) to convert
the distance in miles to kilometers where 1.609
kilometers per mile.
start

read miles

km = 1.609 x miles

print km

end
Problem 2
• Write a Problem Analysis Chart (PAC) to find an
area of a circle where area = pi * radius * radius
start

read radius

area = 3.14 x radius x radius

print area

end
Problem 3
• Write a problem analysis chart (PAC) that
asks a user to enter the distance of a trip
in miles, the miles per gallon estimate for
the user’s car, and the average cost of a
gallon of gas. Calculate and display the
number of gallons of gas needed and the
estimated cost of the trip.
start A

read distance
Read cost_gallon

read miles_gallon total_cost =


total_gas x cost_gallon

total_gas =
distance / mile_gallon Print total_gas,
total_cost

A end
Programming Or Implementation
Phase
• Coding
– Translation or conversion of each operation in the
flowchart or algorithm (pseudocode) into a computer-
understandable language.
– Coding should follow the format of the chosen
programming language.
– Many types or levels of computer programming
language such as:
– Machine language
– Symbolic language or assembly language
– Procedure-oriented language
– The first two languages are also called low-level
programming language. While the last one is called
high-level programming language.
Programming Or Implementation
Phase
• Machine Language
– Machine language uses number to represent letters,
alphabets or special character that are used to
represent bit pattern.
– Example:
• an instruction to add regular pay to overtime pay, yielding
total pay might be written in machine language as follows:
16 128 64 8
• in which 16 is a code that mean ADD to the computer. The
128 and 64 are addresses or location at which regular pay
and overtime pay are stored. The 8 represents the storage
location for the total pay.
Programming Or Implementation
Phase
• Sometimes, bit pattern that represent
letters and alphabets are used for coding.
– Example:
Instead of: 16 128 64 8
Use: 10000 10000000 1000000 1000

– This representation is ideal for a computer but


difficult and tedious to the programmer to
write a lengthy program.
Programming Or Implementation
Phase
• Symbolic Language or Assembly Language
– A symbolic language or assembly language is closely related to
machine language in that, one symbolic instruction will translate
into one machine-language instruction.
– Contain fewer symbols, and these symbols may be letters and
special characters, as well as numbers.
– As example, a machine language instruction
16 128 64 8
can be rewritten in assembly language as
ADD LOC1 LOC2 LOC3

– Which means, add content of location LOC1 to location LOC2


and put the result in location LOC3.
Programming Or Implementation
Phase
• Procedure – Oriented Language
– Programmer has to know the computer hardware
before he can write program in machine and
assembly language. It means the language is
machine dependent.
– Using procedure – oriented language, the
programmer can run the program in any computer
hardware.
– A special program called a compiler will translate
program written using procedure – oriented language
to machine language.
Programming Or Implementation
Phase
• Some example of the language:
– COBOL (COmmon Business Oriented Language)
– FORTRAN (FORmula TRANslation)
– Pascal
– C
– C++
– BASIC, etc.
• These languages are also called high-level
programming language
Programming Or Implementation
Phase

Computer Language Instruction Format

Machine language 16 128 64 8


Assembly language ADD LOC1 LOC2 LOC3
BASIC LET T = R + 0
FORTRAN TOTAL = RPAY + OPAY
COBOL ADD RPAY, OPAY GIVING TOTAL
Pascal TOTAL : = RPAY + OPAY
C TOTAL = RPAY + OPAY
Programming Or Implementation
Phase
• Compiling and Debugging
– Compiling is a process of a compiler translates a
program written in a particular high–level
programming language into a form that the computer
can execute.
– The compiler will check the program code know also
as source code so that any part of the source code
that does not follow the format or any other language
requirements will be flagged as syntax error.
– This syntax error in also called bug, when error is
found the programmer will debug or correct the error
and then recompile the source code again.
– The debugging process is continued until there is no
more error in the program.
Programming Or Implementation
Phase
• Testing
– The program code that contains no more error is
called executable program. It is ready to be tested.
– When it is tested, the data is given and the result is
verified so that it should produced output as intended.
– Though the program is error free, sometimes it does
not produced the right result. In this case the program
faces logic error.
– Incorrect sequence of instruction is an example that
causes logic error.
Programming Or Implementation
Phase
• Documentation and Maintenance
– When the program is thoroughly tested for a substantial period
of time and it is consistently producing the right output, it can be
documented.
– Documentation is important for future reference. Other
programmer may take over the operation of the program and the
best way to understand a program is by studying the
documentation.
– Trying to understand the logic of the program by looking at the
source code is not a good approach.
– Studying the documentation is necessary when the program is
subjected to enhancement or modification.
– Documentation is also necessary for management use as well
as audit purposes.
Cohesion and Coupling
• Especially important concepts to the modern
programmer, who in all probability will be working
with many other programmers on the same
problem.
• Cohesion allows a programmer to write a module
of a large program and test it independently.
• Coupling allows all programmers to use necessary
variables without losing the cohesion.
Cohesion
• Cohesion relates to the functional
independence of the module and to the
performance of a single task within the
module.
• The objective is to separate the problem into
parts.
• Each module is independent of each other
module, with single entrance and a single
exit, such as entering data, printing data,
initialising data.

Coupling
Coupling allows modules to be connected by an
interface, which enables the programmer to transfer
data from one module to another.
• Three coupling techniques are:
– Parameter
– Modules names
– Global variables.
• Allows for communication between modules.
• Coupling allows all programmers to use necessary
variable without losing the cohesion of the module.
Cohesion and Coupling
Cohesion is the ability for each module to be
independent of other modules.

Module 1 Module 2

Coupling
allows
modules to
share data Module 3 Module 4
The Modules and The Functions
• The programmer breaks the problem into
modules, each with specific function.
• It is much easier to write and test many
small modules than a single large program.
• Modules are arranged according to
processing order in interactivity chart.
The rules for designing modules
1. Each module is an entity and has one entrance
and one exit.
2. Each module has a single function such as
printing, calculating or entering data.
3. Each module is short enough to be easily read
and modified.
4. The length of module governed by its function
and the number of instruction to be executed.
5. A module is developed to control the order of
processing.
Types of modules
1. Control module
• Show the overall flow of data through the program.
All other modules are subordinate to it.
1. Init module
• Also called the preparation module, process
instruction that are executed only once – at the
beginning.
1. Process Data module
• May be processed only once, or may be part of a
loop.
1. Calculation Modules
• Do arithmetic calculations.
Types of modules
2. Print Modules
• Print output lines.
2. Read and Data validation modules
• Read or input data, validate data
• Validation modules separate from read modules
4. Wrap-up module
• Execute only once at the end.
• Include closing file and printing totals.
4. Event module
• Such as mouse down, mouse up, key entry.
Local and Global Variables
• The concept of local and global variables to
allow cohesion and coupling to occur.
• Local variables
– defined within a module
– used only by the module itself
• Global variables
– defined outside of the individual modules
– can be used by all modules
Scope of Local and Global Variables

Variables: A, B, C Global to all modules

CONTROL MODULE Local to CONTROL


Variables: X, Y, Z MODULE

MODULE1 Local to MODULE1


Variables: D, E, F

MODULE2 Local to MODULE2


Variables: G, H, I

Parameters
Parameters are local variables that are passed or sent
from one module to another.
• Parameters are another way of facilitating coupling that
allows the communication of data between modules.
• Eg: Read(A, B, C) – A, B & C are parameters
• There are two types of parameters:
– Actual Parameter
list of parameters that follow the module name being processed in
the calling module
– Formal Parameter
list of parameters that follow the module name at the beginning of
the module.
Parameter Terminology
Beginning Problem-Solving Concepts
for the Computer
• Data used in processing
• Constant is a value that never changes
during the processing of all instructions in a
solution
• Constant is given a location in memory and
a name.
• The value of a variable may change during
processing
Rules for naming and using variables:
• Name according to what it represented
• Do not use spaces in a variable name
• Do not a dash
• Consistent in the use of variable names
• Consistent when using upper and
lowercase characters
Data Types
• Data are unorganized facts
• Goes as input and is processed to produce
output, information
• Computers must be told the data type of each
variable and constant
• 3 common types: numeric, character, and
logical
• Other data types: date data type and user-
defined data types
Rules of Data Types
• Programmer designates the data type
during the programming process
• Data types cannot be mixed
• Each data types used data set
• Calculations involved only numeric data
type.
Functions
• Small sets of instructions that perform specific task and return values
• Used as part of instruction in a solution
• Divided into classes:
– Mathematical functions – used in science and business
– String functions – used to manipulate string variables
– Conversion functions – used to convert data from one data type to another
– Statistical functions – used to calculate things such as maximum value
– Utility functions – access information outside the program, i.e. date and
time function
• Functions use data, called parameters. Functions normally do not alter
parameters
Functionname(parameters list)

Operators
They are data connectors within expressions and
equations.
• They tell computer how to process the data
• Operands are data the operator connects and
processes
• Resultant is the answer
• Data type of the operand and the resultant depends
on the operator:
– Mathematical operators
– Relational operators
– Logical operators
• Operators have a hierarchy or precedence
Expressions and Equations
• Expression processes data, the operands,
through the use of operators
• Equation stores the resultant of an
expression in a memory location in the
computer through the equal (=) sign.
• Equations are often called assignment
statements

Common questions

Powered by AI

The concept of a module improves the readability and maintainability of a computer program by breaking complex problems down into smaller, discrete tasks, each represented by a module. This division allows programmers to focus on individual parts, enhances understanding by reducing complexity, and reuses code without rewriting identical processes, thus promoting readability . Modular design also supports maintaining programs by isolating issues within specific modules and enabling updates or changes without affecting the entire program, thereby preserving existing functionality while accommodating growth and change .

A Hierarchy Input Process Output (HIPO) chart aids in structured programming by organizing the problem into subtasks or modules. Each module accomplishes a specific function and interacts with other modules to process data, forming a hierarchy. The main/control module manages the flow between these modules, ensuring that information is processed in a top-down manner from left to right . This organization helps in managing complex problems by breaking them down into smaller, more manageable tasks, promoting clarity and efficiency in programming .

The two main phases involved in solving a problem using a computer are the Pre-Programming Phase and the Programming Phase. The Pre-Programming Phase involves organizing the problem, which includes steps such as analyzing the problem, developing Hierarchy Input Process Output (HIPO) charts, Input-Process-Output (IPO) charts, drawing program flowcharts, and writing algorithms . The Programming Phase involves transcribing the logical steps from the flowchart or algorithm into program code, compiling the code to check for syntax errors, executing the code, and maintaining documentation for future reference .

Pseudocode differs from actual programming code in that it uses plain language and structured principles to describe an algorithm's logic without conforming to any specific programming language syntax. It functions as an intermediary between the flowchart and the actual code, allowing programmers to refine their logic and sequence before coding . Pseudocode is useful because it is easy to read and modify, assisting in the transition from high-level logic design to low-level coding. This simplifies debugging and helps in understanding complex algorithms .

Flowcharts are crucial in the problem-solving process for computer programming because they provide graphical representations of the algorithm or process logic before actual coding begins. Like a blueprint for a building, a flowchart outlines the sequence and structure of operations necessary to solve the problem, independent of any programming language . This visual aid helps programmers understand the flow of actions, identify logical errors, and communicate the program structure efficiently to others involved in the project .

The programming phase of software development involves coding the logic from flowcharts or algorithms into a specific programming language, compiling the code to identify syntax errors, debugging these errors, executing the code to verify functionality, and documenting for future maintenance . Debugging is considered crucial because it addresses syntax and logical errors that can cause the program to malfunction or produce incorrect results. Effective debugging ensures that the program operates correctly within its intended scope and specification, improving reliability and user trust .

The logic structures that can be used to refine a computer program's solution include sequential structures, decision structures, loop structures, and case structures. The sequential structure executes instructions one after another in a linear order, providing the foundation for control flow. Decision structures, implemented using the IF/THEN/ELSE construct, allow the program to branch based on conditions, enabling adaptability . Loop structures, such as WHILE and DO/WHILE, facilitate repetition of instructions until certain conditions are met, allowing efficient handling of repetitive tasks . The case structure enables the selection of one instruction set from many, based on the variable's value, offering a streamlined approach to managing multiple decision pathways . These structures contribute to the program's control flow by providing comprehensive means to handle diverse operational paths and conditions efficiently.

The use of parameters in modules facilitates communication by allowing data to be passed from one module to another. Parameters define interfaces that enable modules to interact while maintaining separation of concerns, increasing modularity and reusability . There are two types of parameters: Actual Parameters, which are specified in the calling module and follow the module name during processing, and Formal Parameters, which are defined in the module's declaration and used within the module itself . This system enables precise data exchange without reliance on global variables, reducing coupling and enhancing module independence.

Local variables, defined within a module, are only accessible by that module, whereas global variables, defined outside individual modules, can be accessed by all modules . The use of local variables increases cohesion within a module by ensuring operations are self-contained and protected from unintended interference by outside variables. Conversely, global variables facilitate coupling by allowing data sharing between modules. However, over-reliance on global variables can lead to tight coupling, where modules become overly dependent on shared global state, potentially causing unforeseen side effects when global variables are modified . Therefore, balancing the use of local and global variables is crucial to achieving modular, flexible, and maintainable code design.

Functions in programming serve to encapsulate specific tasks or processes, providing modularity and reusability in code. They can return values after performing computations, efficiently manage repetitive tasks, and simplify complex operations . Functions are classified based on tasks into Mathematical functions (e.g., scientific computations), String functions (manipulating string variables), Conversion functions (data type transformations), Statistical functions (e.g., finding maximum values), and Utility functions (accessing system information like date and time). This classification helps organize functions by use case, ensuring clarity and ease in selecting the appropriate function for a given task.

You might also like