Script 1 1
Script 1 1
Dear friends,
In Today’s discussion we will cover the very beginning and
general concepts related with problem solving and programming.
Algorithms and Flowcharts are the most effective tools, used for both
problem solving and programming. In this module we will discuss
about the problem solving approach based on flowchart.
Flowchart for structured programing
Structured programming is a programming paradigm aimed at
improving the clarity, quality, and development time of a computer
program by making extensive use of subroutines, block structures
and loops. Before going to jump into the structured programming
terminology we need to find an optimal and clear solution for the
problem. One of the best way to represent your solution of a
particular problem is a flowchart.
Page - 1
numbers'. Select appropriate symbols while designing the flowchart
(the symbols are discussed later in this section).
4. Efficient Coding
Once a flowchart is ready, programmers find it very easy to write the
concerned program because the flowchart acts as a road map for
them. It guides them in proceeding from the starting point of the
program to the final point ensuring that no steps are omitted. The
ultimate result is an error free program developed at a faster rate.
5. Systematic Debugging
Even after taking full care in program design, some errors may
remain in the program because the designer might have never
thought about a particular case. These errors are detected only when
we start executing the program on a computer. Such type of program
errors are called bugs and the process of removing these errors is
known as debugging. A flowchart is very helpful in detecting, locating,
and removing mistakes (bugs) in a program in a systematic manner.
6. Systematic Testing
Testing is the process of confirming whether a program will
successfully do all the jobs for which it has been designed under the
specified constraints. For testing a program, different sets of data are
Page - 2
fed as input to that program to test the different paths in the program
logic.
Let x=5. After adding an integer value say ‘2’, the value of x will be
varied to 7.
Page - 3
numbers twice bigger than signed. This applies to numeric types and
could depend on the programming language.
Coming to C language, it has the following basic built-in data types.
in
float
double
char
int represents Integer ; It holds integer values.
For example:
int a;
Here “a” is the variable that holds integer type values.
Char represents character, holding one character.
Eg: char c;
Char data type can also be used to initialize a string as an array of
characters.
Eg: char name[10];
Here, name is declared as a string that represents an array which can
hold a maximum of 10 character elements.
Float holds single precision floating values.
Eg: float
Double holds double precision floating values.
Apart from these built in data types, C also supports user defined
data types which will be discussed in the later sections.
Programming Statements
Page - 4
present, the statement may have a value. If no expression is present,
the statement is often called the null statement.
Selection Statements: There are 3 types of selection statements
(i)Simple if
Syntax:
If (expression)
statement
In this type of if-statement, the statement will be executed only when
the expression is non-zero or true.
(ii) if else
Syntax:
If (expression)
statement 1;
else
statement 2;
In this type of if-statement, the statement 1 will be executed if the ex-
pression is non-zero; otherwise, the statement 2 will be executed.
(iii) Switch statements
Switch statements are also a type of selection statement. They have
the format of
switch (expression)
{
case 1:
statement 1;
break;
case 2:
statement 2;
break;
default:
statement 3 ;
break;
}
Iteration Statements: C has three kinds of iteration statements.
(i)The while statement.
While statement is of the form,
While(Control expression)
{
Page - 5
Statements
}
The statements inside the brackets run repeatedly until the control ex-
pression evaluates to zero. Otherwise it m
ay not run at all.
(ii) do while
do-while statement is of the form
do
{
Statements;
} while(control expression);
Here the control expression is evaluated at the end of the loop in-
stead of the beginning and consequently the statements inside the
brackets must execute at least once.
(iii) for
For statement of the form
for( initialization; condition;
increment/decrement )
{
Statement 1;
}
* The initialization step is executed first, and only once. This
step allows you to declare and initialize any loop control variables.
* Condition is evaluated. If it is true, the statement1 (body of
the loop) is executed. If it is false it does not execute and the flow
of control jumps to the next statement just after the 'for' loop.
* After the body of the 'for' loop executes, the flow of control
jumps back up to the increment statement. This statement allows
you to update any loop control variables.
Flowchart Symbols
Page - 6
A flowchart uses predefined symbols. The following table
shows the flow chart symbols such as terminal symbol, I/O process,
decision symbol etc.
Symbol Name
Start / end
Arrow
Input/ output
Process
Decision
Connector
Comment symbol
Page - 7
2. Input/output symbol: input/ output symbol is a parallelogram
that represents input or output to a program. If there is a pro-
gram instruction to input data from a disk, tape, card reader,
terminal, or any other type of input device, that step will be in-
dicated in the flowchart with an input/output symbol. Similarly,
all output instructions, whether it is output on a printer, mag-
netic tape, magnetic disk, terminal screen, or any output de-
vice, are indicated in the flowchart with an input/output symbol.
3. Process symbol: A rectangle is used to represents a process,
action or an operation, which mostly includes some verbs. This
symbol represents arithmetic and data movement instructions.
Thus, all arithmetic processes such as adding, subtracting,
multiplying and dividing are shown by a processing symbol.
The logical process of moving data from one location of the
main memory to another is also denoted by this symbol. When
more than one arithmetic and data movement instructions are
to be executed consecutively, they are normally placed in the
same processing box and they are assumed to be executed in
the order of their appearance. Eg: calculate sum (sum=A+B)
etc.
4. Decision symbol: A diamond indicates a decision used to rep-
resent a question in a flowchart. Decision symbols have a sin-
gle point of entry, but may have multiple exits. The exit is deter-
mined by the answer to the question and separated by differ-
ent arrows coming out of the diamond. Ex: there can be two
exits ‘yes’ and ‘no’. The condition upon which each of the pos-
sible exit paths will be executed, should be identified and all
the possible paths should be accounted for. During execution,
the appropriate path is followed depending upon the result of
the decision.
5. If a flowchart becomes very long, the flow lines start
crisscrossing at many places that causes confusion and
reduces the clarity of the flowchart. Moreover, there are
instances when a flowchart becomes too long to fit in a single
page and the use of flow lines becomes impossible. Thus,
whenever a flowchart becomes too complex that the number
and direction of flow lines is confusing or it spreads over more
than one page, it is useful to utilize the connector symbol as a
Page - 8
substitute for flow lines. This symbol represents an entry from,
or an exit to another part of the flowchart. A connector symbol
is represented by a circle and a letter or digit is placed within
the circle to indicate the link. A pair of identically labeled
connector symbols are commonly used to indicate a continued
flow when the use of a line is confusing. So two connectors
with identical labels serve the same function as a long flow
line. That is, they show an exit to some other chart section, or
they indicate an entry from another part of the chart. To
determine if a connector is used as an entry or an exit point,
we look whether if an arrow enters but does not leave a
connector, then it is an exit point and program control is
transferred to the identically labeled connector that does have
an outlet. It may be noted that connectors do not represent any
operation and their use in a flowchart is only for the sake of
convenience and clarity
6. A loop symbol indicates a sequence of commands that will
continue to repeat until a condition is satisfied.
7. Flow lines with arrowheads are used to indicate the flow of
operation, that is, the exact sequence in which the instructions
are to be executed. The normal flow of flowchart is from top to
bottom and left to right. Arrowheads are required only when the
normal top to bottom flow is not to be followed. However, as a
good practice and in order to avoid confusion, flow lines are
usually drawn with an arrowhead at the point of entry to a
symbol. Good practice also dictates that flow lines should not
cross each other and that such intersections should be
avoided whenever possible.
8. The comment symbol, also called as note symbol is used to
add an explanation or a comment, connected by a dashed line
to the relevant section of the flowchart.
The general tips to be considered while designing a Flowchart are as
follows:-
(a) First formulate the main line of logic, then incorporate the details.
(b) Maintain a consistent level of detail for a given flowchart.
(c) Do not give every detail on the flowchart. A reader who is
interested in greater details can refer to the program itself.
(d) Words in the flowchart symbols should be common statements
Page - 9
and easy to understand.
(e) Be consistent in using names and variables in the flowchart.
(f) Go from left to right and top to bottom in constructing the flowchart
1. Now we can design a simple Flowchart to find the sum of two num-
bers
The flowchart begins with the terminal symbol ‘start’. Here, there are
two variables n1 and n2. To read these values we use the input sym-
bol parallelogram. The value of sum is stored in the variable sum. The
process of finding sum is depicted in the flowchart using the rectan-
gle. The output value is represented using the parallelogram again.
This prints the value inside the variable sum. Finally the terminal sym-
bol ‘Stop’ ends the flowchart.
Page - 10
Here, we read 3 values to the variables A, B and C. First check
whether A is greater than B. If “Yes” then check A with C. If “Yes” then
print A is the largest number. If A is not greater than B then check B
with C. If “Yes” print B is the largest number. Otherwise C is the
largest number.
Summary
Page - 11
program in various fields.
Flowcharts are generally drawn in the early stages of
formulating computer solutions. Once the flowchart is drawn, it is
easy to write the program. A good flowchart is like a snapshot of a
program which tells you, at a glance, where the information
originates, how it handles the information and produces the desired
outputs A flowchart uses predefined symbols such as terminal
symbol, I/O process, decision symbol etc.
A computer program is made up of a series of statements.
Programming Statements are the commands given to the computer
that instructs the computer to do a specific action, such as display to
the screen, or collect input. Commonly used programming statements
are label statements, compound ststements, expression statements
and iterative statements.
Assignments
Reference
Page - 12