Tuesday 31 January 2012

Algorithm and Flow charts


                                                                    Algorithm

Introduction:

In order to develop any problem in computer science generally we write programs before writing programming languages . It is always better to write we write a informal discretional for the solution, Which is also called as algorithm.

An algorithm is just like a cooking recipes for example:

If you want to prepare any tea:
Ingredients:
1.       2 cups of water
2.       Tea powder
3.       2 table spoon sugar
4.       Match box to light the gas stove
5.       Bowl
6.       Cups are required to serve
7.       1 cup of Milk
Instructions:
    Step 1: switch on the gas stove
    Step 2: Bowl should be kept on the gas
    Step 3: pour the water into the bowl    
    Step 4: add two tea spoon of tea powder
    Step 5: Wait until it is boiled
    Step 6:  take 2 tea spoons of sugar and add into the boiled water
    Step 7:  Wait for 2 minus for boiling the water
    Step 8:  add milk to the boiled water
    Step 9:  Let it be boiled
    Step 10: Now Tea is ready
    Step 11: Switch off the gas stove.

In the above instructions are fairly precised these instruction are not required for Expert person. These instructions are useful for beginners, in the same way algorithms are very much important to the beginners.

Now we see we have some problem.

Problem-> programs like c

Before writing the program if u write a blue print it is informal language ie. English like language.
To describe what we want to write so that it will be easier for us to communicate before implementation.
So that is called algorithm.
So before going for any problem to solve the problem for writing the program we need the algorithm.
In general if there is a problem p1, there might be many solutions. Let me call these solutions are algorithms. So there may be many algorithms(solutions) like A1,A2,A3.,,,,
Now before implementing any algorithm as a program we have to find out which algorithms are good in terms of time and memory. So we should analyze every algorithm like time here time means how much time is taking to execute. And Second is Memory which one will take less memory. So less time and less memory. So this code design analysis of algorithm .
Design Analysis of Algorithm: here design talk about how to design various algorithms like for a given problem (example p1), how many algorithms are there and how to analyze them so what will be time taken this algorithm, what will be the memory by each algorithm.
Once we selected the best algorithm example A2 is the best algorithm, then I implement that algorithm. So that is the main process of designing the algorithms.


What is Algorithm?




Definetion:

Algorithm is used to solution of a problem step by step procedure using simple english language. It is the part of software designing. An algorithm defines as the step by step procedure or method that can be carried out for solvingprogramming problems. An algorithm tells computer that how to solve problem systematically to get desired output. Mainly we follow following steps to design an algorithm.

Step 1 - START
It represents beginning of the algorithm.

Step 2 - DECLARE
The variables used in algorithm are declared in this step.

Step 3 - INPUT
Here we input the values

Step 4 - FORMULA
The required result is generated in this step.

Step 5 - OUTPUT
It displays the output or result.

Step 6 - STOP
It is an end of Algorithm




                                                  
              FLOW CHART
Flow char is a graphical(pictorial) representation of algorithm. It is used to represent algorithm steps into graphical format. It also gives us an idea to organize the sequence of steps or events necessary to solve a problem with the computer. In other words flow charts are symbolic diagrams of operations and the sequence, data flow, control flow and processing logic in information processing. These charts are used to understand any working sequence of the program.

What are the Advantages(uses) of FLOW CHART?
Ans:
Advantages of flowchart:-

1. It provides an easy way of communication because any other person besides the programmer can understand the way they are represented.

2. It represents the data flow.

3. It provides a clear overview of the entire program and problem and solution.

4. It checks the accuracy in logic flow.

5. It documents the steps followed in an algorithm.

6. It provides the facility for coding.

7. It provides the way of modification of running program.

8. They shows all major elements and their relationship.

erere


Terminator
This symbol represents the beginning and end point in a program. We use start and stop option in it.

Input/Output Symbol
This symbol is used to take any input or output in the algorithm.

Process Symbol
A rectangle indicates the processing, calculation and arithmetic operations

Decision Symbol
It is used when we want to take any decision in the program.

Connector Symbol
This symbol is used to connect the various portion of a flow chart. This is normally used when the flow chart is split between two pages

Data Flow Symbol
This symbol is used to display the flow of the program. It shows the path of logic flow in a program.

1. Write a c program to display "this is my first c program" on monitor
ans:
ALGORITHM
1. START
2. use the printf() function to print the message "this is my first c program".
3. use getch() function to see the output
4. STOP

FLOW CHART

2. Write a c program to find the area of a triangle for the given three sides
ans:
Let a,b,c be the lengths of the sides of a triangle. The area is given by:
Area = √s(s−a)(s−b)(s−c)  
where s is semiperimeter (half the perimeter)
a+b+c/2

 

Algorithm:

1. START
2. declare the floating variables a,b,c,s,area.
3. use the print() to print the message "enter three sides"
4. use the scanf() to read the values of sides from keyboard
5. write the logic for s. s=a+b+c/2
6. write the logic for area=root of (s(s-a)(s-b)(s-c)
7. use the printf() to print the area.
8. STOP

Flow chart

3. WAP to find whether a given year is leap or not?

To check whether a year is a leap year or not, you need to check the following 3

conditions:
1. Any year that is divisible by 400 is definitely a leap year.
2. If it is not divisible by 400, then check if it is divisible by 100, if so, then it is

NOT a leap year (even if it is divisible by 4), and
3. If the above two conditions are not satisfied we check for divisibility by 4, it it is

divisible by 4 it is a leap year.

For example,
(we know that 2000 is a leap year)
if year = 2400, it is leap year,(Condition 1 satisfied)
but if year = 2200, is NOT a leap year, (Cond. 2 satisfied),
and if year = 2020, is a leap year, (Cond. 3 satisfied).

You can use simple if else statements to write the program.
You can read Leap Years or Leap year for better understanding of why every 400th year is

taken as a leap year and not every 100th.

Algorithm

1. START
2. declare year of type integer.
3. use the print() to print the message "enter the year"
4. use the scanf() to read the year from keyboard
5. check the following condition with if..else statement
 (year%4==0&&year%100!=0 || year%400==0)
 (if an year is divisible by 4 AND not divisible by 100, OR is divisible by 400 then it is called Leap Year
6. if the above condition is TRUE then Print "leap year"
7. if the above condition is FALSE then Print "not a leap year".
8. STOP




To Determin the given number is positive, negative or zero
to find sum of first number

Algorithm to find the sum of first n natural numbers:
1. Read n.
2. Initialize a=1.
3. Initialize sum s=0.
4. Calculate s=s+a.
5. Calculate a=a+1.
6. if a>n, then goto step 7 else goto step 4.
7. Write the sum s.
8. Stop.






Draw a flowchart to find the largest of three numbers A, B, and C.








You may like the following posts:

Do you want Training for this course
 
More algorithms
programs on  Algorithms
programs on flowchart

5 comments: