U-1,C-1 (Algorithms and Flowcharts)
U-1,C-1 (Algorithms and Flowcharts)
FLOWCHARTS
The Big Picture (SDLC)
◼ Problem definition
◼ Analysis
◼ Design
◼ Coding
◼ Running the program
◼ Debugging
◼ Testing
◼ Documentation
ALGORITHMS AND FLOWCHARTS
◼ A typical programming task can be divided into
two phases:
◼ Problem solving phase
produce an ordered sequence of steps that describe
solution of problem
this sequence of steps is called an algorithm
◼ Implementation phase
implement the program in some programming
language
Steps in Problem Solving
◼ First produce a general algorithm (one can use
pseudocode)
◼ Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
◼ Pseudocode is an informal language that helps
programmers develop algorithms. Pseudocode
is very similar to everyday English.
Pseudocode & Algorithm
◼ Example: Write an algorithm to determine
a student’s final grade and indicate
whether it is passing or failing. The final
grade is calculated as the average of four
marks.
Pseudocode & Algorithm
Pseudocode:
◼ Input a set of 4 marks
◼ Calculate their average by summing and dividing
by 4
◼ if average is below 50
Print “FAIL”
else
Print “PASS”
Algorithmic Notations
◼ Name of the algorithm
◼ Step number
◼ Explanatory comment
◼ Termination
Characteristics of an Algorithm
◼ Input: It may accept zero or more inputs.
◼ Output: It should produce at least one result.
◼ Definiteness: clear, precise no ambiguity.
◼ Finiteness: in sequence, no infinite loops.
◼ Effectiveness: must solve the given problem for any
input.
Pseudocode & Algorithm
◼ Detailed Algorithm
Algorithm: Determine Results
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
Step 4: Stop
The Flowchart
◼ (General) A schematic representation of a sequence of
operations, as in a manufacturing process or computer
program.
◼ (Technical) A graphical representation of the sequence of
operations in a system or program.
System flowcharts show how data flows from source
modules through the computer to other modules.
Program flowcharts show the sequence of
instructions in a single program or subroutine.
Different symbols are used to draw each type of
flowchart.
The Flowchart
A Flowchart
Is a pictorial representation of an algorithm
Shows logic of an algorithm
Emphasizes individual steps and their
interconnections
Ex. control flow from one action to the next
Flowchart Symbols
Example
START
Algorithm: Determine Results
Step 1: Input M1,M2,M3,M4
Input
M1,M2,M3,M4 Step 2: GRADE (M1+M2+M3+M4)/4
Step 3: if (GRADE <50) then
GRADE(M1+M2+M3+M4)/4
Print “FAIL”
else
Print “PASS”
N IS Y endif
GRADE<50
Step 4: Stop
PRINT PRINT
“PASS” “FAIL”
STOP
Example
Write an algorithm and draw a flowchart that
will read the two sides of a rectangle and
calculate its area.
Pseudocode
◼ Input the width (W) and Length (L) of a rectangle
◼ Calculate the area (A) by multiplying L with W
◼ Print A
Example
Algorithm: Compute Area START
◼ Step 2: A L x W
W, L
◼ Step 4: Stop
Print
A
STOP
Example
◼ Write an algorithm and draw a flowchart that
will calculate the roots of a quadratic equation
ax 2 + bx + c = 0
◼ Hint: d = sqrt ( b 2 − 4ac ), and the roots are:
x1 = (–b + d)/2a and x2 = (–b – d)/2a
Example
Pseudocode:
◼ Input the coefficients (a, b, c) of the
quadratic equation
◼ Calculate d
◼ Calculate x1
◼ Calculate x2
◼ Print x1 and x2
Example
START
Print
x1 ,x2
STOP
Example
◼ Write an algorithm that reads two values, determines the
largest value and prints the largest value with an
identifying message.
ALGORITHM: Largest of 2 values
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX VALUE1
else
MAX VALUE2
endif
Step 3: Print “The largest value is”, MAX
Step 4: Stop
Example
START
Input
VALUE1,VALUE2
Y is
N
VALUE1>VALUE2
Print
“The largest value is”,
MAX
STOP
Introduction to C
◼ Structured Programming
Language
◼ High level
◼ Machine Independent,
allows development
without worrying about
the underlying hardware.
History of C
◼ ALGOL (1960)- Root of all the modern
languages (Structured Programming – Block
structure)
◼ BCPL (1967) was used for writing system
software.
◼ B (1970) by Ken Thompson. It was used to
create earlier versions of UNIX OS.
◼ C (1972) evolved using ALGOL, BCPL and B by
Dennis Ritchie at Bell Laboratories.
History of C (Continued)
◼ “The C Programming Language” book by
Kerningham and Ritchie (K&R C).
◼ Development of different version of C. They
were not compatible and was a serious concern
for developers.
◼ ANSI (American National Standards Institute)
formed a committee to standardize C.
◼ ANSI C was approved by International
Standards Organization (ISO) – C89.
◼ New features were added to C in 1999 – C99.
Importance of C
◼ Robust, rich build-in functions
◼ Efficient and fast
◼ Portable
◼ Functions/Blocks
◼ Extendable
Sample Program
// Sample C Program
#include<stdio.h>
void main()
{
// This is a single line comment
printf (“C-Programming class”);
/* This is a multiline
comment */
}
Sample Program (Continued)
◼ printf and PRINTF are not same in C. It is
case sensitive.
◼ Everything is written in lowercase with a
few exceptions.
The main function
◼ The main is part of every C program.
◼ C allows different forms of main statement.
main()
main(void)
void main()
void main(void)
int main()
int main(void)
The #include directive
◼ Modules or functions can be defined by users.
Built-in functions can be used by programmer.
◼ These related functions are group in header
files.
◼ If we want to access these functions, then we
have to tell the compiler about the same.
◼ Ex. #include <stdio.h>, #include<math.h>,
#include<string.h>
Executing a ‘C’ Program
◼ Creating the program
◼ Compiling the program
◼ Linking the program with C library
◼ Executing the program
Prog1.c
Prog1.o or
Prog1.obj
a.out or
Prog1.exe