Professional Documents
Culture Documents
BITS Pilani: Computer Programming
BITS Pilani: Computer Programming
Computer Programming
BITS Pilani SEMTZC163
Dr. Kiran D C
Pilani Campus
COMPUTER PROGRAMMING
SEMTZC163
3
3/2/2022
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
COURSE OBJECTIVES
COMPUTER PROGRAMMING
3/2/2022 4
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Text & Reference Books
• Text Book
T1. J.R. Hanly and E.B. Koffman, Problem Solving and Program Design in
C., 5th Edition. Pearson Education 2007.
• Reference Books
R1 J. B. Dixit, Programming in C, Third Edition, Firewall Media © 2010
[Available on 24x7 Ebooks]
R2 Brian W. Kernighan, Dennis Ritchie, The C Programming Language. Prentice Hall. 2nd
Edition.
R3 E Balaguruswamy, Programming in ANSI C, 4th Edition, Tata McGraw-Hill Education,
2008.
R4 Byron, S. Gottfreid, "Programming with C", Tata McGraw Hill, 2nd edition 1998.
COMPUTER PROGRAMMING
3/2/2022 6
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Modular Structure
COMPUTER PROGRAMMING
3/2/2022 9
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Why Number System ?
COMPUTER PROGRAMMING
3/2/2022 10
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Introduction to Computer Programming
COMPUTER PROGRAMMING
3/2/2022 11
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
A Computer System have?
• Memory Unit
– To store information
• Processing Unit
– To process the information
• Control Unit
– To control the overall operation!!!
• Anything Else???
• Let’s connect all of them. How???
COMPUTER PROGRAMMING
3/2/2022 12
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Block Diagram of a Computer
Memory
Input Output
Processor
Data Flow
Control
Control Unit
Flow
COMPUTER PROGRAMMING
3/2/2022 13
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Computer Programming
• Objective of Programming
– Solving Problems
• Program ??
- Set of instructions that tell a computer , how a
problem is to be solved
COMPUTER PROGRAMMING
3/2/2022 14
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Towards Problem Solving
What is Program? Solution to the Problem
COMPUTER PROGRAMMING
3/2/2022 16
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
What are Programs?
• Compare: Plans, Recipes, and Programs.
COMPUTER PROGRAMMING
3/2/2022 17
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
What are Programs?
COMPUTER PROGRAMMING
3/2/2022 18
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
What are Programs?
COMPUTER PROGRAMMING
3/2/2022 19
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
What are Programs?
COMPUTER PROGRAMMING
3/2/2022 20
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
What are Programs?
A Program is a
Formal
Unambiguous
Executable Specification
of a solution to a problem
COMPUTER PROGRAMMING
3/2/2022 21
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Programming
• .
• .
• .
• .
• .
• .
COMPUTER PROGRAMMING
3/2/2022 22
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Programming
Programming a computer is
Instructing a computer to execute a sequence of steps (each
of which is an instruction that is built in)
Different computers have different instruction sets
Different computers may have different levels of
instruction sets
Some simple – e.g. ADD instruction in most computers
adds 2 integers (Eg: 2+3)
Some more complex e.g. ADDV instruction in Cray-1
(one of the most popular super-computers late 70s to
early 80s) adds two 64 element vectors
COMPUTER PROGRAMMING
3/2/2022 23
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Programming Languages
• A programming language can be thought of as “abstract” computer
– i.e. it is an instruction set where
➢ Each instruction maps to a sequence of one or more instructions
on the (physical) computer
• The most “direct” programming language is a machine language
- each instruction in a machine language (.e.g. Pentium machine
language) maps directly to an instruction executable in a
computer (i.e. Pentium processor in the example.)
- But programming using such a language is tedious for human
users / programmers.
COMPUTER PROGRAMMING
3/2/2022 24
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Programming Languages
COMPUTER PROGRAMMING
3/2/2022 25
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Programming Languages
• At a more abstract level is a High Level
Language (e.g. C)
– An instruction in a HLL (often referred to as
an operation, an expression, or a statement)
Is fairly more complex and
Usually translates to a sequence of
instructions or a template (of such a
sequence) in a machine language
Note: We will revisit these later.
But observe the layering of abstractions.
COMPUTER PROGRAMMING
3/2/2022 26
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Programming Options
1000 0001 1000 0001 .ORIG X3000 num1=5;
0001 1000 1010 0110
LD R0,NUM1 num2=7;
1000 1100 0101 1000
0101 0000 0100 1001
LD R1,NUM2 num2=num1+num2;
1010 1001 1100 0001 ADD R1,R1,R0
0101 1110 1011 1100
0011 0111 0110 1110
NUM1 .FILL #5
0101 0000 0100 1001
1010 1001 1100 0001
NUM2 .FILL #7
TRAP X25 ;
.END
Note : This is just
a random sequence
of 1’s and 0’s
COMPUTER PROGRAMMING
3/2/2022 27
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
High Level Programming
• Advantage
– More Programmer Friendly
• The high level language that will be
discussed in our course - C
COMPUTER PROGRAMMING
3/2/2022 28
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Approach to Programming
• Solutions to complex problems are not straightforward
• Algorithm is a step by step procedure to solve a problem
• Flowchart is a pictorial representation of an algorithm
• Approach :
– Analyse Problem Statement
– Write Algorithm
– Draw the corresponding flowchart
– Convert the Algorithm into a Program - Coding
COMPUTER PROGRAMMING
3/2/2022 29
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Data representation………………
COMPUTER PROGRAMMING
3/2/2022 30
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
31
3/2/2022
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Why Number System ?
COMPUTER PROGRAMMING
3/2/2022 32
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
What kinds of data do we need to represent?
Numbers – signed, unsigned, integers, floating
point, complex, rational, irrational, …
Text – characters, strings, …
Images – pixels, colors, shapes, …
Sound
Logical – true, false
Instructions
…
34
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
What is Base?
COMPUTER PROGRAMMING
3/2/2022 36
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
3-Bit BinaryRepresentation
22 21 20
0 0 0 0
0 0 1 1
0 1 0 2
0 1 1 3
1 0 0 4
1 0 1 5
1 1 0 6
1 1 1 7
= 26+25+23 = 64+32+8
X = 104ten
39
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
More Examples
n 2n
X = 00100111two 0 1
= 25+22+21+20 = 32+4+2+1 1 2
2 4
X = 39ten 3 8
4 16
5 32
6 64
7 128
8 256
9 512
10 1024
40
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Converting Decimal to Binary
41
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example:
X= 104ten
104/2 = 52 r 0 bit 0
52/2 = 26 r 0 bit 1
26/2 = 13 r 0 bit 2
13/2 = 06 r 1 bit 3
6/2 = 03 r 0 bit 4
3/2 = 01 r 1 bit 5
1/2 = 0 r 1 bit 6
X =01101000two
42
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Decimal to Octal
Octal to Decimal
Sum the multiplication of each digit with its position value
Example: Convert 6248 to its decimal equivalent.
6 x 82 + 2 x 81 + 4 x 80 = 40410
43
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Octal to Binary
First Method:
First convert Octal number into its Decimal Equivalent and then From
Decimal to Binary.
Second Method:
Write 3-bit binary equivalent for each octal digit in the given number.
Resultant number will be binary equivalent.
Example: Binary equivalent of 6248 6 2 4
110010100
Question: How to convert a Binary number to its Octal equivalent?
44
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Hexadecimal Notation
It is often convenient to write binary (base-2) numbers as hexadecimal (base-
16) numbers instead.
Why?
– Fewer digits -- four bits per hex digit
– Less error prone -- easy to corrupt long string of 1’s and 0’s
3 A 8 F 4 D 7
46
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise
47
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
How do we represent data in a computer?
48
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Computer is a binary digital system Cont…
Basic unit of information is the binary digit, or bit.
Values with more than two states require multiple bits.
Inference:
– A sequence of n bits has 2n possible states.
49
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Unsigned Integers (cont.)
50
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Unsigned Binary Arithmetic
Base-2 addition – just like base-10!
– Add from right to left, propagating carry
carry
10111
+ 111
11110
51
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Signed Integers Representation
• Signed Magnitude
• 1’s Complement
• 2’s Complement
COMPUTER PROGRAMMING
3/2/2022 52
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Question
Given n bit string, What is the Maximum number we can represent in
Signed Magnitude form?
1 1 1 . . . . . . 1
2n-1 2n-2 . . . . . 22 21 20
53
Representation of Signed Integers Cont…
Ex: n = 3
Signed
magnitude
000 +0
001 +1
010 +2
011 +3
100 -0
101 -1
110 -2
111 -3
54
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
1’s Complement Representation
•Positive numbers representation:
– same as signed integers.
•Negative numbers representation:
– by flipping all the bits of corresponding
positive numbers
For Example:
+5 is represented as 00101
-5 is represented by 11010
55
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example: 1’s Complement
How we represent -12 in 1’s complement form
58
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Two’s Complement Representation
If number is negative
– Start with positive number
– Flip every bit (i.e., take the one’s complement)
– Then add one
Example:
00101 (5) 01001 (9)
11010 (1’s comp) 10110 (1’s comp)
+ 1 + 1
11011 (-5) 10111 (-9)
59
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example: 2’s complement
2+(-3) =??
Express -3 in 2’s complement form:
+3 → 00011
1’s Complement of 3 →11100
2’s Complement of 3 →11101 i.e. -3
2 →00010
+ +
-3 →11101
11111
Look at MSB ➔ 1 So take 2’s complement again
00001
60
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Two’s Complement Signed Integers
MS bit is sign bit:– it has weight –2n-1.
Range of an n-bit number: -2n-1 through 2n-1 – 1.
– The most negative number (-2n-1) has no positive counterpart.
23 22 21 20 23 22 21 20
0 0 0 0 0 1 0 0 0 -8
0 0 0 1 1 1 0 0 1 -7
0 0 1 0 2 1 0 1 0 -6
0 0 1 1 3 1 0 1 1 -5
0 1 0 0 4 1 1 0 0 -4
0 1 0 1 5 1 1 0 1 -3
0 1 1 0 6 1 1 1 0 -2
0 1 1 1 7 1 1 1 1 -1
61
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Converting Binary (2’s C) to Decimal
5. What is the maximum and minimum number we can represent in 2’s complement form
using 16 bits
63
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
COMPUTER PROGRAMMING
3/2/2022 64
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956