You are on page 1of 2

Algorithm Flow-charts are important as they provide a pictorial view of the program flow and hence

provide a better understanding of the complete process or algorithm.


Let us take a simpler example, say we wish to calculate the area of a circle given the radius of
the circle. For doing so, we would follow the following algorithm:
#include <iostream.h> Symbol Description
void main() This symbol is used for the beginning and the end of a
{ program or of a module of a program. The words
1. Get a value for r, the float r; START (alternatively BEGIN) or STOP (END) would
radius of the circle cout << “Enter the radius of the circle: “; typically be entered in this symbol.
cin >> r; This symbol is used for all processes in a program. A
2. Set the value of area float a = 3.1416 * r * r; process is generally used for arithmetic/computational
to (pi*r )
2 operations.
3. Print the value of area cout << “The area of the circle is “ << a << “.\n”; Used to test for a given condition. Testing whether one
} quantity is larger than another is one example of a
testing/decision operation. Similarly, testing whether a
specific quantity is positive or zero is another typical
Flow-Charts test. 'IF...THEN...ELSE statements would typically
Flow-charts can be defined as maps or pictorial representations of a well defined process where characterize this test operation
the various steps in a process are depicted with symbolic shapes and the flow of the process is This symbol is used for input or output operations. For
indicated with arrows connecting these symbolic shapes. example, if we want to input the radius of a circle or
output the calculated area of the circle.
Flow-chart: Calculating area of a circle
Start #include <iostream.h>
Pseudo Code
// start of the program
void main() A ‘pseudo code’ is an outline of a program, written in a form that can easily be converted into
Enter the
{ real programming statements. Pseudo code can neither be compiled nor executed, and there is
radius
float r; no real formatting or syntax rules. The benefit of pseudo code is that it enables the
cout << “Enter the radius of the circle: “; programmer to concentrate on the algorithms without worrying about all the syntactic details of
cin >> r; // enter the radius a particular programming language. In fact, you can write pseudo code without even knowing
what programming language you will use for the final implementation.
Area = π r2 float a;
// solve for the area of the circle For the same example:
a = 3.1416 * r * r; Step 1: Define a constant PI .
Things to do
Print the // display the area of the circle  Using the ‘# define’ preprocessor directive declare a constant PI = 3.1416
Area of the cout << “The area of the circle is “ << a << “.\n”;
circle Step 2: Enter the radius of the circle from the user.
} // end of the program Things to do
 Declare a variable ‘radius’ of type int.
End  Using the ‘cin’ statement to input ‘radius’ from the user.
Step 3: Calculate the area of the circle and display it on the screen. converted to electrical energy. Note that the mass of one cubic meter of water is 1000 kg.
Things to do Use 9.80 meters/second2 as the gravitational constant g. Be sure to use meaningful names
 Declare a float variable ‘area’ for both the gravitational constant and the 90% efficiency constant. For one run, use a
 Assign the value obtained by the formula (PI * (radius)2 ) to the variable ‘area’. height of 170 m and flow of 1.30 x 103 m3/s. The relevant formula (w = work, m = mass, g
 Display the variable ‘area’ on the screen with the help of the ‘cout’ statement. = gravity, h = height) is w = mgh

The resulting program would be: 2. A manufacturer wishes to determine the cost of producing an open-top cylindrical
# include<iostream.h> container. The surface area of the container is the sum of the area of the circular base plus
# include<conio.h> the area of the outside (the circumference of the base times the container height). Write a
// step 1 program to take the radius of the base, the height of the container, the cost per square
# define PI 3.1416 centimeter of the material, and the number of containers to be produced. Calculate the cost
of each container and the total cost of producing all the containers.
void main()
{ 3. Wavelengths of visible light fall approximately into the range 0.4-0.7 microns. The table
// Step 2 below shows the relationship between wavelength and color. Write a program that inputs a
int radius = 0; wavelength and then displays the associated light color. If the wavelength is shorter than
float area = 0; // from step 3 0.4 microns or longer than 0.7, display the message Wavelength outside visual range.
cout << " Enter the radius of the circle\n"; Classify boundary wavelengths as the lower-wavelength color. For example, label a
cin >> radius; wavelength of 0.424 microns as violet.
Color Approximate wavelength range (microns)
// step 3 Violet 0.400-0.424
area= PI * (radius * radius); Blue 0.424-0.491
cout << "\n The area of the circle is"<<area; Green 0.491-0.575
getch(); // pause the program Yellow 0.575-0.585
// program exits after pressing any key Orange 0.585-0.647
} Red 0.647-0.700

The getch() statement belongs to the conio.h library. This extracts a character keyed in by the 4. Write a program that reports the contents of a compressed-gas cylinder based on the
user through the keyboard. This can also be used to pause programs. first letter of the cylinder's color. The program input is a character representing the
observed color of the cylinder: ' Y ' or ' y ' for yellow, ' 0' or ' o ' for orange, and so on.
Another function, the getche(), has the same characteristics except that it displays the character Cylinder colors and associated contents are as follows:
on the screen as being pressed while the getch() does not.
Orange ammonia
Also commonly used is the clrscr() statement. This clears the user screen. Brown carbon monoxide
Yellow hydrogen
Green oxygen
Sample problems:
Your program should respond to input of a letter other than the first letters of the given
1. Write a program to assist in the design of a hydro-electric dam. Prompt the user for the
colors with the message, Contents unknown.
height of the dam and for the number of cubic meters of water that are projected to flow
from the top to the bottom of the dam each second. Predict how many megawatts (MW —
1MW=106W) of power will be produced if 90% of the work done on the water by gravity is

You might also like