Documente Academic
Documente Profesional
Documente Cultură
Chapter objectives
After completing this chapter, you will be able to: Identify the approaches to solve a programming problem. Describe the steps to program development. Demonstrate the principles and use of flow-charting.
2
Algorithms
To solve a problem, you got to figure out how to solve it by yourself. To make a computer solve a problem, you have to tell the computer, step by step, exactly what you want it to do. The computer then "executes" the program, following each step mechanically, to accomplish the end goal. Algorithm: Sequence of step-by-step instructions that will produce a solution to a problem
Flowcharts
A flowchart is a diagrammatic representation that illustrates the sequence of operations to be performed to get the solution of a problem. Notation that is often used during the design stage of the software life cycle for quick and easy understanding of a programs flow.
6
Flowcharts
Flowcharts
Process Input/Output
1
9
These blocks are used to start and end a process or program. These blocks should contain simple labels to indicate the process or program.
10
Input/Output Block
Input/Output
This block can represent either input or output statements. This block should be labeled with a brief description of the input or output.
11
Process Block
Process
The process block represents some operation carried out on an element of data. This could be a calculation. This block should be labeled with a brief description of the process being carried out.
12
Decision Block
Decision
The decision block should be a True/False choice. The label on the block should contain the question to the decision that should be made.
13
Continuation symbol
14
15
In drawing a proper flowchart, all necessary requirements should be listed out in logical order. The flowchart should be clear, neat and easy to follow. The usual direction of the flow of a procedure or system is from left to right or top to bottom. Only one flow line should come out from a process symbol.
16
Only one flow line should enter a decision symbol, but two or three flow lines, one for each possible answer, should leave the decision symbol.
17
End
start
18
Write within standard symbols briefly. As necessary, you can use the annotation symbol to describe data or computational steps more clearly.
19
If the flowchart becomes complex, it is better to use connector symbols to reduce the number of flow lines. Avoid the intersection of flow lines if you want to make it more effective and better way of communication. Ensure that the flowchart has a logical start and End. It is useful to test the validity of the flowchart by passing through it with a simple test data.
20
Communication: Flowcharts are better way of communicating the logic of a system to all concerned. Effective analysis: With the help of flowchart, problem can be analysed in more effective way. Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes.
21
Efficient Coding: The flowcharts act as a guide during the systems analysis and program development phase. Proper Debugging: The flowchart helps in debugging process. Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part
22
START
A flowchart is a diagram that depicts the flow of a program. The figure shown here is a flowchart for the paycalculating program.
Read Hours
END
Example:
START
Display message How many hours did you work?
Rounded Rectangle
Pay-calculating program
Notice there are three types of symbols in this flowchart:
Read Hours
Parallelogram
Rectangle
Rounded Rectangle
END
START
Terminal
Terminals
Read Hours
END
Terminal
END
START
Display message How many hours did you work?
Input/Output Operations
Read Hours
Input/Output Operation
Read Hours
END
Example:
START
Display message How many hours did you work?
Pay-calculating program
Processes
Read Hours
Display message How much do you get paid per hour? Read Pay Rate
Process
END
START
Output Operation
Read Hours
END
START
Read Hours
END
START
Read Hours
Output Operation
END
START
Display message How many hours did you work?
Read Hours
END
START
Display message How many hours did you work?
Read Hours
END
START
Display message How many hours did you work?
Read Hours
Output Operation
END
Sequence Structure
a series of actions are performed in sequence The pay-calculating example was a sequence flowchart.
Decision Structure
Decision Structure
A new symbol, the diamond, indicates a yes/no question. If the answer to the question is yes, the flow follows one path. If the answer is no, the flow follows another path
NO YES
Decision Structure
In the flowchart segment below, the question is x < y? is asked. If the answer is no, then process A is performed. If the answer is yes, then process B is performed.
NO YES
x < y?
Process A
Process B
Decision Structure
NO
x < y?
YES
Calculate a as x plus y.
Calculate a as x times 2.
Decision Structure
The flowchart segment below shows a decision structure with only one action to perform.
Flowchart
NO
x < y?
YES
Calculate a as x times 2.
Repetition Structure
A repetition structure represents part of the program that repeats. This type of structure is commonly known as a loop.
Repetition Structure
Notice the use of the diamond symbol. A loop tests a condition, and if the condition exists, it performs an action. Then it tests the condition again. If the condition still exists, the action is repeated. This continues until the condition no longer exists.
Repetition Structure
In the flowchart segment, the question is x < y? is asked. If the answer is yes, then Process A is performed. The question is x < y? is asked again. Process A is repeated as long as x is less than y. When x is no longer less than y, the repetition stops and the structure is exited.
x < y? YES Process A
Repetition Structure
x < y?
YES
Add 1 to x
45
46
47
Start
C, N, R I=1
T =C (1 + R)I
49
50
Flowchart Assignments
Draw a flowchart to find the sum of the even numbers in a given series from 1 to N. Draw a flowchart to compute the sum of squares of integers from 1 to 50 Draw a flowchart to perform S=1+1/2+1/3+1/4+..+1/100. Draw a flowchart to perform S = 1 - + 1/3 - +-1/100 Draw a flowchart to perform S= 1 + 1/3 + 1/5 + 1/7 +..+ 1/99. 51