Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Problem Solving and Program Design 1

Uploaded by

ttavengwa055
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Problem Solving and Program Design 1

Uploaded by

ttavengwa055
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Problem solving and

program design
Introduction to Computer Science and Programming
Programming = problem solving + implementation
• A typical programming task can be divided into two phases:
• Problem solving phase
- produce an ordered sequence of steps that describe solution of
problem.
- this sequence of steps is called an algorithm
• Implementation phase
-implement the program in some programming language
Software development method
1)Specify the problem requirements.
2)Analyze the problem.
3)Design the algorithm to solve the problem.
4)Implement the algorithm.
5)Test and verify the completed program.
6)Maintain and update the program.
Specify the problem requirements
State the problem clearly and unambiguously

Gain a clear understanding of what is required for its solution.


The objective is to eliminate unimportant aspects and zero in on the root problem.

Specifications can include the
following:
- Does the problem require interaction with the user?
- Does the problem manipulate data?
What is the input data & how it is represented?
- Does the problem produce output? How the
results should be generated and formatted.
- What are the required formula for solution
- Is there any constraints on problem solution?
Analyse the problem
Analysing the problem involves identifying the problem:

–inputs, that is, the data you have to work with;


–outputs , that is, the desired results; and
–any additional requirements or constraints on the solution. At this stage, you should also
determine
–developa list of problem variables and their relationships. These relationships may be
expressed as formulas.
An Example for Problem Specifications:

Problem Statement:
Determine the total cost of apples given the number of kilos of apples
purchased and the cost per kilo of apples.

We can summarize the information contained in the problem


statement as follows:

Problem Inputs
–quantity of apples purchased (in kilos)
–cost per kilo of apples (in dollars per kilo)

Problem Output
–total cost of apples (in dollars)

Once you know the problem inputs and outputs, develop a list of formulas that specify relationships
between them.

The general formula
–Total cost = Unit cost* Number of units

Total cost of apples = Cost per kilo* kilos of apples



Algorithm Design and Testing

•Design an algorithm for the problem.


• If the problem is divided into smaller
sub-problems, then design an algorithm for
each sub-problem.

How to write an Algorithm?


An algorithm is a sequence of statements to perform some operations.
An algorithm can be expressed using a flowchart.
• The algorithm would consist of at least the
• following tasks:

• 1- Input (Read the data)


• 2- Processing (Perform the computation)
• 3- Output (Display the results)
Testing the Algorithm:
- You need to check the algorithm for
correctness
- Use sample data for algorithm testing
Implementation

• Coding:
• - After verifying that the algorithm is correct,
• you can code it in any high-level programming
• language(C in our case)
• - The algorithm is now converted into a program
Test and verify the program

•Compile the program (to check for syntax error)

• Run the program. If the execution doesn’t go


well, then reexamine the code, the algorithm, or
even the problem analysis.
Algorithms and Flowcharts
Pseudocode & Algorithm

• Example 1: Write an algorithm to determine a student’s final


grade and indicate whether it is passing or failing. The final
grade is calculated as the average of four marks.
Pseudocode & Algorithm

Pseudocode:

•Input a set of 4 marks


•Calculate their average by summing and dividing by 4
•if average is below 50
Print “FAIL”
else
Print “PASS”
Pseudocode & Algorithm
•Detailed Algorithm
• Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
The Flowchart
A Flowchart
–shows logic of an algorithm
–emphasizes individual steps and their interconnections
–e.g. control flow from one action to the next
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Denotes a process to be carried out


Rectangle
e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made. The program should


continue along one of two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


Example
Example 2

•Write an algorithm and draw a flowchart to convert the length in


feet to centimeter.
Pseudocode:
• Input the length in feet (Lft)
•Calculate the length in cm (Lcm) by multiplying LFT with 30
•Print length in cm (LCM)
Example 2
Example 3

Write an algorithm and draw a flowchart that will read the two sides of a
rectangle and calculate its area.
Pseudocode
•Input the width (W) and Length (L) of a rectangle
•Calculate the area (A) by multiplying L with W
•Print A
Example 3
Example 4
•Write an algorithm and draw a flowchart that will calculate the roots of a
quadratic equation

• Hint: d = sqrt ( b*b -4*a*c ), and the roots are:


•x1 = (–b + d)/2a and x2 = (–b – d)/2a
Example 4
Pseudocode:
•Input the coefficients (a, b, c) of the quadratic equation
•Calculate d
•Calculate x1
•Calculate x2
•Print x1 and x2
Example 4
Example


You have just been hired by the city council to mark distances between major cities in Zimbabwe.
Unfortunately the current distances are measured in miles and some give distances in kilometres
You and your co-workers prefer to deal in metric measurements(Kilometres). Write a program that
performs the necessary conversion.

You might also like