Sie sind auf Seite 1von 23

CMP 101 Fundamentals of Computer and programming in C

Computer Program Planning

Algorithm
The word algorithm is the deformed version of the word alkhowarizm: A method used by Ab Abdallh Muammad ibn Ms al-Khwrizm for problem solving.
Reference :http://en.wikipedia.org/wiki/Muhammad_ibn_M%C5%ABs%C4%81_alKhw%C4%81rizm%C4%AB#Arithmetic Al Khwrizmi was a thematician, astronomer and geographer, a scholar in the House of Wisdom in Baghdad. His contributions had a great impact on language. "Algebra" is derived from al-jabr, one of the two operations he used to solve quadratic equations. Algorism and algorithm stem from Algoritmi, the Latin form of his name. His name is the origin of (Spanish) guarismo and of (Portuguese) algarismo, both meaning digit.

Algorithm
A ser of step-by-step instructions to accomplish a task.
An algorithm must have start instruction Each instruction must be precise. Each instruction must be unambiguous. Each instruction must be executed in finite time. An algorithm must have stop instruction.

Algorithm Example 1
Suppose you are given a set of mark sheets where each mark sheet bears A, B, C or F letter grades. Write an algorithm to read mark sheet and print the grade that it contains. 1. Start 2. Take a mark sheet and read the grade. 3. Print the grade 4. Stop

Algorithm Example 2
Suppose you are given a set of mark sheets where each mark sheet bears A, B, C or F grades. Write an algorithm to read a mark sheet and print the if the grade is A only. 1. Start 2. Take a mark sheet and read the grade. 3. If grade is A then Print the grade 4. Stop

Algorithm Example 3
Suppose you are given a set of mark sheets where each mark sheet bears A, B, C or F grades. Write an algorithm to read a mark sheet and print the grade if it is A or B only. 1. Start 2. Take a mark sheet and read the grade. 3. If grade is A or B then Print the grade 4. Stop

Algorithm representation
A pseudo code A flowchart Programs statements in a programming language. Pseudocode Example 1
Start Take a mark sheet and read the grade. Print the grade Stop

Pseudocode Example 2
Start Take a mark sheet and read the grade. If grade is A then Print the grade Stop

Flowchart
Pseudocode Example 1 START START Grade Grade Grade = A Grade Grade STOP STOP Yes

Pseudocode Example 2

No

Flowchart Symbols
INPUT DECISION PROCESS

TERMINAL Flow line PRINT

DIRECT ACCESS STORAGE

Sequential Access

MAGNETIC DISK

Flowchart/Pseudocode examples
if A < B then Print BIG B else If A>B then Print BIG A else Print A=B endif endif if A < B then Start Print Big B else A,B If A>B then Print BIG A else Print A=B endif If block

else block

A<B

A:B A=B

A>B

endif

BIG B

A=B

BIG A

Stop

Find the biggest of the two numbers A and B

Flowchart/Pseudocode examples Find the largest of


three numbers A, B and C. Start A 50 A,B,C A<B A:B A=B A=B A>B B 30 C 40

B<C

B:C B=C

B>C

A<C

A:C

A>C

BIG C

BIG B

BIG C

A=C

BIG A

Stop

Flowchart/Pseudocode Selection
Start Input A,B,C
If A < B then Start A,B,C A<B A< B B<C B:C B=C B>=C A:B A=B A=B A<=C A>B A>B

Print A<B If B < C then


Print Big C else if B = C then Print A=B else Print Big B end if end if else

A:C A=C

A>C

BIG C

B:C

B>C A<=C
A:C BIG B BIG C Stop

BIG A

B=C

A=C

end if

Flowcharts
Given the input data:
Student id number, student name, the marks obtained in 5 subjects, each subject having maximum marks 100.
Start Roll, name, m1,m2,m3,m4,m5 Total = m1+m2+m3+m4+m5 Percentage = Total/5

Draw a flowchart for the algorithm to:


Calculate the percentage marks obtained , and Print students roll number and percentage of marks.

Roll, name, percentage

Stop

Start
Student no=1

Compute percentages for three students and print their roll numbers, names, and percentage of marks.

Roll, name, m1,m2,m3,m4,m5

Student no 0 Roll name m1 m2 m3 0 0 0 0

1 25 A 70 80 80

2 25 A 70 80 80

2 35 B 80 70 70

3 35 B 80 70 70

3 27 C 60 45 90

Total = m1+m2+m3+m4+m5 Percentage = Total/5 Student no=+1 Roll, name, percentage Is student no < 3 No Stop

m4 m5
Total

0 0
0

90 40
360 72

90 40
360 72

70 50
340 68
Second iteration

70 50
340 68

60 30
285 57
Third iteration

Percentage 0

Yes

Variables or Memory address

Initial value

First iteration

Start

Compute percentages for many students and print their roll numbers, names, and percentage of marks.

Roll, name, m1,m2,m3,m4,m5

Roll
name m1

0
0

25
A 70

35
B 80

0 0
0 0 0 0 340 68

Yes
Roll =0

m2 No Total = m1+m2+m3+m4+m5
m3 m4 m5 Percentage = Total/5 Total

0
0 0 0 0

80
80 90 40 360 72

70
70 70 50 340 68

Percentage 0
Variables or Memory address

Roll, name, percentage

Second Third Initial First value iteration iteration iteration

Stop

Flowchart Selection
Start Start Input A, B if A > B then Print I am watching end if end A,B Then block

Is A>B?

Yes

No

I am watching

Stop

Flowchart Selection
Start Start Input A, B if A > B then then block else Print I am watching end if end A,B

else block
No Is A>B?

Yes

I am watching

Stop

Flowchart Selection
Start Start Input A, B if A > B then X=A*B Print X else if B=0 then Print B is zero else X=A/B Print X end if end if end A,B then block

No

Is A>B?

Yes Is B=0? No X=A/B else block

X=A*B

Yes B is zero

Stop

Flowchart Selection
Start A,B then block

No

Is A>B?

Yes else if block Is B=0? No X=A/B

X=A*B

Yes B is zero

Start Input A, B if A > B then X=A*B Print X else if B=0 then Print B is zero else X=A/B Print X end if end

Stop

Flowchart Selection (Case)


Start A Is A =1 Is A = 20 Is A = 65 W start Input A If A =1 then Print X elseif A =20 then Print Y elseif A=65 then Print Z else Print W endif end

start Input A case A of 1: Print X 2: Print Y 3: Print Z otherwise: Print W endcase end

Stop

Flowchart Iteration
Start Count=1 WHILE LOOP FOR LOOP Range Start for Count = 1 to 5 by 1 do Print Count end for end Start Count = 1 while Count <= 5 do Print Count Count=Count+1 end while end Start Count = 1 repeat Print Count Count=Count+1 until count > 5 end

Count=+1 No

Count

Is count=5? Yes Stop

Range

REPEAT LOOP

Range

Flowchart Iteration
Start N i=1 Start Input N for I = 1 to N by 1 do Input Roll, Name, m1, m2, m3, m4, m5 Print Roll, Name, m1, m2, m3, m4, m5 end for end Start Input N i =1 repeat Input Roll, Name, m1, m2, m3, m4, m5 Print Roll, Name, m1, m2, m3, m4, m5 i++ until i > N end

Roll, Name, m1,m2,m3,m4,m5 i=+1

Yes

Roll, Name, m1,m2,m3,m4,m5

Is I < N ? No Stop

Flowchart Iteration
Start Count=0

WHILE LOOP
Start Count = 0 Input Roll, Name, m1, m2, m3, m4, m5 while Roll <= 5 do Count=Count+1 Input Roll, Name, m1, m2, m3, m4, m5 end while Print Count end

No

Roll, Name, m1,m2,m3,m4,m5

Count=+1 Is Roll=0?

Yes
Count Stop

Das könnte Ihnen auch gefallen