Basic Programming Concepts
Some Terminologies
• Algorithm / Flowchart
– A step-by-step procedure for solving a particular problem.
– Independent of the programming language.
• Program
– A translation of the algorithm/flowchart into a form that can be
processed by a computer.
– Typically written in a high-level language like C, C++, Java,
etc.
Dept. of CSE, IIT KGP
Variables and Constants
• Most important concept for problem solving using computers
• All temporary results are stored in terms of variables
– The value of a variable can be changed.
– The value of a constant do not change.
• Where are they stored?
– In main memory.
Dept. of CSE, IIT KGP
Contd.
• How does memory look like (logically)?
– As a list of storage locations, each having a unique address.
– Variables and constants are stored in these storage locations.
– A variable is like a bin
• The contents of the bin is the value of the variable
• The variable name is used to refer to the value of the variable
• A variable is mapped to a location of the memory, called its
address
Dept. of CSE, IIT KGP
Memory map
Address 0
Address 1
Address 2
Address 3
Every variable is
Address 4
mapped to a particular
Address 5
memory address
Address 6
Address N-1
Dept. of CSE, IIT KGP
Variables in Memory
Instruction executed Variable X
X = 10 10
T
i X = 20 20
m
e
X = X +1 21
X =X *5 105
Dept. of CSE, IIT KGP
Variables in Memory (contd.)
Variable
Instruction executed
X Y
X = 20 20 ?
T
i Y = 15 20 15
m
e
X = Y +3 18 15
Y = X /6 18 3
Dept. of CSE, IIT KGP
Data Types
• Three common data types used:
– Integer :: can store only whole numbers
• Examples: 25, -56, 1, 0
– Floating-point :: can store numbers with fractional values.
• Examples: 3.14159, 5.0, -12345.345
– Character :: can store a character
• Examples: ‘A’, ‘a’, ‘*’, ‘3’, ‘ ’, ‘+’
Dept. of CSE, IIT KGP
Data Types (contd.)
• How are they stored in memory?
– Integer ::
• 16 bits
• 32 bits
Actual number of bits vary from
– Float ::
one computer to another
• 32 bits
• 64 bits
– Char ::
• 8 bits (ASCII code)
• 16 bits (UNICODE, used in Java)
Dept. of CSE, IIT KGP
Problem solving
• Step 1:
– Clearly specify the problem to be solved.
• Step 2:
– Draw flowchart or write algorithm.
• Step 3:
– Convert flowchart (algorithm) into program code.
• Step 4:
– Compile the program into object code.
• Step 5:
– Execute the program.
Dept. of CSE, IIT KGP
Flowchart: basic symbols
Computation
Input / Output
Decision Box
Start / Stop
Dept. of CSE, IIT KGP
Contd.
Flow of
control
Connector
Dept. of CSE, IIT KGP
Example 1: Adding three numbers
START
READ A, B, C
S = A + B +C
OUTPUT S
STOP
Dept. of CSE, IIT KGP
Example 2: Larger of two numbers
START
READ X, Y
YES IS NO
X>Y?
OUTPUT X OUTPUT Y
STOP STOP
Dept. of CSE, IIT KGP
Example 3: Largest of three numbers
START
READ X, Y, Z
YES IS NO
X > Y?
Max = X Max = Y
YES IS NO
Max > Z?
OUTPUT Max OUTPUT Z
STOP STOP
Dept. of CSE, IIT KGP
Example 4: Sum of first N natural numbers
START
READ N
SUM = 0
COUNT = 1
SUM = SUM + COUNT
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT SUM
STOP
Dept. of CSE, IIT KGP
Example 5: SUM = 12 + 22 + 32 + N2
START
READ N
SUM = 0
COUNT = 1
SUM = SUM + COUNT COUNT
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT SUM
STOP
Dept. of CSE, IIT KGP
Example 6: SUM = 1.2 + 2.3 + 3.4 + to N terms
START
READ N
SUM = 0
COUNT = 1
SUM = SUM + COUNT (COUNT + 1)
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT SUM
STOP
Dept. of CSE, IIT KGP
Example 7: Computing Factorial
START
READ N
PROD = 1
COUNT = 1
PROD = PROD * COUNT
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT PROD
STOP
Dept. of CSE, IIT KGP
Example 8: Computing ex series up to N terms
START
READ X, N
TERM = 1
SUM = 0
COUNT = 1
SUM = SUM + TERM
TERM = TERM * X / COUNT
COUNT = COUNT + 1
NO IS YES
COUNT > N? OUTPUT SUM
STOP
Dept. of CSE, IIT KGP
Example 8: Computing ex series up to 4 decimal places
START
READ X, N
TERM = 1
SUM = 0
COUNT = 1
SUM = SUM + TERM
TERM = TERM * X / COUNT
COUNT = COUNT + 1
NO IS YES
TERM < 0.0001? OUTPUT SUM
STOP
Dept. of CSE, IIT KGP
Example 10: Roots of a quadratic equation
ax2 + bx + c = 0
TRY YOURSELF
Dept. of CSE, IIT KGP
Example 11: Grade computation
MARKS 90 Ex
89 MARKS 80 A
79 MARKS 70 B
69 MARKS 60 C
59 MARKS 50 D
49 MARKS 35 P
34 MARKS F
Dept. of CSE, IIT KGP
Grade Computation (contd.)
START
READ MARKS
NO NO NO
MARKS 90? MARKS 80? MARKS 70? A
YES YES YES
OUTPUT “Ex” OUTPUT “A” OUTPUT “B”
STOP STOP STOP
Dept. of CSE, IIT KGP
NO NO NO
A MARKS 60? MARKS 50? MARKS 35?
YES YES YES
OUTPUT “C” OUTPUT “D” OUTPUT “P” OUTPUT “F”
STOP STOP STOP STOP
Dept. of CSE, IIT KGP