Introduction- Algorithms (new)
Introduction- Algorithms (new)
ALGORITHM: Introduction
Instruction in algorithm,
Problem
Given: a set A={a1,a2,…,an} of integers
Question: find the maximum integer Ai
ALGORITHM: Example (MAX)
A straightforward idea is
1. Start
2. Input 'n', no of elements in list.
3. Input set of integer values in A={a1,a2,…,an}
4. Simply assign a1 to a new variable max (assuming first value as
max)
5. check max with all elements of list, if any value in list is greater
than max, update stored max to that value
6. Output max is the maximum value
7. Stop
ALGORITHM: Example (Factorial)
1. input a number n
2. set variable final as 1
3. final <= final * n
4. decrease n
5. check if n is equal to 0
6. if n is equal to zero, goto step 8 (break out of loop)
7. else goto step 3
8. print the result final
ALGORITHM: Example (Factorial)
1. input a number n
2. set variable final as 1
3. final <= final * n
4. decrease n
5. check if n is equal to 1
6. if n is equal to 1, goto step 8 (break out of loop)
7. else goto step 3
8. print the result final