1 Introduction - 16894420 - 2024 - 04 - 03 - 08 - 52
1 Introduction - 16894420 - 2024 - 04 - 03 - 08 - 52
Chapter-1. Introduction
Turning Machine
A Turing machine is an abstract machine that manipulates symbols on strip of tape according to
table of rules. The more practical form of the same is called as a mathematical model.
The Turing model forms the basis of software development. it shows the behaviour expected
from the system to be designed. Using a Turing model, one can design and develop the step expected
in the procedural oriented programming.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 1
C-Programming Introduction
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 2
C-Programming Introduction
Decimal to binary
e.g. (𝟓)𝟏𝟎 = ( ? )𝟐
Quotient Remainde
r
2 5
2 2 1
2 1 0
0 1
(𝟓)𝟏𝟎 = (𝟏 𝟎 𝟏)𝟐
Decimal to binary
Converting a binary number to a decimal number is:
Multiply the binary digits (bits) with powers of 2 according to their positional weight.
E.g. (𝟏𝟏𝟏𝟎𝟎)𝟐 = ( ? )𝟏𝟎
= 1 1 1 0 0
= 1* (2)4 + 1* (3)3 + 1* (2)2 + 0* (2)1 + 0* (2)0
= 16 + 8 + 4 + 0 + 0
= (28)10
3. Octal Number System (Base 8. Digits used: 0 to 7)
The octal system has the base of eight as it uses eight digits 0, 1, 2, 3, 4, 5, 6, 7.
Converting Decimal to Octal
Steps:
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 3
C-Programming Introduction
1. Divide the decimal number by 16. Treat the division as an integer division.
6.
3. Divide the result again by 16. Treat the division as an integer division.
5.The hex value is the digit sequence of the remainders from the last to first
(2861)10 = (B2D)16
There are various operations that are performed on binary data like AND, OR, EXOR and NOT.
1. AND
The output is 1 if and only if A AND B (A&B being the inputs) are 1.
0 0 0
0 1 0
1 0 0
1 1 1
2. OR
0 0 0
0 1 1
1 0 1
1 1 1
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 4
C-Programming Introduction
3. NOT
A(INPUT) Y(OUTPUT)
0 1
1 0
4. EXOR
The output is 1 if and only if EXCLUSIVELY A OR B (A&B being the inputs) are 1.
0 0 0
0 1 1
1 0 1
1 1 0
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 5
C-Programming Introduction
Algorithm
A sequential solution of any problem written in English language is called as algorithm.
Algorithm is finite Set of instruction that specifies a sequence of operation to be carried out in order to
solve a specific problem.
The process of the programming is as shown in the Fig.
Defining a Problem
Algorithm development
Program implementation
An algorithm can also be defined in simple terms as description of the steps necessary to solve a
problem. The algorithm should define the procedure to perform the given task.
Graphical representation of an algorithm can be done using flowchart or pseudo code algorithm.
Properties of algorithm
The algorithm should have following properties associated with it
a) Non-ambiguity: Each of the statement in the algorithm must be clear and precise. There should not
be ambiguity in any of the statement.
b) Range of Input: The range of input for which algorithm work and there should be clear indication
for the range of inputs for which the algorithm may fail.
c) Multiplicity: Algorithm can be written in English language or by the graphical representation called
as flowchart or the pseudo code.
e) Finiteness: Algorithm should be finite i.e there should not be infinite condition leading to a never-
ending procedure and hence never completing the task.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 6
C-Programming Introduction
Developing an algorithm
Rules for writing an algorithm in Pseudo code form.
1. The algorithms should always begin with “START” and end with “STOP”.
2. To accept input from the user, we will use “INPUT” or the “READ”.
3. To display output on the monitor, we will use “PRINT” statement. The words to be displayed as it is
will be written in double quotes while the variables whose values are to be displayed will not be in
double quotes.
4. We will use the basic arithmetic operators to indicate the operations.
5. We will use “AND”, “OR” and “NOT” to indicate conjunction, disjunction and negation
respectively.
6. To check conditions, we can use the “IF” “THEN” and “ELSE” CONSTRUCTS.
7. To Jump from one step to another the construct used is “GOTO”, statement is always associated with
a step number
Step V : Stop
• Pseudo code
Step I : START
Step IV : a=n*n
Step V : PRINT a
Step VI : STOP
Algorithm 2: Write an algorithm to accept two numbers and display its product.
• Algorithm
Step I : Start
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 7
C-Programming Introduction
Step V : Stop
• Pseudo code
Step I : START
Step IV :x=a*b
Step V : PRINT x
Step VI : STOP
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 8
C-Programming Introduction
Step III : Check if counter is greater than 10, if yes then goto step VII.
Step I : START
Step II :i=1
Step III : IF i > 10 THEN GOTO Step VII.
Step IV : PRINT i
Step V :i=i+1
Step VI : GOTO step III
Step VII : STOP.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 9
C-Programming Introduction
Step IV : Check if counter is greater than user entered number, if yes then goto
step VIII.
Step IX : Stop
• Pseudo code
Step I : START
Step IV : i = 1, fact = 1;
Step VII : i = i + 1;
Step X : STOP.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 10
C-Programming Introduction
Example:
Write an algorithm to sort set of numbers in ascending order. For the
above problem, in which case the time complexity is calculated.
Step I : START
Step II : Input N as size of the list
Step III : PRINT ‘Enter list of elements’
Step IV : For I ←0 to N – 1 DO Repeat step 5
Step V : INPUT A[I]
Step VI : FOR I←N – 1 DOWNTO 1 DO Repeat step 7
Step VII : FOR j←0 TO I – 1 DO Repeat step 8
Step VIII : IF A[j] > A[j+1] THEN
Begin
TEMP ← A[j]
A[j] ← A[j+1]
A[j+1] ←TEMP
End
Step IX : FOR I ←0 TO N – 1 Repeat step 10
Step X : PRINT A[I]
Step XI : STOP
Time complexity for the above algorithm must be measured for the average
case as well as worst case (array is in descending order). When we have N elements
than the number of comparisons required for sorting the complete list will be given
as follows.
Number of comparisons = (N – 1) + (N – 2) +………. + 2 + 1
= (N (N − 1)) / 2
= (N2 −N) / 2
Time complexity of this algorithm will be O (N2 ) in average case as well as
worst case.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 11
C-Programming Introduction
Flowchart
A Flowchart is a graphical representation of an algorithm to show sequence of steps.
Flowchart gives flow of algorithm in sequence.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 12
C-Programming Introduction
Disadvantages
• Difficulty in presenting complex programs and tasks: Complex tasks cannot be presented through
symbols so easily. Sometimes it becomes difficult for even for the expert to present the program
having complicated steps. Various logics of the program are difficult to draw in a set or already
defined shapes in a flowchart.
• No scope for alteration or modification: If there is any error found in the process or logic of the
flowchart, it is difficult to alter or modify the same. This is because either we have to erase the
beginning or end of the program and the whole flowchart is affected.
• Reproduction becomes a problem: The flowchart symbols cannot be drawn. We have to use
different applications like Word or Excel to draw shapes and type words inside those symbols. This
recreation becomes difficult as they require shapes to present the whole process.
• It’s a time-consuming process: as we have already seen, reproduction of flowcharts is a problem
due to the complexity of shapes and the use of no specific application to give already set up symbols
to write the logic of the process or task. Thus it becomes a time-absorbing task.
• Difficult to understand for people who don’t know flowchart symbols: Since not all are experts
in understanding the purpose and motive behind using specific symbols in a flowchart it is not
understandable by the common people instantly. This proper knowledge and expertise are important
to understand the flowchart. Thus, communication will become more effective.
• No man to computer communication: A flowchart is not meant for man to computer
communication. Only man can interpret the result of the flowchart.
• Complex logic: Sometimes, the program logic is quite complicated. In that case, flowchart becomes
complex and clumsy.
• Alterations and Modifications: If alterations are required the flowchart may require re-drawing
completely.
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 13
C-Programming Introduction
Flowchart
Draw a flowchart to find the product of two numbers.
Solution:
START
Product=num1 × num2
Display product
STOP
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 14
C-Programming Introduction
Flowchart
START
Accept a num
Display Mult
STOP
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 15
C-Programming Introduction
Flowchart
Print “FAIL”
ELSE
Print “PASS”
END IF
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 16
C-Programming Introduction
Flowchart
Start
Read A, B
IS
A>B
Max = A Max = B
Display
Max
End
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 17
C-Programming Introduction
Flowchart
Start
Read num
IS
num%2=0
End
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 18
C-Programming Introduction
Flowchart
Solution:
Step 1: Start.
Step 2: Initialise SUM = 0 and N = 0
Step 3: N = N + 1
Step 4: SUM=SUM+N
Step 5: IF N = 50? THEN go to step 6.
ELSE goto step 3 Start
Step 6: Display SUM.
Step 7: End. SUM = 0
N=0
N=N+1
SUM = SUM + N
IS
N = 50?
Print SUM
END
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 19
C-Programming Introduction
Flowchart
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 20
C-Programming Introduction
true true
False False
true False
true
False
This document is property of RKDEMY and cannot be used, disclosed or duplicated without the prior written consent of RKDEMY pg.1- 21