C Programming- Algorithm and Flowchart
What is Algorithm ?
A process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer.
Why Algorithm ?
An algorithm is important in optimizing a computer program according to the available resources. .
What makes a better Algorithm ?
Ultimately when anyone decide to solve a problem through better algorithms then searching for the best combination of program speed and least amount of memory consumption is desired.
Types Of Algorithm :
- Brute Force
- Divide and Conquer
- Decrease and Conquer
- Dynamic Programming
- Greedy Algorithm
- Transform and Conquer
- Back Tracking Algorithm
Steps Of Writing An Algorithm:
- Obtain a description of the problem.
- Analyze the problem.
- Develop a high level algorithm.
- Refine the algorithm with extra details.
- Review and Implement the algorithm.
Problem: I need a send a birthday card to my brother, Animesh.
Analysis: I don’t have a card. I prefer to buy a card rather than make one myself.
High-level algorithm:Go to a store that sells greeting cards
Select a card
Purchase a card
Mail the card
This algorithm is satisfactory for daily use, but it lacks details that would have to be added were a computer to carry out the solution. These details include answers to questions such as the following.“Which store will I visit?”
“How will I get there: walk, drive, ride my bicycle, take the bus?”
“What kind of card does Animesh like: humorous, sentimental, risqué?”
Flowchart
A graphical representation of steps of a computer program in relation to solution to a problem.
Diagrams used in flowchart :
Conventions to be used in developing Algorithm and Flowchart:
- Each algorithm must be enclosed within START and STOP.
- Use keyword INPUT or READ to take user’s input.
- Use keyword PRINT or DISPLAY to show outputs to user.
- Arithmetical operators (+, -, *, /, etc) are used in mathematical expressions.
- Relational operators ( >, ==, ≥ etc) are used in conditions.
- Logical operators (AND, NOT, OR, etc) are used for logical expressions.
Exercise:
- Write an algorithm and draw flowchart for finding the sum of any two numbers.
Algorithm:
i. Start
ii. Display “Enter two numbers”
iii. Read A and B
iv. C= A+B
v. Display “C as the sum of two numbers”.
vi. Stop. - Calculate Simple Interest (SI = (P*T*R)/100)
- Find greatest number among 3 different numbers A, B and C.
- Sum of N numbers ( sum= 1+2+3 …).