Download as pdf or txt
Download as pdf or txt
You are on page 1of 64

BITS Pilani

Computer Programming
BITS Pilani SEMTZC163
Dr. Kiran D C
Pilani Campus
COMPUTER PROGRAMMING
SEMTZC163

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


What are we going to do?

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.

R5 Behrouz A. Forouzan, Richard F. Gilberg: Computer Science - A Structured Approach


Using C, 3 rd Edition, Cengage Learning, 2007.
COMPUTER PROGRAMMING
3/2/2022 5
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Evaluation Scheme

COMPUTER PROGRAMMING
3/2/2022 6
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Modular Structure

No Title of the Module


M1 Introduction: Computers and Programming
M2 Elements of C Language
M3 Top Down Design using Functions
M4 Conditional Constructs
M5 Iterative Constructs
M6 Random Access Lists: Arrays
M7 Abstract Data Types: Structure & Union
M8 Pointers and Linked Lists
M9 File Handling

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Learning Outcomes:
No. Learning Outcomes
LO1 Students will be able to describe the number systems.

LO2 Students will be able to write, compile and debug programs


with different data types in C language.
LO3 Students will be able to design simple programs involving
decision structures, loops and functions.
LO4 Students will be able to apply the dynamic memory
allocation using pointers.
L05 Students will be use different operations on files to store
8
3/2/2022
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Today’s Topics
• Introduction to Computer Programming
• Representation of Numbers
• Base Conversion
• Binary to Decimal and Decimal to Binary
• Decimal to Octal and Octal to Decimal
• Octal to Binary and Binary to Octal
• Binary to Hexadecimal and Hexadecimal to Binary
• Binary Numbers
– Unsigned Integers
» Non Positional
» Positional
• Signed Integers
» Signed Magnitude
» 1’s Complement
» 2’s complement
• Arithmetic Operation

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

What is Programming ? Solving Problem in Engineering approach /


Systematic Procedure.

What is Programming Language ? Tool to solve problem

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 16
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
What are Programs?
• Compare: Plans, Recipes, and Programs.

• Recipe: (e.g. Avial) [courtesy vahrevah.com]


Cut all vegetables in long pieces and cook it with a little bit of water
and a tsp of turmeric powder.
Grind a cup of grated coconut with jeera & green chillies
Mix the cooked vegetables and the ground ones in a vessel and
boil for a few minutes;
Add a few curry leaves and salt as per taste and boil for 3 or 5 min.
Stir in between, not too often.
After some time add the curds and mix it together
Add a tbsp of coconut oil and mix well.

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

• At a slightly “higher level” compared to a


machine language is an assembly language
(e.g. Intel x86 assembly language)

– An instruction in an assembly language is usually a


“human readable” version of an instruction in a
machine language

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

Machine Language Assembly Program High Level Program

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………………

• How to represent the Data???


– Number Systems
– Representation of numbers

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?

•Decimal Number System


– Base is 10
– All numbers are represented by 0 to 9
•Binary Number System
– Base is 2
– All numbers are represented by 0 and 1
•Octal Number System
– Base is 8
– All numbers are represented by 0 to 7
•Inference
– Maximum digit value in any number system is Base -1
35
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Topics
• Representation of Numbers
• Base Conversion
• Binary to Decimal and Decimal to Binary
• Decimal to Octal and Octal to Decimal
• Octal to Binary and Binary to Octal
• Binary to Hexadecimal and Hexadecimal to Binary
• Binary Numbers
– Unsigned Integers
» Non Positional
» Positional
• Signed Integers
» Signed Magnitude
» 1’s Complement
» 2’s complement
• Arithmetic Operation

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

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


4-Bit Binary Representation

Binary Decimal Binary Decimal


0000 0 1000 8
0001 1 1001 9
0010 2 1010 10
0011 3 1011 11
0100 4 1100 12
0101 5 1101 13
0110 6 1110 14
0111 7 1111 15

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Example: Binary to Decimal Conversion
X = 01101000two
X = 01101000
Add powers of 2 that have “1” in the corresponding bit positions.

= 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

1. Find magnitude of decimal number. (Always +ive)


2. Divide by two – remainder is least significant bit.
3. Keep dividing by two until answer is zero, writing
remainders from right to left.
4. Append a zero as the MS bit.

If original number was negative, take two’s


complement.

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

1. Find magnitude of decimal number. (Always +ive)


2. Divide by Eight – remainder is least significant bit.
3. Keep dividing by Eight until answer is zero, writing
remainders from right to left.
Example: Convert 40410 to its octal equivalent.

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

Binary Hex Decimal Binary Hex Decimal


0000 0 0 1000 8 8
0001 1 1 1001 9 9
0010 2 2 1010 A 10
0011 3 3 1011 B 11
0100 4 4 1100 C 12
0101 5 5 1101 D 13
0110 6 6 1110 E 14
0111 7 7 1111 F 15
45
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Converting from Binary to Hexadecimal

Every four bits is a hex digit.


– Start grouping from right-hand side
Example:
011101010001111010011010111

3 A 8 F 4 D 7

This is not a new machine representation,


just a convenient way to write the number.

46
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise

Convert a Hexadecimal Number into Binary


Number?
1. ADF6
2. DB69
3. CA36
4. 5678
5. DABC

47
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
How do we represent data in a computer?

At the lowest level, a computer is an electronic


machine.
– Works by controlling the flow of electrons
Easy to recognize two conditions:
1. Presence of a voltage – we’ll call this state “1”
2. Absence of a voltage – we’ll call this state “0”

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.

– A sequence of two bits has four possible states:


00, 01, 10, 11

– A sequence of three bits has eight possible states:


000, 001, 010, 011, 100, 101, 110, 111

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.)

An n-bit unsigned integer represents 2n


values:
22 21 20
from 0 to 2n-1. 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

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

10010 10010 1111


+ 1001 + 1011 + 1
11011 11101 10000

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

The Maximum +ive value is : 2n-1 -1


The Maximum -ive value is : 2n-1 -1

Ex: Given 5 bits,


Max. number in signed magnitude form is: 24 -1 =15
Min. number in signed magnitude form is: -24 -1= -15

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

Step1: Take +12 in binary representation


➔ 01100
Step2: Flip all the bits of the above
➔ 10011
Inference
-12 representation in 1’s complement form: 10011
+12 representation in 1’s complement form: 01100
56
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Representation of Signed Integers
Ex: n = 3
Signed 1’s Complement
magnitude
000 +0 +0
001 +1 +1
010 +2 +2
011 +3 +3
100 -0 -3
101 -1 -2
110 -2 -1
111 -3 -0
57
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Limitations!
Problems with sign-magnitude and 1’s
complement!
– Two representations of zero (+0 and –0)
– Arithmetic circuits are complex to implement
above representation
Because:
• How to add two sign-magnitude numbers?
– e.g., try 2 + (-3)
00010
10011
10101 => -5 ??

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

1. If leading bit is one, take two’s


complement to get a positive n 2n
0 1
number. 1 2
2 4
3 8
2. Add powers of 2 that have “1” in 4 16
the corresponding bit positions. 5 32

Assuming 8-bit 2’s complement numbers. 6 64


7 128
8 256
3. If original number was negative,
9 512
add a minus sign. 10 1024
62
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
EXERCISE 1
1. Represent (-26)10 in its signed magnitude form using 8-bits:
00011010
2. Represent (-10)10 in its 1’s complement form using 8 bits
00001010-> 11110101
3. Represent (-12)10 in its 2’s complement form using 8-bits
00001100->11110011->11110100
4. How many bits required to represent (-128)10 in 2’s complement and 1’s complement
representation
Range of an n-bit number: -2n-1 through 2n-1 – 1 -→ 2’s complement
1’s complement:
The Maximum +ive value is : 2n-1 -1
The Maximum -ive value is : 2n-1 -1

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

You might also like