1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for...

13
1 Algorithm…

Transcript of 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for...

Page 1: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

1

Algorithm…

Page 2: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

2

Algorithm: Is a planned set of steps to solve certain problemEx.: Assume for instance that the gross pay of an employee is to be

calculated and then 10 percent of the gross is to remove as tax. The yield being the net pay. Write down the algorithm for this problem.

Sol.:1. Begin2. input name, hours_worked, and wage par hour 3. calculate gross_pay = hours_worked * wage par hour4. calculate tax = (10/100) * gross_pay5. calculate net_pay = gross_pay – tax6. print name, net_pay7. End

Page 3: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

3

Algorithm:Ex.: Write an algorithm to read the name and the

mark (one) for one student then add to his mark 5 marks

Sol.:1. Begin2. input name, mark3. new_mark = mark+54. print name, new_mark5. End

Page 4: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

4

Algorithm:Ex.: Write an algorithm to read the name and mark of

student and calculate the new mark of each by adding 5 to its mark. We have 10 students

Sol.:1. Begin2. studentcount =13. if studentcount > 10 then Stop else

A. read name, markB. calculate new_mark = mark + 5C. print name, new_markD. studentcount = studentcount +1E. goto step 3

Page 5: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

5

Pseudo code algorithm: It is an algorithm written in a language that is very close to high level languages while …do loop syntax: while ( condition) do begin endex.: 1. start2. student_count = 13. while (student_count < = 10) do4. begin5. read name, mark6. new_mark = mark + 57. print name, new_mark8. student_count = student_count + 19. end10. stop

Page 6: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

6

Pseudo code algorithm: repeat … until loopsyntax: repeat until (condition);ex.: 1. start2. student_count = 13. repeat4. read name, mark5. new_mark = mark + 56. print name, new_mark7. student_count = student_count + 18. until (student_count > 10);9. stop

Page 7: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

7

Pseudo code algorithm: For loopsyntax: for variable_name = initial_value to end_value do begin endex.: 1. start2. for student_count = 1 to 10 do3. begin4. read name, mark5. new_mark = mark + 56. print name, new_mark7. end8. stop

Page 8: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

8

Pseudo code algorithm:ex.: write an algorithm to compute the sum of n

numberssol.:1. start2. read n3. sum = 04. for I = 1 to n5. begin6. sum = sum + I7. end8. write sum9. stop

Page 9: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

9

Pseudo code algorithm:ex.: write an algorithm to compute y= 1+ 22/2 + 33/3 + … + nn/nsol.:1. start2. read n3. sum = 04. for I = 1 to n do5. begin6. sum = sum + (I ** I) / I7. end8. y = sum9. print y10. stop

Page 10: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

10

Pseudo code algorithm: if statement:syntax: if ( condition) then begin endor: if ( condition) then begin end else begin end

or: if ( condition) then begin end else if ( condition) then begin endor: if ( condition) then if ( condition) then if ( condition) then if ( condition) then

else else else else

Page 11: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

11

Pseudo code algorithm:Ex.: Write an algorithm to find the maximum of two valuesSol.:1. Start2. Read a, b3. if ( a>b) then4. max = a5. else6. if ( b>a) then7. max = b8. else9. print (a = b)10. print max11. Stop

Page 12: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

12

Pseudo code algorithm:Ex.: Write an algorithm to find the maximum of three valuesSol.:1. Start2. Read a, b, c3. if ( a>b) then4. if ( a>c) then5. max = a6. else7. if ( b>c) then8. max = b9. else10. max = c11. else12. if ( b>c) then13. max = b14. else15. max = c16. print max17. Stop

Page 13: 1 Algorithm…. 2 Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated.

13

Pseudo code algorithm:ex.: write an algorithm to compute y= 1- 22/2 + 33/3 - … nn/nsol.:1. start2. read n3. sum = 04. for I = 1 to n do5. begin6. if ( I mod 2 = 0) then 7. sum = sum - (I ** I) / I8. else9. sum = sum + (I ** I) / I10. end11. y = sum12. print y13. stop