Intro To CPP
Intro To CPP
Introduction to Programming
Introduction
No matter what field of work you choose, you may have to solve
problems. Many of these can be solved quickly and easily. Still others
require considerable planning and forethought if the solution is to be
appropriate and efficient. Problem in this sense is one that can be
solved using computers, some examples,
Requirements for:
Handling and manipulating employee data
Solving mathematical equations based on formula
Handling and manipulation of information about students etc.
A problem may be real problem (existing problem), or anticipated
(potential) problem. The reasons (initiation) for studying (identifying) a
given problems may be
Directives (in organizations)
Opportunities (to do things better)
Or other things.
Department of Computing 1
What part of the problem is going to be solved?
What input data is required to solve the problem?
What output data (result) is expected?
What procedures are needed to achieve the result?
Department of Computing 2
Figure below lists the relative amount of effort that is typically expended on
each of these four development and design steps in large commercial programming
projects.
Step Effort (%)
Analyzing the solution 10
Developing the solution 20
Coding the solution 20
Testing the solution 50
Department of Computing 3
Design and Implementation of Algorithms:
A. Pseudo code
Pseudo code is an artificial and informal language that helps
programmers to develop algorithms. Pseudo code is a language used
to describe the manipulations to be performed on data. This language
is a simple mixture of natural language and mathematical notations
and it is independent of programming language. Pseudo code has
some ways to represent sequence, decision and repetition in
algorithms.
Example: 1) Pseudo code to add two numbers.
Step 1: start
Step 2: Read two numbers n1 and n2.
Step 3: sum n1 + n2
Step 4: Print sum
Step 5: Stop
Example: 2) pseudo code to find largest number from two numbers.
Step 1: start
Step 2: Read two numbers n1 and n2.
Step 3: If n1 > n2 then
Big n1
else
Big n2
Step 4: Print Big
Step 5: Stop
Example: 3) pseudo code to find largest number from three numbers.
Step 1: start
Step 2: Read three numbers n1, n2 and n3.
Step 3: If n1 > n2 and n1 > n3 then
Big n1
Else
If n2 > n1 and n2 > n3 then
Big n2
else
Big n3
Step 4: Print Big
Step 5: Stop.
Department of Computing 4
Loops
Some times there is a situation in which it is necessary to
execute a group of statements repeatedly until some condition is
satisfied. This situation is called a loop. Loop is a sequence of
instruction, which is repeated until some specific condition occurs.
The general execution flow structure of loops looks like the following.
[Initialization]
true
[Compute]
[Increment/Decrement]
The above simple structure tells that execution continues as far as the
test holds true.
Example 4) Pseudo code to find sum of N positive integer numbers.
Step 1: start
Step 2: Read N
Step 3: Sum 0,
Step 4: Count 0
Step 5: Read Num
Step 6: SumSum + Num
Step 7: count count +1
Step 8: If Count < N then goto step 5
Step 9: Print Sum
Step 10: Stop
Assignment:
1) Write an algorithm to find the roots of quadratic equation
. Using the formula x= . Depending on
the values of D Where
2) Write an algorithm to check whether the given number is even or
odd and display the suitable message.
B. Flow charts:
Due to the detail required of them, programming languages are not
convenient tools for initial algorithm design. The means of notation
widely used for algorithm is a flowchart. Flowchart is a two-
dimensional representation of an algorithm; the predefined graphic
symbols of a flowchart are used to indicate the various operations and
the flow of control. The most significant advantage of flowcharts is a
clear presentation of the flow of control in the algorithm, i.e. the
sequence in which operations are performed.
Department of Computing 5
A basic set of established flowchart symbols is:
Decision
Processing Input/output
Connector
Annotation
Flow lines
The symbols have the following meanings:
Processing: one or more computational tasks are to be performed
sequentially
Input/Output: Data are to be read into the computer memory from
an input device or data are to be passed from the memory to an
output device. (Parallelogram)
Decision: two alternative execution paths are possible. The path to
be followed is selected during the execution by testing whether or not
the condition specified within the outline is fulfilled. (Rhombus)
Terminals: appears either at the beginning of a flowchart (and
contains the word “start”) or at its conclusion (and contains “stop”).
Annotation: contains comments that simplify the understanding of
the algorithm or description of data.
Connector: makes it possible to separate a flowchart into parts.
Identical cross reference symbols are placed in this outline where the
flow line is interrupted and where it resumes.
Flow lines: indicates the outline that is to be entered next.
Flowcharts allow the reader to follow the logic of the algorithm more
easily than would a linear description in English.
1. A flowchart to find factorial 2. A Flowchart to find
of a positive integer n largest of two numbers.
START
Start
Input n
Read A,
B.
F=1
Yes No
Is
No A>B ?
n> Display F
0? Yes
Display Display
END
F= F*n A is B is
Largest Largest
Flowchart versus pseudo code
Stop
n = n -1
Department of Computing 6
Since flowcharts are inconvenient to revise, they have fallen out of
favour by programmers. Nowadays, the use of pseudo code has gained
increasing acceptance.
Only after an algorithm has been selected and the programmer
understands the steps required can the algorithm be written using
computer-language statements. The writing of an algorithm using
computer-language statements is called coding the algorithm, which is
the third step in our program development process.
Exercise:
Algorithms:
1. Write an algorithm to find the smallest number from three
numbers.
2. Write an algorithm to find the sum of first N even numbers.
3. Write an algorithm to generate Fibonacci series.(a series which
goes like 1,1,2,3,5,8,13,…)
4. Write an algorithm to find the sum of digits of given number.
(Eg. If the given number is 251, the sum of digits is 2+5+1=8)
Flowcharts:
1. Draw a flowchart to find sum of N positive numbers.
2. Draw a flowchart to find the biggest among N numbers.
Department of Computing 7