Algorithm and flowchart
Algorithms and flowcharts are two different ways of presenting the process
of solving a problem. Algorithms consist of steps for solving a particular
problem, while in flowcharts, those steps are usually displayed in shapes and
process boxes with arrows. So flowcharts can be used for presenting
algorithms.
Algorithm in C Language:
Algorithm is a step-by-step procedure, which defines a set of instructions to
be executed in a certain order to get the desired output. Algorithms are
generally created independent of underlying languages, i.e. an algorithm can
be implemented in more than one programming language.
From the data structure point of view, following are some important categories
of algorithms
Search − Algorithm to search an item in a data structure.
Sort − Algorithm to sort items in a certain order.
Insert − Algorithm to insert item in a data structure.
Update − Algorithm to update an existing item in a data structure.
Delete − Algorithm to delete an existing item from a data structure.
Characteristics of an Algorithm:
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics
Unambiguous − Algorithm should be clear and unambiguous. Each of its
steps (or phases), and their inputs/outputs should be clear and must lead to
only one meaning.
Input − An algorithm should have 0 or more well-defined inputs.
Output − An algorithm should have 1 or more well-defined outputs, and
should match the desired output.
Finiteness − Algorithms must terminate after a finite number of steps.
Feasibility − Should be feasible with the available resources.
Independent − An algorithm should have step-by-step directions, which
should be independent of any programming code.
Rules of Drawing Flowcharts for Algorithms:
Flowcharts usually consist of basic symbols and shapes that have a specific
representation. The symbols make the chart more meaningful and easy to
understand.
Some of the most standard symbols include shapes like a rectangle
representing an operation, a parallelogram showing input and output of data,
a cylinder indicating the start/end, while a diamond represents a decision.
However, knowing these symbols well and using them in a flowchart involves
critical thinking. Here are some basic rules that need to be considered while
drawing a flowchart.
1. All the boxes and shapes in a flowchart should be connected with
arrows to indicate a logical order and make it easy to follow.
2. Make sure the flow direction is consistent and not ambiguous. The flow
charts always flow from either left to right or top to bottom.
3. Every chart will begin with a Start box and end with a Terminal box,
with the latter only having one flow line associated with it.
4. All the shapes, symbols, and lines should be consistent. You can also
add a symbol key to make it easier to follow the chart.
Flow chat symbols:
Algorithm and Flowchart Examples:
Example 1: Determine and Output Whether Number N is Even or Odd
Step 1: Read number N.
Step 2: Set remainder as N modulo 2.
Step 3: If the remainder is equal to 0 then number N is even, else
number N is odd.
Step 4: Print output.
Example 2: Calculate the Interest of a Bank Deposit
Step 1: Read amount.
Step 2: Read years.
Step 3: Read rate.
Step 4: Calculate the interest with the formula
"Interest=Amount*Years*Rate/100.
Step 5: Print interest.
Example 3: Determine Whether a Temperature is Below or Above the
Freezing Point
Step 1: Input temperature.
Step 2: If it is less than 32, then print "below freezing point", otherwise
print "above freezing point".
Example 4: Determine Whether A Student secured first class or Not in
the Exam.
Step 1: Input grades of 4 courses M1, M2, M3 and M4.
Step 2: Calculate the average grade with the formula
"Grade=(M1+M2+M3+M4)/4".
Step 3: If the average grade is less than 60, print "FAIL", else print
"PASS".
Example 5: Find the Sum of Two Numbers Entered
Step 1: Read the Integer A.
Step 2: Read Integer B.
Step 3: Perform the addition by using the formula: C= A + B.
Step 4: Print the Integer C.
Example 6: Determining the Largest Number Among All the Entered
Integers
Step 1: Read the Integer A.
Step 2: Read Integer B.
Step 3: If B is greater than A, then print B, else A.
Example 7: Work Out All the Roots of a Quadratic Equation ax² +bx+c=0
Step 1: Enter the variables a, b, c, D, x1, x2, rp, and ip.
Step 2: Evaluate the discriminant by using the formula: D= b²- 4ac
Step 3: Print rp and ip.
Example 8: Find the Fibonacci series till term ≤1000
Step 1: Declare the variables i, a, b, show.
Step 2: Enter the values for the variables, a=0, b=1, show=0
Step 3: Enter the terms of the Fibonacci series to be printed, i.e=, 1000.
Step 4: Print the first two terms of the series.
Step 5: Loop the following steps:
o Show = a + b
o a= b
o b = show
o Add 1 to the value of i each time.
o Print Show
Example 9: Calculate the Sum of The First 50 Numbers
Step 1: Declare number N= 0 and sum= 0
Step 2: Determine N by N= N+1
Step 3: Calculate the sum by the formula: Sum= N + Sum.
Step 4: Add a loop between steps 2 and 3 until N= 50.
Step 5: Print Sum.
Example 10: Find the largest price among 100 given values and reduce
it by 10%
Step 1: Read the 100 prices.
Step 2: Compare the first price with the next and let the greater of the
two be ‘max’ in the ‘max index.’
Step 3: Loop it until the largest price has been found.
Step 4: Reduce the ‘max’ value by 10% using the formula: prices [max
index] = prices [max index] x 0.9.
Step 5: Print.
Example 11: Arrange the numbers X, Y, Z in descending order
Step 1: Input the value for variables X, Y, Z.
Step 2: Read the integers X, Y, Z.
Step 3: Check if x>y, x>z, y>z
Step 4: Print the possible order.
Example 12: Determine the Highest Common Factor (HCF) of two
integers
Step 1: Read the variables a and b.
Step 2: If a = b, go to step 4.
Step 3: If a > b, then: a = a - b. Return to step 2.
Step 4: Print a or b.
Pseudo code in C
The pseudo code in C consists of words and phrases that make pseudo code
looks similar to the program but not a program. Pseudo codes are written with
respect to a programming language, but programming language syntax or
grammar is not strictly followed.
The pseudo-code is neither an algorithm nor a program. pseudo-code is a
semi-formal description of the steps to be carried out by the computer,
including Steps that are to be repeated and decisions that are to be made but
it constructs/models and maybe even look like programming code.
Pseudocode is made up of two words, ‘pseudo’ and ‘code’. Pseudo means
imitation and code refer to instructions, written in a programming language.
Pseudocode is also known as Program Design Language (PDL) or Structured
has the following characteristics:
A free syntax of natural language that describes a processing feature.
A subprogram definition and calling techniques.
Fixed syntax of keywords that provide for all structured constructs,
data declarations and modularity characteristics.
A data declaration facility.
Pseudocode is a set of sequential written human language instructions,
usually numbered, that is used to describe the actions a program will take
when it is coded in a programming language.
Pseudocode Examples
1. Factorial of a positive integer n is product of all values from n to 1.
For example, the factorial of 3 is (3 * 2 * 1 = 6).
Algorithm
Algorithm of this program is very easy
START
Step 1 → Take integer variable A
Step 2 → Assign value to the variable
Step 3 → From value A upto 1 multiply each digit and store
Step 4 → the final stored value is factorial of A
STOP
Pseudocode
We can draft a pseudocode of the above algorithm as follows −
procedure find_factorial(number)
FOR value = 1 to number
factorial = factorial * value
END FOR
DISPLAY factorial
end procedure
2.
If student's grade is greater than or equal to 60
Print "passed"
else
Print "failed"
Endif
2.
Set total to zero
Set grade counter to one
While grade counter is less than or equal to ten
Input the next grade
Add the grade into the total
endwhile
Set the class average to the total divided by ten
Print the class average.
3.
Initialize total to zero
Initialize counter to zero
Input the first grade
while the user has not as yet entered the sentinel
add this grade into the running total
add one to the grade counter
input the next grade (possibly the sentinel)
endwhile
if the counter is not equal to zero
set the average to the total divided by the counter
print the average
else
print 'no grades were entered'
endif
4.
initialize passes to zero
initialize failures to zero
initialize student to one
while student counter is less than or equal to ten
input the next exam result
if the student passed
add one to passes
else
add one to failures
add one to student counter
endif
endwhile
print the number of passes
print the number of failures
if eight or more students passed
print "raise tuition"
endif
5.
Larger example:
NOTE: NEVER ANY DATA DECLARATIONS IN PSEUDOCODE
Print out appropriate heading and make it pretty
While not EOF do:
Scan over blanks and white space until a char is found
(get first character on the line)
set can't-be-ascending-flag to 0
set consec cntr to 1
set ascending cntr to 1
putchar first char of string to screen
set read character to hold character
While next character read != blanks and white space
putchar out on screen
if new char = hold char + 1
add 1 to consec cntr
set hold char = new char
continue
endif
if new char >= hold char
if consec cntr < 3
set consec cntr to 1
endif
set hold char = new char
continue
endif
if new char < hold char
if consec cntr < 3
set consec cntr to 1
endif
set hold char = new char
set can't be ascending flag to 1
continue
endif
end while
if consec cntr >= 3
printf (Appropriate message 1 and skip a line)
add 1 to consec total
endif
if can't be ascending flag = 0
printf (Appropriate message 2 and skip a line)
add 1 to ascending total
else
printf (Sorry message and skip a line)
add 1 to sorry total
endif
end While
Print out totals: Number of consecs, ascendings, and sorries.
Stop