0% found this document useful (0 votes)
3 views8 pages

C Programming Week1

The document outlines the course CS1100: Introduction to Programming at IIT Madras, covering topics such as computing fundamentals, programming in C, and numerical methods. It details evaluation methods, class schedules, policies regarding cell phone usage, and the structure of computers. Additionally, it discusses programming paradigms, limitations of computers, and provides resources for further reading.

Uploaded by

ABHISHEK GOUTAM
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)
3 views8 pages

C Programming Week1

The document outlines the course CS1100: Introduction to Programming at IIT Madras, covering topics such as computing fundamentals, programming in C, and numerical methods. It details evaluation methods, class schedules, policies regarding cell phone usage, and the structure of computers. Additionally, it discusses programming paradigms, limitations of computers, and provides resources for further reading.

Uploaded by

ABHISHEK GOUTAM
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/ 8

09/08/17

Course Outline
• Introduction to Computing

CS1100 • Programming (in C)


Introduction to Programming
• Exercises and examples from the mathematical
Introduction to Computing area of Numerical Methods
Madhu Mutyam
Department of Computer Science and Engineering • Problem solving using computers
Indian Institute of Technology Madras

Course Material – SD, SB, PSK, NSN, DK, TAG – CS&E, IIT M 1 2
SD, PSK, NSN, DK, TAG – CS&E, IIT M

Evaluation Class Hours


• Two Quizzes – 30 • Theory class meets 3 times a week (F1 slot)
– Wednesday 11:00 – 11:50 AM
– Thursday 9:00 – 9:50 AM
• Programming Assignments – 25
– Friday 8:00 – 8.50 AM
• Venue: CS36
• End of Semester Exam – 45
• Lab class meets once a week
– B.Tech: Friday 2:00 4:50 PM (T1 slot)
• Attendance – taken in the lab and in lectures – DD: Wednesday 2:00 4:50 PM (R1 slot)
• Attendance sheet will be circulated for signature

SD, PSK, NSN, DK, TAG – CS&E, IIT M 3 SD, PSK, NSN, DK, TAG – CS&E, IIT M 4

Policies What is CS1101 About?


• Strictly no cell phone usage in class • Computer and its components
– Keep your cell phone turned off
• Not even in silent mode
• Computing
– No SMS, chat etc.
• Cell phones will be confiscated if there are
violations • Programming Languages
– Returned only after 2 weeks
• Repeat violations • Problem Solving and Limitations of a Computer
– Students will be sent to the Dean (Acad)

SD, PSK, NSN, DK, TAG – CS&E, IIT M 5 SD, PSK, NSN, DK, TAG – CS&E, IIT M 6

1
09/08/17

Common Uses of a Computer What is a Computer?


• A computer is a programmable machine
• Its behavior is controlled by a program
• Programs reside in the memory of the machine
– “The stored program concept”
All figures are taken from Google

Von Neumann Alan Turing


Charles Babbage
SD, PSK, NSN, DK, TAG – CS&E, IIT M 7 SD, PSK, NSN, DK, TAG – CS&E, IIT M 8

Early Computing Hardware The Difference Engine: ~1850’s

Part of Babbage's difference


engine, assembled after his
death by Babbage's son, using
The Slide rule parts found in his laboratory.
http://sliderulemuseum.com/ The Chinese Abacus

The London Science Museum's


replica Difference Engine, built
from Babbage's design.

Odhner’s Arithmometer An automatic, mechanical


Pascal’s Calculator calculator designed to tabulate
Dalton’s Adding Machine polynomial functions
SD, PSK, NSN, DK, TAG – CS&E, IIT M 9 SD, PSK, NSN, DK, TAG – CS&E, IIT M 10
“Mechanical calculator” from Wikipedia

The First Programmer ENIAC The First Electronic Computer


Augusta Ada King, Countess of Lovelace (December Physically, ENIAC was massive
10, 1815 – November 27, 1852) is mainly known for compared to modern PC standards. It
having written a description of Charles Babbage's early
mechanical general-purpose computer, the analytical contained 17,468 vacuum tubes, 7,200
engine. crystal diodes, 1,500 relays, 70,000
The programming language ADA is named after her.
resistors, 10,000 capacitors and around
5 million hand-soldered joints. It
weighed 27 tons, was roughly 2.4 m by
0.9 m by 30 m, took up 167 m², and
consumed 150 kW of power.
Electronic Numerical Integrator And Computer,
1946-55 (Univ. Penn.)

SD, PSK, NSN, DK, TAG – CS&E, IIT M 11 SD, PSK, NSN, DK, TAG – CS&E, IIT M 12

2
09/08/17

Core i7 Processor The Computing Machine

PROCESSOR

MEMORY

01234……. (say) 256 MEGABYTES

The computer is made up of a processor and a


2008: Intel Core i7 Processor
Clock speed: >2.5 GHz memory. The memory can be thought of as a series
No. of Transistors: 731 million of locations to store information.
Technology: 45nm CMOS
13 14
SD, PSK, NSN, DK, TAG – CS&E, IIT M
Area: 263mm2 SD, PSK, NSN, DK, TAG – CS&E, IIT M

The Computing Machine Variables


PROCESSOR • Data is represented as binary strings
– It is a sequence of 0’s and 1’s (bits), of a
MEMORY
predetermined size – “word”. A byte is made of 8 bits.
01234……. 256 MEGABYTES • Each memory location is given a name
program data • The name is the variable that refers to the data
• A program is a sequence of instructions assembled for stored in that location
some given task – e.g. rollNo, classSize
• Most instructions operate on data • Variables have types that define the interpretation
• Some instructions control the flow of the operations of data
• It is even possible to treat programs as data. By doing so a – e.g. integers (1, 14, 25649), or characters (a, f, G, H)
program could even modify itself.
SD, PSK, NSN, DK, TAG – CS&E, IIT M 15 SD, PSK, NSN, DK, TAG – CS&E, IIT M 16

Instructions Programs
• Instructions take data stored in variables as • A program is a sequence of instructions
arguments • Normally the processor works as follows,
• Some instructions do some operation on the data – Step A: pick next instruction in the sequence
and store it back in some variable – Step B: get data for the instruction to operate upon
– e.g. The instruction “X X+1” on integer type says – Step C: execute instruction on data (or “jump”)
that “Take the integer stored in X, add 1 to it, and – Step D: store results in designated location (variable)
store it back in (location) X”
– Step E: go to Step A
• Other instructions tell the processor to do
• Such programs are known as imperative
something
programs
– e.g. “jump” to a particular instruction next, or to exit
SD, PSK, NSN, DK, TAG – CS&E, IIT M 17 SD, PSK, NSN, DK, TAG – CS&E, IIT M 18

3
09/08/17

Programming Paradigms A Limitation Computer Arithmetic


• Imperative programs are sequences of instructions. They • Number of digits that can be stored is limited
are abstractions of how the von Neumann machine
operates
• Pascal, C, Fortran • Causes serious problems
• Object Oriented Programming Systems (OOPS) model
the domain into objects and interactions between them
• Simula, CLOS, C++, Java Consider a computer that can store:
• Logic programs use logical inference as the basis of Sign, 3 digits and a decimal point
computation
• Prolog Sign and decimal point are optional
• Functional programs take a mathematical approach of
functions
• LISP, ML, Haskell example : 212, -212, -21.2, -2.12, -.212
SD, PSK, NSN, DK, TAG – CS&E, IIT M 19 SD, PSK, NSN, DK, TAG – CS&E, IIT M 20

More Examples Why?


• 113. + -111. = 2.00 Consider 113. + -111. + 7.51
• 2.00 + 7.51 = 9.51
• -111. + 7.51 = -103.49 (exact arithmetic) To us addition is associative
(a+b)+c = a+(b+c)
But our computer can store only 3 digits.
So it rounds –103.49 to –103 (113. + -111.) + 7.51 = 2.00 + 7.51 = 9.51
113. + (-111. + 7.51) = 113. – 103. = 10.0
This is a very important thing to know as a system
designer. Why?
SD, PSK, NSN, DK, TAG – CS&E, IIT M 21 SD, PSK, NSN, DK, TAG – CS&E, IIT M 22

Conclusion Books
• P. Deitel and H. Deitel. C: How to Program.
• Computer is fast but restricted • V. Rajaraman. Computer Programming in C.
• R. G. Dromey. How to Solve It By Computer.
• So we must learn to use its speed • Kernighan and Ritchie. The C Programming
Language.
• And manage its restrictions • Kernighan and Pike. The Unix Programming
Environment.

SD, PSK, NSN, DK, TAG – CS&E, IIT M 23 SD, PSK, NSN, DK, TAG – CS&E, IIT M 24

4
09/08/17

Building Blocks of a Computer The Blocks, Their Functions


• Input Unit
Central
Processing
– Takes inputs from the external world via variety of
Unit input devices – keyboard, mouse, etc.
Control Unit
• Output Unit
– Sends information (after retrieving, processing) to
output devices – monitors/displays, projectors, audio
Input Memory ALU Output
devices, etc.

System Bus

SD, PSK, NSN, DK, TAG – CS&E, IIT M 25 SD, PSK, NSN, DK, TAG – CS&E, IIT M 26

More (try more filename on your Unix/Linux machine) Some More (commands are in /bin, /usr/bin. Use ls)
• Memory • System Bus
– Place where information is stored – Essentially a set of wires, used by the other units to
– Primary Memory communicate with each other
• Electronic devices, used primarily for temporary storage – transfers data at a very high rate
• Characterized by their speedy response • ALU – Arithmetic and Logic Unit
– Secondary Memory – Processes data - add, subtract, multiply, …
• Devices for long-term storage
– Decides – after comparing with another value, for
• Contained well tuned mechanical components, magnetic
example
storage media – floppies, hard disks
• Compact Disks use optical technology

SD, PSK, NSN, DK, TAG – CS&E, IIT M 27 SD, PSK, NSN, DK, TAG – CS&E, IIT M 28

Finally (check man cp, man mv, man ls, man k search string) The CPU (editors vi, emacs used to create text)
• Control Unit • Can fetch an instruction from memory
– Controls the interaction among other units • Execute the instruction
– Knows each unit by its name, responds to requests • Store the result in memory
fairly, reacts quickly on certain critical events • A program – a set of instructions
– Gives up control periodically in the interest of the • An instruction has the following structure
system Operation operands destination
• A simple operation
CPU = Control Unit + ALU add a, b Adds the contents of memory locations a and b
and stores the result in location a

SD, PSK, NSN, DK, TAG – CS&E, IIT M 29 SD, PSK, NSN, DK, TAG – CS&E, IIT M 30

5
09/08/17

Compilers Assembly Language


Human friendly languages à source code
• An x86/IA-32 processor can execute the
Source code in a Source code in a following binary instruction as expressed in
Higher Level Language 1 Higher Level Language n
machine language:
Compiler Compiler
Binary: 10110000 01100001
mov al, 061h
Assembly language code – Move the hexadecimal value 61 (97 decimal) into the
processor register named “al”.
Assembler, linker, loader – Assembly language representation is easier to
remember (mnemonic)

machine language code From Wikipedia

Machine understandable language


SD, PSK, NSN, DK, TAG – CS&E, IIT M 31 SD, PSK, NSN, DK, TAG – CS&E, IIT M 32

Higher Level Languages Data Representation


• Higher level statement = many assembly • Integers – Fixed Point Numbers
instructions
• For example “X = Y + Z” could require the
following sequence Decimal System - Base 10 uses 0,1,2,…,9
– Fetch the contents of Y into R1 (396)10 = (6 × 100) + (9 × 101) + (3 × 102) = (396)10
– Fetch the contents of Z into R2
– Add contents of R1 and R2 and store it in R1 Binary System - Base 2 uses 0,1
– Move contents of R1 into location named X
(11001)2 = (1 × 20)+(0 × 21)+(0 × 22)+(1 × 23)+(1 × 24)
= (25)10

SD, PSK, NSN, DK, TAG – CS&E, IIT M 33 SD, PSK, NSN, DK, TAG – CS&E, IIT M 34

Decimal to Binary Conversion Largest Number that can be Stored in m-digits


Convert (39)10 to binary form
Base = 2 base - 10 : (99999…9) = 10m - 1
2 39 39 = 2*19 + 1 base - 2 : (11111…1) = 2m - 1
2 19 + Remainder 1 = 2*(2*9 +1) + 1
= 22*9 + 21*1 + 1
2 9 + Remainder 1 = 22*(2*4+1) + 21*1 + 1
2 4 + Remainder 1 = 23*4+22*1+ 21*1 + 1
2 2 + Remainder 0 = 23*(2*2+0)+22*1+ 21*1 + 1 m=3 (999) = 103 - 1
= 24*2+23*0+ 22*1+ 21*1 + 1
2 1 + Remainder 0 = 24*(2*1+0) + … (111) = 23 - 1
0 + Remainder 1 = 25*1+24*0+23*0+22*1+ 21*1+ 1
Limitation: Memory cells consist of 8 bits (1 byte)
Put the remainders in reverse order multiples, each position containing 1 binary digit
(100111)2 = (1 × 20 )+(1 × 21)+(1 × 22)+(0 × 23)+(0 × 24)+(1 × 25)
= (39)10 35 36
SD, PSK, NSN, DK, TAG – CS&E, IIT M SD, PSK, NSN, DK, TAG – CS&E, IIT M

6
09/08/17

Sign-Magnitude Notation One’s Complement Notation


In the one’s complement method, the negative of integer n
Common cell lengths for integers : k = 16 or 32 or 64 bits is represented as the bit complement of binary n
000 : 0 000 : 0
First bit is used for a sign 001 : +1 E.g. : One’s Complement of (3)10 in a 3-bit cell 001 : +1
0 – positive number 010 : +2 010 : +2
complement of 011 : 100
011 : +3 011 : +3
1 – negative number 100 : -0 -3 is represented as = (100)2 100 : -3
101 : -1 101 : -2
The remaining bits are used to store the binary 110 : -2 Arithmetic requires care: 110 : -1
magnitude of the number. 111 : -3 111 : -0
2 + (-3) = 010 + 100 = 110 – ok
Limit of 16 bit cell : (32,767)10 = (215 – 1)10 Zero has two But, 3 + (-2) = 011 + 101 = 000 and carry of 1
Zero has two
representations! representations!
Limit of 32 bit cell : (2,147,483,647)10 = (231 – 1)10 need to add back the carry to get 001!

37
NOT WIDELY USED 38
SD, PSK, NSN, DK, TAG – CS&E, IIT M SD, PSK, NSN, DK, TAG – CS&E, IIT M

Two’s Complement Notation Two’s Complement Notation


In the two’s complement method, the negative of integer n
in a k-bit cell is represented as 2k – n The two’s complement notation admits one more negative
number than the sign - magnitude and one’s complement
Two’s Complement of n = (2k – n) notations.
E.g. : Two’s Complement of (3)10 in a 3-bit cell To get back n, read off the sign from the MSB 000 : 0
-3 is represented as (23 - 3)10 = (5)10 = (101)2
If –ve, to get magnitude, complement the cell and 001 : +1
010 : +2
Arithmetic requires no special care: 000 : 0 add 1 to it! 011 : +3
001 : +1
2 + (-3) = 010 + 101 = 111 – ok 010 : +2 E.g.: 101 à 010 à 011 = (-3)10 100 : -4
011 : +3 101 : -3
3 + (-2) = 011 + 110 = 001 and carry of 1 100 : -4 (8 – 4) 110 : -2
101 : -3 (8 – 3)
we can ignore the carry! 111 : -1
110 : -2 (8 – 2)
111 : -1 (8 – 1)
WIDELY USED METHOD for –ve numbers
SD, PSK, NSN, DK, TAG – CS&E, IIT M 39 SD, PSK, NSN, DK, TAG – CS&E, IIT M 40

Numbers with Fractions Binary Fraction à Decimal Fraction

Integer Part + Fractional Part (10.11)2

Decimal System - base 10 Integer Part (10)2 = 1*21 + 0*20 = 2


235 . 7846
1 1
Binary System - base 2 Fractional Part (11)2 = + = 0.5 + 0.25 = 0.75
2 22
(10011 . 11101)2 = (19.90625) 10
7 8 4 6 Decimal Fraction = ( 2.75 )10
Fractional Part (0.7846)10 =
10
+ + +
10 2 103 10 4

1 1 1 0 1
Fractional Part (0.11101)2 = + + + + = 0.90625
2 22 23 24 25

SD, PSK, NSN, DK, TAG – CS&E, IIT M 41 SD, PSK, NSN, DK, TAG – CS&E, IIT M 42

7
09/08/17

Decimal Fraction à Binary Fraction (1) Decimal Fraction à Binary Fraction (2)
Convert (0.90625)10 to binary fraction Convert (0.9)10 to binary fraction
0.90625 0.9
×2 0.90625 = ½(1+0.8125) ×2
= ½(1+ ½(1+0.625)) For some fractions, we do
0.8125 + integer part 1 0.8 + integer part 1
×2 = ½(1+ ½(1+ ½(1+0.25))) ×2 not get 0.0 at any stage!
0.625 + integer part 1 = ½(1+½(1+ ½(1+½(0+0.5)))) 0.6 + integer part 1 These fractions require an
×2 = ½(1+½(1+½(1+½(0+½(1+0.0))))) ×2 infinite number of bits!
0.25 + integer part 1 = ½+1/22+1/23+0/24 +1/25 0.2 + integer part 1 Cannot be represented
×2 = (0.11101)2 ×2 exactly!
0.5 + integer part 0 0.4 + integer part 0
×2 ×2
0 + integer part 1 0.8 + integer part 0 Repetition
Thus, (0.90625)10 = (0.11101)2 (0.9)10 = 0.11100110011001100 . . . = 0.11100
SD, PSK, NSN, DK, TAG – CS&E, IIT M 43 SD, PSK, NSN, DK, TAG – CS&E, IIT M 44

Fixed Versus Floating Point Numbers Scientific Notation (Decimal)


Fixed Point: position of the radix point fixed
and is same for all numbers 0.0000747 = 7.47 × 10-5
31.4159265 = 3.14159265 × 101
E.g.: With 3 digits after decimal point:
0.120 * 0.120 = 0.014 9,700,000,000 = 9.7 × 109
A digit is lost!!
Floating point numbers: radix point can float Binary
1.20 × 10-1 * 1.20 × 10-1 = 1.44 × 10-2 (10.01)2 = (1.001)2 × 21
Floating point system allows a much wider range of (0.110)2 = (1.10)2 × 2-1
values to be represented
SD, PSK, NSN, DK, TAG – CS&E, IIT M 45 SD, PSK, NSN, DK, TAG – CS&E, IIT M 46

Using Floating Point Notation


For any number x 32 bits :
x ≈ ± q × 2n First bit for sign
q – mantissa Next 8 bits for exponent
n – exponent 23 bits for mantissa

(-39.9)10 = (-100111.1 1100)2 = -39. 90000152587890625

= (-1.001111 1100)2 × 25
Decimal Value of stored number (-39.9)10
= (-1. 001111 1100 1100 1100 11001) × 25

23 bit

SD, PSK, NSN, DK, TAG – CS&E, IIT M 47

You might also like