0% found this document useful (0 votes)
12 views21 pages

1 Introduction - 16894420 - 2024 - 04 - 03 - 08 - 52

Uploaded by

yogeshc28282
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views21 pages

1 Introduction - 16894420 - 2024 - 04 - 03 - 08 - 52

Uploaded by

yogeshc28282
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

C-Programming Introduction

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.

Von Neumann model


It is made of a monitor (standard display unit), CPU (Central processing unit) and keyboard
(standard input device).
A Computer can be visualized as a system with input/output (I/O) devices, Central processing
Unit (CPU) and the memory interconnected.

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

Basics of Positional Number System


The number system is used for representing the information. The number system has different
bases and the most common of them are the decimal, binary, octal, and hexadecimal. The base or
radix of the number system is the total number of the digit used in the number system.
Types of Number Systems

1. Decimal Number Systems


The number system is having digit 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; this number system is known as a
decimal number system because total ten digits are involved. The base of the decimal number system
is 10.

2. Binary Number System (Base 2. Digits used: 0, 1)


• Computer can understand only binary language.
• Binary number system can have only two representations namely ‘0’ or ‘1’.
• All numbers and characters are to be represented using ‘0’ or ‘1’ only.
• The conversion from binary to decimal and decimal to binary can be done as shown below with
examples.

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:

1. Divide the decimal number by 8. Treat the division as an integer division.


2. Write down the remainder (in octal form).
3. Divide the result again by 8. Treat the division as an integer division.
4. Repeat step 2 and 3 until result is 0.
5. The octal value is the digit sequence of the remainders from the last to first

4. Hexa-Decimal Number System (Base 16. Digits used: 0 to 9, Letters used: A- F)


These numbers are used extensively in microprocessor work. The hexadecimal number system
has a base of 16, and hence it consists of the following sixteen number of digits.
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
Converting Decimal to Hexadecimal
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.

2. Write down the remainder (in hexadecimal).

6.
3. Divide the result again by 16. Treat the division as an integer division.

4. Repeat step 2 and 3 until result is 0.

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.

A (INPUT) B(INPUT) Y(OUTPUT)

0 0 0

0 1 0

1 0 0

1 1 1

2. OR

The output is 1 if and only if A or B (A&B being the inputs) is 1.

A (INPUT) B(INPUT) Y(OUTPUT)

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

The output is NOT A (A being the input) i.e. complement of A

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.

A (INPUT) B(INPUT) Y(OUTPUT)

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.

d) Speed: It should produce the result as fast as possible or efficiently.

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

Algorithm 1: Write an algorithm to accept a number and display its square.


• Algorithm
Step I : Start

Step II : Input a number

Step III : Calculate the square of the user entered number.

Step IV : Display the calculated result.

Step V : Stop
• Pseudo code
Step I : START

Step II : PRINT “Enter a number”

Step III : Input n

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

Step II : Input two numbers.

Step III : Calculate product of two number.

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 IV : Display product.

Step V : Stop
• Pseudo code
Step I : START

Step II : PRINT “Enter two numbers”

Step III : Input a, b

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

Algorithm 3: Write an algorithm to display first ten natural numbers.


• Algorithm
Step I : Start

Step II : Initialize the counter with value as 1

Step III : Check if counter is greater than 10, if yes then goto step VII.

Step IV : Display the value of counter variable.

Step V : Increment the counter variable.

Step VI : Goto step III

Step VII : Stop


• Pseudo code

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

Algorithm 4: Write an algorithm find the factorial of a number.


• Algorithm
Step I : Start

Step II : Input a number

Step III : Initialize the counter with value as 1.

Step IV : Check if counter is greater than user entered number, if yes then goto

step VIII.

Step V : Multiply the fact variable with counter value.

Step VI : Increment the counter variable.

Step VII : Goto step IV

Step VIII : Display the value of factorial.

Step IX : Stop
• Pseudo code
Step I : START

Step II : PRINT “Enter a number”

Step III : Input n.

Step IV : i = 1, fact = 1;

Step V : IF i > n THEN GOTO step IX.

Step VI : fact = fact * i

Step VII : i = i + 1;

Step VIII : GOTO step V;

Step IX : PRINT fact;

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.

Following are symbols used for making flowchart.


SYMBOL NAME DESCRIPTION
Terminal Defines the start and end of a
flowchart.

Input / Output Used to perform input and Output


operations.

Process, Instruction Manipulation of data (Assignment


and mathematical computations)

Flow Lines Define logical sequence or


direction of flowchart.

Decision Process, Decision or branch


conditions using relational
operators.(e.g., If / ELSE etc.)
Connector Connector (connect one part of the
flowchart to another)

Advantages & Disadvantages of Using Flowcharts


Advantages
• Communication: Flowcharts are better way of communicating the logic of a system to all
concerned.
• Effective analysis: With the help of flowchart, problem can be analysed in more effective way.
• Proper documentation: Program flowcharts serve as a good program documentation, which is
needed for various purposes.
• Efficient Coding: The flowcharts act as a guide or blue print during the systems analysis and
program development phase.
• Proper Debugging: The flowchart helps in debugging process.

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.

Limitations of Using Flowcharts:

• 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

Accept num1, num2

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

Draw a flowchart to find the square of a numbers.


Solution:

START

Accept a num

Mult = num × 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

Write an algorithm and draw flowchart to accept marks of 4


subjects and display grade.
Solution:
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”

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

Write an algorithm and draw a flowchart to find the larger of two


numbers A and B.
Solution:
Step 1: Input A, B
Step 2: IF (A > B) THEN
MAX=A
ELSE
MAX=B
END IF
Step 3: PRINT “The largest value is”, MAX

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

Write an algorithm and draw a flowchart to find the number is


even or odd.
Solution:
Step 1: Input num
Step 2: IF (num%2==0) THEN
PRINT “the number is even”;
ELSE
PRINT “the number is odd”;
END IF
Step 3: STOP

Start

Read num

IS
num%2=0

Print “The number is even” Print “The number is odd”

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

Draw a flowchart to find the sum of first 50 natural numbers.

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

Write an algorithm and draw a flowchart that will calculate the


roots of a quadratic equation 𝐚𝐱 𝟐 + 𝐛𝐱 + 𝐜 = 𝟎. (May-13) 6M
Hint: d = sqrt (𝐛𝟐 − 𝟒𝐚𝐜), and the roots are:
x1 = (–b + d)/2a and x2 = (–b – d)/2a
Solution:
Step 1 : Start
Step 2 : Input a, b, c
Step 2 : d sqrt (b2 − 4 × a × c)
−b+d
Step 3 : x1 ( )
2×a
−b−d
Step 4 : x2 ( )
2×a

Step 5 : PRINT X1, X2


Step 6 : Stop

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

Loops Using Flowchart


Do While For loop

true true

False False

If else While loop

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

You might also like