Ministry of Higher Education Scientific Research Al Mustansiriya

  • Slides: 7
Download presentation
Ministry of Higher Education & Scientific Research Al- Mustansiriya University College of Engineering First

Ministry of Higher Education & Scientific Research Al- Mustansiriya University College of Engineering First Year L e c t. E m a d A. H u s s i e n L e c t. G r e g o r A. A r m i s e A s s t. L e c t. M a j i d E. D o b a k h

LECTURE 2 1. Algorithms It is a combination of phrases and events that can

LECTURE 2 1. Algorithms It is a combination of phrases and events that can be arranged as steps to solve a specific problem. That can be done by understanding this problem whether it mathematic or logic before convert it to flow chart. E x a m p l e - 1 : Write an algorithm to read five numbers then find and print their summation. S o l u t i o n : A computer algorithm can only carry out simple instruction like: s". the summation". Thus an algorithm is: 1. Start 2. Read L, M, N, O, P 3. Find Sum (S) , S =L+M+N+O+P 4. Print S 5. End E x a m p l e - 2 : Write an algorithm to read find the area of rectangle. Enter the length and width then print the result. S o l u t i o n : A computer algorithm can only carry out simple instruction like: Enter length and width". Fin the Area of the rectangle". the Area". 1. Start 2. Input L, W 3. 4. Find Area (A) , A =L* W Print A 5. End

2: Flowcharts are graphs that represent the formal view used to solve any problem.

2: Flowcharts are graphs that represent the formal view used to solve any problem. Flowcharts help the programmer to write his program. Flowcharts consist of a shapes connected by a straight lines. The following table shows these shapes and their operations. Shape Operation Start or End chart Input / Output data �Read or Input �Print Processing / Storing � Add(+) , Sub(-) , Mul(*) , Div(/) , Power(^), … Store a value (Put) Flow Lines Connectior Decision �If statement �Question ( ? ) Looping / Counters

The advantage of using algorithms and flowcharts are: 1. To show the mathematical logic

The advantage of using algorithms and flowcharts are: 1. To show the mathematical logic used to solve problems. 2. To show the data processing is done. 3. Helps the programmer to write his program. 4. Divides the big problem to smaller parts. 5. To avoid the errors that occurred during writing the program. 6. It is a middle step between problem difficulty and its conversion to suitable program. 7. Easy to convert it to any programming language. In general, we can divide flowcharts to a four shapes (charts): 1. Simple sequence charts 2. Branched charts. 3. Single loop charts. 4. Multi-loop charts. 2 -1: Simple sequence charts The events arrangement of this type is as straight sequence Start from the beginning of the program to the end without any branches or repetition (see figure beside). 1 st event 2 nd event Nth event End

E x a m p l e - 3 : Write an algorithm and

E x a m p l e - 3 : Write an algorithm and draw a flow chart to find the value of A, B, C from the following equations: A = X 2+2 Y , B = 2 X-3 A , C = A 2 -XB Where X is the circle area and Y is the circumference. Input the radius (R) and print the value of A, B and C. Solution: 1. Start Input R 2. Input Radius (R) 3. Put pie = 3. 14 Put pie=3. 14 4. Find Area (X), X= R^2*pie 5. Find Circumference (Y), Y= 2*R*pie 6. Find A, A= X^2+2*Y 7. Find B, B=2*X-3*A 8. Find C, C=A^2 -X*B 9. Print A, B, C X=R^2*pie Y=2*R*pie A=X^2*2*Y B=2*X-3*A C=A^2 -X*B 10. End Print A, B, C End

2 -2: Branched charts The need for the branching is to make decisions or

2 -2: Branched charts The need for the branching is to make decisions or comparison between two or more choices. Each choice will flow in different way (branch). Generally the branched charts may take one of the two forms below: (value) < If –Else if(cond. ) st 1 event > (value) = (value) 2 event 3 rd event nd Decision of three branches NO If (Condition) ? 1 st event Yes 2 nd event Decision of two branches E x a m p l e - 4 : Write an algorithm and draw a flow chart to find and print the maximum number between two numbers. S o l u t i o n Start 1. Start Read A, B 2. Read A , B 3. If (A > B) ; continue. No Else: goto-5. 4. Find maximum (Max = A) : goto-6 5. Find maximum (Max = B). If (A >B)? Max=A Max=B 6. Print Max 7. End Yes Print Max) End

E x a m p l e - 5 : Write an algorithm and

E x a m p l e - 5 : Write an algorithm and draw a flow chart to evaluate W from the below equation. Input X and print the value of W for each value of X. �X � 1 : X � 0 � W �� Sin(X) � 5 : X � 0 � 2 X � 1 : X � 0 � Sol utio n: Start Input X 1. Start 3. (x >x 0) ; continue 2. If Input >0 else. If (x = 0) ; goto-5 else. If (x < 0) ; goto -6 4. Find W, W = X+1 : goto-7 W=2*X-1 If (X)? =0 W=Sin(X)+5 >0 W=X+1 5. Find W, W = Sin(X)+5: goto-7 6. Find W, W = 2*X-1 7. Print X , W 8. End Print X, W End 2 -3: Single loop charts These charts are used when we need to repeat an operation or group of operations to specific number of times. These types of charts are used to create the counters. Counters are used to repeat an operation or group of operations in specific number. Counters can be classified to two forms: � General form. � Conventional form.