Computer Programming in C Kishore B. M.Tech., (PhD) Asst. Professor – Sr. Scale, Dept. of Computer Engineering SOSE +601176689554 [email protected]
Nov 07, 2015
Computer Programming
in C
Kishore B. M.Tech., (PhD)
Asst. Professor Sr. Scale, Dept. of Computer Engineering
SOSE
+601176689554
COURSE DESCRIPTION
CP 2
1. Course Code EYB 1034
2. Course Title COMPUTER PROGRAMMING
3. L T P 2 0 1 4. Credits 3
5. Pre-requisite NIL
6. Teaching Department
School of Science and Engineering (SOSE)
7. Course Instructor Kishore B
Course Objectives
CP 3
Apply the fundamental principles of problem solving in software development and basic
programming principles using C language.
Design and develop program applications using C language.
Debug in troubleshooting program application.
Perform efficiently in accomplishing any given task.
Course Outline
CP 4
Introduction to Computers Number Systems Algorithms and Flowcharts C Fundamentals Operators and Expressions Flow of Control 1-Dimensional and 2-Dimensional Arrays Functions Structures Pointers Matlab
Textbook References
CP 5
E. Balaguruswamy, Computing Fundamentals & C Programming, TataMcGraw Hill,
2008.
E. Balaguruswamy, Programming in ANSI C, ed 5, McGrawhill.
Paul Deitel, Harvey Deitel, C how to program, Pearson, 2008
Ellis Horowitz, Sartaj Sahni, Sanguthevar Rajasekaran, Computer Algorithms, Galgotia Publications, 2001.
Thomas H. Cormen, Charles E. Leiserson, Ronal L, Rivest, Clifford Stein, Introduction to Algorithms, 2nd Edition, PHI, 2006.
Herbert Schildt,C: The Complete Reference, 4th edition, Tata McGrawHill, 2000. MATLAB Dr. Duane Hanselman, Bruce R. Littlefield, "Mastering Matlab", 2013, Pearson, Prentice
hall
Craig Lent, Learning to Program with MATLAB: Building GUI Tools, University of Notre Dame, John Wiley & Sons, Inc., 2013
Agam Kumar Tyagi, MATLAB and Simulink for Engineers, University of Petroleum & Energy Studies, Oxford University Press, 2012
Assessments
CP 6
Assessment Marking % Topics Date Time Submit to
Test 1(Classroom) 15 Fundamentals Basics, controls
TBA TBA Mr. Kishore
Test 2(Classroom) 15 Arrays, Functions, Structures, pointers
TBA TBA Mr. Kishore
Assignment / Quiz 20 Mr. Kishore
End semester exam 50 TBA
Introduction to Computers
What is a computer?
A computer is an electronic device, operating under the
control of instructions (software) stored in its own memory
unit, that can accept data (input), manipulate data (process),
and produce information (output) from the processing.
Generally, the term is used to describe a collection of devices
that function together as a system.
Devices that comprise a Computer system
Printer (output)
Monitor (output)
Speaker (output)
Scanner (input)
Mouse (input)
Keyboard (input)
System unit
(processor, memory)
Storage devices
(CD-RW, Floppy, Hard disk, zip,)
What Does A Computer Do?
Computers can perform four general operations, which
comprise the information processing cycle.
Input
Process/Computation
Storage
Output
Data and Information
Computer processing requires data, which is a collection of raw facts, figures and symbols, such as numbers, words,
images, video and sound, given to the computer during the input phase.
Computers manipulate/processes data to create information.
Information is data that is organized, meaningful, and useful.
During the output Phase, the information that has been created is put into some form, such as a printed report.
The information can also be put in computer storage for future use.
Why Is A Computer So Powerful?
The ability to perform the information processing cycle with
amazing speed.
Reliability (low failure rate).
Accuracy.
Ability to store huge amount of data and information.
Ability to communicate with other computers.
How Does a Computer Know what to do?
It must be given a detailed list of instructions, called a computer
program or software, that tells it exactly what to do.
Before processing a specific job, the computer program
corresponding to that job must be stored in memory.
Once the program is stored in memory the computer can start the
operation by executing the program instructions one after the
other.
Block Diagram of Computer
Arithmetic Logic
Unit
Primary storage
Control unit
Input Device
Auxiliary storage
Device
Output Device
Central Processing
unit
Peripherals
input, output, or auxiliary storage devices attached to a
computer
Input Devices include keyboard and mouse, scanners.
Output Devices include printers, video display, LCD screens.
Auxiliary Devices/Secondary Storage Devices include disk
drives, CD-ROM and DVD-ROM drives, modems, and digital
cameras.
An external device connected to the CPU, used to feed data
and instructions for solving the problem at hand
The most commonly used input device is the Keyboard and
some of the other input devices are listed below
Mouse
Joystick
Light pen
Trackball
Optical Scanner
Voice input
Input Device
It is used to display the results.
The most commonly used output device is the monitor. Some of the other output devices are
printer
plotter
plasma display panels
LCD displays
voice output
Output Device
System Unit
Data and instructions received from the input device are
stored and processed in the System unit. The Central
Processing Unit and Memory Unit are together called as
System Unit.
Arithmetic Logic
Unit
Primary storage
Control unit
Input Device
Auxiliary storage
Device
Output Device Central Processing
unit
Central Processing Unit
Once the data and instructions received from the input device, they are processed in this unit. This is generally called CPU. Further it consists of two functional units.
Control Unit (CU)
Arithmetic and Logic Unit (ALU)
Arithmetic Logic
Unit
Primary storage
Control unit
Input Device
Auxiliary storage
Device
Output Device Central Processing
unit
Arithmetic/Logic Unit
Performs arithmetic and logical micro operations:
Example:
arithmetic(+,-) and
logical (
Control Unit
Controls the order in which your program instructions are
executed.
Functions of CU:
Fetches data and instructions to main memory
Interprets these instructions
Controls the transfer of data and instructions to and from
main memory
Controls input and output devices.
Overall supervision of computer system
Memory Unit
It is a storage device where the data and instructions fed by
the user are stored.
It is an ordered sequence of storage cells, each capable of
holding a piece of information
Each cell has its own unique address
The information held can be input data, computed values,
or your program instructions.
Memory Unit
Representation:
Memory Unit
The computer memory is measured in terms of bits, bytes and
words.
A bit is a binary digit either 0 or 1.
A byte is unit of memory and is defined as sequence of 8 bits.
The word can be defined as a sequence of 16/32/64 bits or 2/4/8
bytes respectively depending on the machine architecture.
http://www.coolnerds.com/newbies/kbmbgb/sizeandspeed.htm
The computer memory is classified into
Main memory-Primary storage
Secondary memory-Auxiliary storage
Cache Memory
Memory
It is the place where the data and instructions, currently being
executed are stored.
Located outside CPU
High speed.
This is a temporary memory because the data and
instructions stored here get erased when the power goes off.
It is a semiconductor memory and measured in terms of
megabytes and gigabytes.
Eg. RAM and ROM
Main/Primary Memory
RAM stands for Random Access Memory
It is the read and write memory. The information typed by the user are stored in this memory and any memory location can be accessed
directly without scanning it sequentially.
Hence it is called as random access memory. During power failure the information stored in it will be erased.
It is also called as volatile memory. ROM stands for Read Only Memory
Permanent memory and non volatile. The contents in locations in ROM can not be changed It stores mainly stored program and basic input output systems programs.
Main/Primary Memory
Secondary/Auxiliary Storage Devices
Because most of main memory is volatile and limited, it is
essential that there should be other types of storage devices
where programs and data can be stored when they are no
longer being processed.
Secondary storage devices can be installed within the
computer box at the factory or added later as needed.
It is a non volatile memory and made up of magnetic material
and stores large amount of information for long time.
Low speed.
Holds programs not currently being executed.
Input, output, and auxiliary/secondary storage devices attached
to a computer are called peripherals.
E.g. Magnetic tapes, magnetic disks, compact disks etc.
Secondary/Auxiliary Storage Devices
Auxiliary Storage Devices
(Floppy) disks
Hard disks
CD-ROMs
Flash drives
and more
Cache memory
It is a High speed memory and placed between the CPU and
the main memory.
Users can not access this memory.
It stores the data and instructions currently to be executed.
More costlier than main memory.
Less Capacity than main memory
The Bus
Computer components are connected by a bus.
A bus is a group of parallel wires that carry control signals and
data between components.
Arithmetic Logic
Unit
Primary storage
Control unit
Input Device
Auxiliary storage
Device
Output Device Central Processing
unit
Hardware vs. Software
Hardware :
The physical elements of a computing system
(printer, circuit boards, wires, keyboard).
Software:
The programs that provide the instructions for a
computer to execute.
Classification of Software
Software
Application Software
System Software
Classification of Software
System software consists of programs that manages the
computer resources.
Divided into three classes
Operating System-user interface, database access
System support software-other operational services
like disk format programs etc
System Development software - language
translators and debugging tools etc
Application Software
Application Software is directly responsible for helping users
solve their problems.
For example
Word processing Electronic spreadsheet Database Presentation graphics
MS Word
MS Excel
MS Access
MS Powerpoint
Computer Languages
Machine Language- The only programming language available in earlier days
Consists of only 0s and 1s; e.g.:- 10101011
Symbolic language or Assembly language-
symbols or mnemonics used to represent instructions
hardware specific
e.g. ADD X,Y; Add the contents of y to x
High-level languages- English like language using which the programmer can write programs to solve a problem.
more concerned with the problem specification and not oriented towards the details of computer.
e.g.: C, C++, C#, Fortran, BASIC, Pascal etc.
basically translator programs
used to translate programs written in one programming
language to machine level language.
e.g.:- Interpreters, Compilers and Assemblers.
Language Translators
Compiler : Entire high level language program as input and translates it into machine language at a time. e.g.:- C, C++ compilers Interpreter : Program which translates one statement of a high level language program into machine language at a time and executes it. e.g.:- Basic Interpreters, Java Interpreters.
Assembler : is a program which translates an assembly language
program into machine language.
e.g.:- TASM(Turbo ASseMbler), MASM(Macro ASseMbler).
Language Translators
Compiler Interpreter
1. Takes entire high level language program as input and translate into machine language
Takes one statement of a high level language program as input & translates into machine language & executes.
2. Errors that occur in the program are listed & displayed.
Errors that occur only in the statement being taken for translation are displayed.
3. Debugging is faster. Debugging is slower.
4. Requires more memory. Requires less memory.
5. Costlier Cheaper.
Compiler vs Interpreter
Operating System basics
Operating System is an integrated collection of programs which make the
computer operational and help in executing user programs.
It acts as interface between the man and machine.
It manages the system resources like memory, processors, input-output
devices and files.
Types:
Single user [DOS] and Multi user OS [Windows XP, Unix],
Real time OS [Windows CE, QNX],
Single-tasking [DOS] and Multi-tasking [Unix],
Distributed OS [Amoeba],
Embedded OS [Windows CE, Symbian OS] etc.
Operating System as an Interface
So an Operating System
A program that runs on the raw hardware and supports Resource Sharing
Abstracts and standardizes the interface to the user across different types of hardware OS hides the messy details which must be performed
Manages the hardware resources Each program gets time with the resource
Each program gets space on the resource
Manages different goals: Use hardware efficiently and give maximum performance to each user.
Questions
What is a computer?
What do you mean by stored program concept? Who
suggested this idea?
What is CPU? What it consists of?
What are the functions of control unit?
Differentiate between
RAM and ROM
PROM and EPROM
Main memory and secondary memory
High level language and m/c level language
Give an example which can be used as both i/p and o/p device
What is an O.S?
Common Number Systems
System
Base
Symbols
Used by
humans?
Used in
computers?
Decimal 10 0, 1, 9 Yes No
Binary 2 0, 1 No Yes
Octal 8 0, 1, 7 No No
Hexa-
decimal
16 0, 1, 9,
A, B, F
No YES/No
Number Systems
Quantities/Counting
Decimal
Binary
Octal
Hexa-
decimal
0 0 0 0
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
Decimal
Binary
Octal
Hexa-
decimal
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
Decimal Number System
The decimal number system is also known as base 10.
The values of the positions are calculated by taking 10
to some power.
Why is the base 10 for decimal numbers?
Because we use 10 digits, the digits 0 through 9.
Binary Number System
The binary number system is also known as base 2.
The values of the positions are calculated by taking 2
to some power.
Why is the base 2 for binary numbers?
o Because we use 2 digits, the digits 0 and 1.
Binary Number System
The binary number system is also a positional
numbering system.
Instead of using ten digits, 0 - 9, the binary system
uses only two digits, 0 and 1.
Example of a binary number and the values of the
positions:
1 0 0 1 1 0 1
26 25 24 23 22 21 20
Hexadecimal Number System
The hexadecimal number system is also known as base 16. The
values of the positions are calculated by taking 16 to some
power.
Why is the base 16 for hexadecimal numbers ?
Because we use 16 symbols, the digits 0 through 9 and the
letters A through F.
Hexadecimal Number System
Binary Decimal Hexadecimal Binary Decimal Hexadecimal
0 0 0 1010 10 A 1 1 1 1011 11 B
10 2 2 1100 12 C
11 3 3 1101 13 D
100 4 4 1110 14 E
101 5 5 1111 15 F
110 6 6
111 7 7
1000 8 8
1001 9 9
Example of a hexadecimal
number and the values of the
positions:
3 C 8 B 0 5 1
166 165 164 163 162 161 160
Conversion Among Bases
The possibilities:
Hexadecimal
Decimal Octal
Binary
Example Different forms
2510 = 110012 = 318 = 1916
Base
Decimal to Decimal (for understanding)
12510 => 5 x 100 = 5
2 x 101 = 20
1 x 102 = 100
125
Base
Weight
Binary to Decimal
Technique
Multiply each bit by 2n, where n is the weight of
the bit
The weight is the position of the bit, starting from 0
on the right
Add the results
Example- Binary to Decimal
1010112 => 1 x 20 = 1
1 x 21 = 2
0 x 22 = 0
1 x 23 = 8
0 x 24 = 0
1 x 25 = 32
4310
Bit 0
Octal to Decimal
Technique
Multiply each bit by 8n, where n is the weight of
the bit
The weight is the position of the bit, starting from 0
on the right
Add the results
Example- Octal to Decimal
7248 => 4 x 80 = 4
2 x 81 = 16
7 x 82 = 448
46810
Hexadecimal to Decimal
Technique
Multiply each bit by 16n, where n is the weight
of the bit
The weight is the position of the bit, starting from 0
on the right
Add the results
Example- Hexadecimal to Decimal
ABC16 => C x 160 = 12 x 1 = 12
B x 161 = 11 x 16 = 176
A x 162 = 10 x 256 = 2560
274810
Decimal to Binary
Technique
Divide by two, keep track of the remainder
First remainder is bit 0 (LSB, least-significant bit)
Second remainder is bit 1
So on.
Example- Decimal to Binary
12510 = ?2 2 125
62 1 2
31 0 2
15 1 2
7 1 2
3 1 2
1 1 2
0 1
12510 = 11111012
Octal to Binary
Technique
Convert each octal digit to a 3-bit equivalent
binary representation
Example- Octal to Binary
7058 = ?2
7 0 5
111 000 101
7058 = 1110001012
Hexadecimal to Binary
Technique
Convert each hexadecimal digit to a 4-bit
equivalent binary representation
Example- Hexadecimal to Binary
10AF16 = ?2
1 0 A F
0001 0000 1010 1111
10AF16 = 00010000101011112
Decimal to Octal
Technique
Divide by 8
Keep track of the remainder
Example- Decimal to Octal
123410 = ?8
8 1234
154 2 8
19 2 8
2 3 8
0 2
123410 = 23228
Decimal to Hexadecimal
Technique
Divide by 16
Keep track of the remainder
Example- Decimal to Hexadecimal
123410 = ?16
123410 = 4D216
16 1234
77 2 16
4 13 = D 16
0 4
Binary to Octal
Technique
Group bits in threes, starting on right
Convert to octal digits
Example- Binary to Octal
10110101112 = ?8
1 011 010 111
1 3 2 7
10110101112 = 13278
Binary to Hexadecimal
Technique
Group bits in fours, starting on right
Convert to hexadecimal digits
Example- Binary to Hexadecimal
10101110112 = ?16
10 1011 1011
2 B B
10101110112 = 2BB16
Octal to Hexadecimal
Technique
Use binary as an intermediary
Example- Octal to Hexadecimal
10768 = ?16
1 0 7 6
001 000 111 110
2 3 E
10768 = 23E16
Hexadecimal to Octal
Technique
Use binary as an intermediary
Example- Hexadecimal to Octal
1F0C16 = ?8
1 F 0 C
0001 1111 0000 1100
1 7 4 1 4
1F0C16 = 174148
Exercise Convert ...
Do not use a calculator!
Decimal
Binary
Octal
Hexa-
decimal
33
1110101
703
1AF
Decimal
Binary
Octal
Hexa-
decimal
33 100001 41 21
117 1110101 165 75
451 111000011 703 1C3
431 110101111 657 1AF
Answer
Common Powers
Base 10
Power Preface Symbol
10-12 pico p
10-9 nano n
10-6 micro
10-3 milli m
103 kilo k
106 mega M
109 giga G
1012 tera T
Value
.000000000001
.000000001
.000001
.001
1000
1000000
1000000000
1000000000000
Common Powers
Base 2 Power Preface Symbol 210 kilo kB
220 mega MB
230 Giga GB
Value
1024
1048576
1073741824
What is the value of kB, MB, and GB ?
In computing, particularly w.r.t. memory, the base-2
interpretation generally applies.
240 tera TB
250 peta PB
260 exa EB
270 zetta ZB
280 yotta YB
1099511627776
1125899906842624
1152921504606846976
1180591620717411303424
1208925819614629174706176
Review multiplying powers
For common bases, add powers
26 210 = 216 = 65,536
or
26 210 = 64 210 = 64k
ab ac = ab+c
Binary Addition
Two 1-bit values
A B A + B
0 0 0
0 1 1
1 0 1
1 1 10 two
Binary Addition
Two n-bit values
Add individual bits
Propagate carries
E.g.,
10101 29
+ 11001 + 25
101110 54
1 1 1
Multiplication
Decimal (just for understanding)
35
x 105
175
000
35
3675
Multiplication
Binary, two 1-bit values
A B A B
0 0 0
0 1 0
1 0 0
1 1 1
Multiplication
Binary, two n-bit values
As with decimal values
E.g., 1110 x 1011
1110
1110
0000
1110
10011010
Fractions
Decimal to decimal (just for understanding)
3.14 => 4 x 10-2 = 0.04
1 x 10-1 = 0.1
3 x 100 = 3
3.14
Fractions
Binary to decimal
10.1011 => 1 x 2-4 = 0.0625
1 x 2-3 = 0.125
0 x 2-2 = 0.0
1 x 2-1 = 0.5
0 x 20 = 0.0
1 x 21 = 2.0
2.6875
Fractions
Decimal to binary 3.14579
.14579
x 2
0.29158
x 2
0.58316
x 2
1.16632
x 2
0.33264
x 2
0.66528
x 2
1.33056
etc. 11.001001...
Representation of Number Basic Idea
Hardware can only deal with binary digits, 0 and 1.
Must represent all numbers, integers or floating
point, positive or negative, by binary digits, called
bits .
Positive Integers
Decimal system: made of 10 digits, {0,1,2, . . . , 9}
41 = 4101 + 1100
255 = 2102 + 5101 + 5100
Binary system: made of two digits, {0,1}
00101001 = 027 + 026 + 125 + 024
+123 + 022 + 021 + 120
= 32 + 8 +1 = 41
11111111 = 255, largest number with 8 binary digits, 28-1
Writing Numbers
Most significant digit Least significant digit
Least significant bit
(LSB)
Most significant bit
(MSB)
2 5 5
0 0 1 0 1 0 0 1
1s Complement Representation
1s Complement Representation
Fixed size representation
Most significant bit is reserved as sign-bit
Positive numbers are represented using standard
binary notation
Negative numbers are represented by inverting all
bits in the standard binary notation
1s Complement - Example
Represent 1210 using 8-bit 1s Complement
Converting to binary: 1210 = 11002
8-bit 1s complement representation: 000011002
Sign bit is 0 to indicate positive value.
Represent -1310 using 8-bit SBN
Converting to binary: 1310 = 11012
8-bit representation: 000011012
Since original decimal number was negative each bit in 8-bit representation is inverted!
1s Complement representation = 11110010
Note: Sign bit becomes 1 to indicate negative value
1s Complement - Example
Convert 1s complement 000011112 to decimal
Sign bit is 0 to indicating positive value.
Converting 000011112 to decimal we get 1510
Final result: +1510
Convert 1s complement 111101012 to decimal
Sign bit is 1 indicating negative value.
First invert all the bits to get: 000010102
Convert above binary to decimal to get 1010
Final result: -1010
1s Complement
2 different representations for 0
+0 (00000000) and -0 (11111111)
However, A A operations can be easily
represented
A A = A + (-A) = 11111111 (which is effectively 0 in
1s complement)
2s Complement
Overcomes the limitations of 1s complement representation!
Fixed size representation
Enables subtraction of numbers via addition!
Also reserves a special sign bit (left most bit)
0 (sign bit) indicates positive numbers
1 (sign bit) indicates negative numbers
Conversion to and from 2s complement requires similar
steps
Several of them are identical to 1s complement
2s Complement Example
Represent +2010 using 8-bit 2s Complement
Since number is positive simply represent +2010 as a 8-bit binary number!
+2010 = 000101002
Represent -1810 using 8-bit 2s complement
Since number is negative we need to do 1s complement and add 12
Step 1: Convert 1810 to 8-bit binary = 000100102
Step 2: Invert bits = 111011012
Step 3: Add 12 = 111011102
Final result: -1810 = 1 1 1 0 1 1 1 0 2 in 8-bit 2s complement
2s Complement Conversion
Convert 8-bit 2s complement to decimal
Case 1: 000010102 Sign bit is 0 indicating positive number
Simply convert binary to decimal to get 1010
Case 2: 111110102
Sign bit is 1 indicating negative number
Step 1: Invert all bits to get 000001012
Step 2: Add 12 to get 000001102
Convert binary to decimal to get -610
Note the negative sign on decimal number!
Converting 2s Complement to Decimal
an-1an-2 . . . a1a0 = -2n-1an-1 + 2i ai
i=0
n-2
-128 64 32 16 8 4 2 1
8-bit conversion box
-128 64 32 16 8 4 2 1 1 1 1 1 1 1 0 1
Example
-128+64+32+16+8+4+1 = -128 + 125 = -3
Summary
Computer
Software
Hardware
Software Classification
Memory
Operating System
Number System
Number Conversions
Number Representations