Computer Programming -1- Lecture 3

advertisement
Computer Programming -1Lecture 3
The main steps of program development to solve
the problem:
1- problem definition :
The problem must be defined into three question :
1- What are the available data or data that represent the input
of the program? (input)
2- What is the method or set of processes that lead to get the
results from the available data? (process)
3-What are the required results or Output of the program?
(output)
2-Design solution :
- Dividing the problem into small steps
- Then identify the solution in a sequence of steps called
algorithm.
3-Write the program (source code) :
compile the algorithm into a specific programming language.
4- Compilation :
When testing program, the compiler may find Syntax errors
in the program , in this case must be corrected and then
recompile the program again
Types of Program Error
 There are three basic categories of program errors:
1. Syntax Errors
2. Run-time Errors
3. Logic Flaws
 In the first two cases when an error occurs, the computer
displays an 'Error Message', which describes the error, and
its cause. In the final case, the program will not show an
error message but it will not do what the programmer wanted
it to do.
1-Syntax Errors :
 These errors are the easiest to find because they are
highlighted by the compiler. Error messages are given. This
type of error is caused by the failure of the programmer to
use the correct grammatical rules of the language.
 If a program has a syntax error it cannot be translated, and
the program will not be executed.
2. Run-time Errors
 Run-time errors are detected by the computer and displayed
during execution of a program.
 A run-time error occurs when the user directs the computer to
perform an illegal operation, eg: Dividing a number by zero,
Assigning a variable to the wrong type of variable
3. Logic Flaws
 These are the hardest errors to find as they do not halt the
program. They arise from faulty thinking on behalf of the
programmer.
 Programs with logic errors will often compile, execute, and
output results. However, at least some of the time the output
will be incorrect. Error messages will generally not appear if
a logic error occurs, this makes logic errors very difficult to
locate and correct.
Algorithm and flowchart
- An Algorithm :
is a finite sequence of instructions that produce the solutions
to the problem and is written by any human-language.
- An algorithm must ( algorithm constraint ):



Be clear.
Give the correct solution in all cases
should not be associated a specific programming
language, but can be converted to any language
selected by the programmer.
Algorithm
Example
- write an algorithm that find the perimeter of the
rectangle :
1- start
2- input : L and W
3- process : p= 2*(L+W)
4- output : print p
5- End
Flow chart
Flowchart :
 A graphical representation of the sequence of operations
in a program.
 Programmer prepares flowchart before coding.
 Note: Different symbols are used to draw each type of
flowchart.
Flowchart Symbol
Symbol
Name
‫بداية – نهاية‬
Start - End
‫مدخالت – مخرجات‬
Input - Output
‫معالجة‬
Processing
‫القرار و التفرع‬
Decision and branching
‫خط التدفق‬
Flow Line
Flowchart
Example :
-Draw flowchart that find the perimeter of the
rectangle.
start
Input L,W
p= 2*(L+w)
print p
End
Example 1 :
- write an algorithm and flowchart for finding the average
of two numbers.
Algorithm :
1-start
2-input: A and B
3-process : average = (A+B)/2
4- output : print average
5- End
Flowchart :
start
input A and B
average= (A+B)/2
print average
End
Example 2 :
- Write an algorithm and draw flowchart that reads two
values, determines the largest value and prints the
largest value.
Algorithm :
1-start .
2- input : A,B
3- process : A>=B
yes : A= Max
No : B=Max
4- output: Max
5- End
Flowchart :
Start
Input A, B
No
A >=B
?
Max =B
Yes
Max =A
Print Max
End
Example 3 :
- Write an algorithm and draw flowchart to calculate the
value of each of the variables C, B, A in the following
equation:
(1) A= X² + 2Y
(2) B= 2X – 3A
(3) C= A² + XB
Algorithm :
1-start.
2- input : X,Y
3- process : A= X² + 2Y
B= 2X – 3A
C= A² + XB
4- output: A,B,C
5- end
Flowchart :
Start
Input X,Y
A=(X*X) + 2Y
B= 2X – 3A
C= A + XB
print A,B,C
End
Homework
 Write an algorithm and draw a flowchart :
1) finding Area to the circle when Area = 3.14 * X .
2) finding the value to A and B( when X and y are
entered by user)
A=X^2+2Y , B=2X-3A.
Download