Presentation is loading. Please wait.

Presentation is loading. Please wait.

ALGORITHMS Algorithm: Is an ordered set of unambiguous, executable steps defining a terminating process Unambiguous:during the execution of an algorithm,

Similar presentations


Presentation on theme: "ALGORITHMS Algorithm: Is an ordered set of unambiguous, executable steps defining a terminating process Unambiguous:during the execution of an algorithm,"— Presentation transcript:

1

2 ALGORITHMS Algorithm: Is an ordered set of unambiguous, executable steps defining a terminating process Unambiguous:during the execution of an algorithm, the information in the state of the process must be sufficient to determine uniquely and completely the action required by each step

3 ALGORITHMS Or The execution of each step in an algorithm does not require creative skills. Rather, it requires only the ability to follow directions.

4 Control Structures The key to clear algorithm design lies in limiting the control structure to only three structures: Sequence, Selection, & Repetition The following are the basic C and C++ control structures illustrated in flowchart and statement form:

5 1) Sequence : Example: C=A+B

6 2) Selection: if & if/else
if/else Structure(Double Selection) if Structure (Single Selection) T F

7 2) Selection: switch Break T F Switch Structure(Multiple Selection)

8 3) Repetition: while & do/while
do/while structure T F while Structure T F

9 3) Repetition: for loop T F for Structure

10 Pseudocode: There are several ways to represent algorithms. Such as pseudocode, flowcharts, … Pseudocode: Individual operations are described briefly and precisely in English standard statements consists of nouns and verbs

11 Pseudocode: For example, to evaluate the formula for converting temperature reading from Celsius to Fahrenheit: F= (9 /5) C + 32 Solution: Algorithm in pseudocode: Get the value of C Calculate F Print F

12 While the flowchart used is shown bellow (the used control structure is Sequence ):
Start Read C F=(9/5)C+32 Print F End

13 Example 1 : Write an algorithm and draw a flowchart to read the grades of five students , find the average of these grades and print it Counter=1 total=0,average=0 If Counter<5 Read Grade Total=Total+grade Average=Total/5 End START T F Print Average Counter=Counter+1 Algorithm: Initialize counter to 1 Initialize total to zero Initialize average to zero While counter is less than or equal 5 Input the grade Add grade into total average=total/5 Print average

14 Example2: A student took final exams of 10 courses
Example2: A student took final exams of 10 courses. Write an algorithm and draw a flowchart to read the grades and find their sum and print it. START Count=1 Total=0 If Count<10 Read Grade Print Total Total=Total+Grade END Count=Count+1 T F Algorithm: Initialize Count to 1 Initialize Total to Zero For Count=1 to 10 Read a Grade Add Grade to Total Print Total

15 Example 3 : Write an algorithm and draw a flowchart to read a grade, test it if PASS(more than or equal 50) or FAIL, and print the result . START Read Grade If Grade>50 Print”PASS” Print”FAIL” END T F Algorithm : Read a Grade If Grade more than or equal 50 Print PASS Else Print FAIL


Download ppt "ALGORITHMS Algorithm: Is an ordered set of unambiguous, executable steps defining a terminating process Unambiguous:during the execution of an algorithm,"

Similar presentations


Ads by Google