CP Unit 1
CP Unit 1
CP Unit 1
Text Books:
1. Programming in ANSI C : E. Balguruswami McGraw Hill
2. Mastering C: K. R. Venugopal and S. R. Prasad, Tata
McGraw Hill
Reference Books:
1. Programming with C: Byron Gottfried, Schaums Outline
Series.
2. Let Us C: Yashwant Kanetkar, BPB Publication
COURSE OUTCOMES:
On successful completion of course student will learn:
1. To formulate simple algorithms for arithmetic and logical problems, translate the
algorithms to programs (in C language), test and execute the programs and correct
syntax and logical errors.
3. To use arrays, pointers, structures and I/O operations for the formulation of
algorithms and programs.
Arithmetic
Input and
Logic Unit
Memory
I/O Processor
18
PROGRAMMING
Procedural
Defining set of steps to transform inputs into outputs
Translating steps into code
Constructed as a set of procedures
Each procedure is a set of instructions
Object-Oriented
Defining/utilizing objects to represent real-world entities
that work together to solve problem
Basic O-O Programming Components
Class
Object/Instance
Properties
Methods
PROBLEM SOLVING
31
FUNCTIONS OF OPERATING SYSTEM
1. Finiteness
2. Definiteness
3. Effectiveness
4. Generality
5. Input/output
35
Suppose we want to find the average of three numbers, the
algorithm is as follows:
36
Write an algorithm to calculate the simple interest using the
formula Simple interest = P*N* R/100. Where P is principle
Amount, N is the number of years and R is the rate of interest.
37
Write an algorithm to find the area of the triangle.
38
Write an algorithm to find the largest of three numbers X, Y,Z.
Step 5: Stop.
39
Write down an algorithm to find the largest data value of a set
of given data values.
Step 1: LARGE=0
Step 2: read NUM
Step 3:
While NUM > = 0 do
if NUM > LARGE then
LARGE=NUM
read NUM
Step 4: Write “largest data value is”, LARGE
Step 5: STOP
40
Write algorithm to find the factorial of a given number N
Step 1: Fact=1
Step 2: I=1
Step 3: read N
Step 4:
While I < N do
Fact =Fact* I
I=I + 1
Step 5: Write “Factorial of”, N, “is”, Fact
Step 6: end.
41
Algorithm to check Prime Number.
FOR loop = 2 to A - 1
check if number is divisible by loop
IF divisible
RETURN "NOT PRIME"
END IF
END FOR
RETURN "PRIME"
Step 3 → STOP
42
Find Root of the quadratic equation ax 2 + bx + c = 0
S te p 1 : S t a r t
S te p 2 : D e c l a r e v a r i a b le s a , b , c , D , r 1 , r 2 , r p a n d i p ;
S te p 3 : C a l c ula te
D ← b2-4ac
S te p 4 : I f ( D = = 0 )
r1=-b/2a
r2=-b/2a
D i spl ay r 1 a n d r 2 a s r o o t s .
e l s e i f (D > 0 )
r 1 ← ( - b + √D ) / 2 a
r 2 ← ( - b - √D ) / 2 a
D i s p lay r 1 a n d r 2 a s r o o t s .
else
C a l c ula te r e a l p a r t a n d i m a g i n a r y p a r t
rp ← -b/2a
ip ← √(-D)/2a
D i s p lay r p + j ( i p ) a n d r p - j ( i p ) a s r o o t s
S te p 5 : S to p
43
FLOWCHART
44
ADVANTAGES OF FLOWCHARTS
Parallelogram
48
FIND THE LARGEST AMONG THREE DIFFERENT
NUMBERS ENTERED BY THE USER.
49
CHECK WHETHER YEAR IS LEAP OR NOT
50
PSEUDO CODE
51
Example: Write a pseudo code to perform the basic arithmetic
operations.
Read n1 , n2
Sum = n1 + n2
Dif f = n1 – n2
Mult = n1 * n2
Div = n1/n2
Print Sum, Dif f, Mult, Div
End.
52
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”
PSEUDOCODE & ALGORITHM
Detailed Algorithm
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
EXAMPLE
START
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
Input
M1,M2,M3,M4
Step 3: if (GRADE <50) then
Print “FAIL”
else
GRADE(M1+M2+M3+M4)/4 Print “PASS”
endif
N IS Y
GRADE<50
PRINT PRINT
“PASS” “FAIL”
STOP
EXAMPLE 2
Algorithm Flowchart
Step 1: Input Lft
START
Step 2: Lcm Lft x 30
Step 3: Print Lcm Input
Lft
Lcm Lft x 30
Input
Lcm
STOP
EXAMPLE 3
Algorithm
START
Step 1: Input W,L
Step 2: AL x W Input
Step 3: Print A W, L
ALxW
Print A
STOP
EXAMPLE 4
61