Est102 - C - Module 1
Est102 - C - Module 1
PROGRAMMING IN C
MODULE 1
MODULE OVERVIEW:
INTRODUCTION TO COMPUTER:
● A computer is an electronic programmable device that can accept data,
process data and produce results based on the instructions given to it.
● It is used to process data into information that is useful to people.
● The word ‘programmable’ makes it unique as compared to other electronic
devices which perform only the function for which it was developed for. They
are not programmable. Eg: television, radio, etc.
● Computers can be used to carry out massive and sophisticated scientific as
well as commercial purposes.
DIKW Model:
Basic Components of a Computer System:
● The complete computer system has 4 components:
1. Hardware- any part of the computer system that one can see and touch.
2. Software- tells the computer what to do and makes the computer do
meaningful work.
3. Users-Computers work as per directions of the user.
4. Data- raw facts that are manipulated by the computer according to the
instructions provided by the software. Computers transform raw data into
useful information.
BASICS OF COMPUTER ARCHITECTURE:
● The hardware of a computer system can be categorised into following four
functional units :
1. Central processing unit (Processor)
2. Memory unit
3. Input devices
4. Output devices
Components of Computer Hardware(Figure):
CPU:
● Central Processing unit or Processor is the brain of the computer .
● The Central Processing Unit, also known as the processor is an electronic
circuitry designed on the motherboard to carry out operations of the computer
system.
● It contains arithmetic and logic unit (ALU), control unit (CU) and an array of
registers. The processor runs a program by executing the instructions one by
one.
The Memory Unit:
● The Memory unit refers to the area where instructions and data are stored in
a computer.
● The processor reads instructions and data from the memory, executes them
and writes the result back to it.
● Different forms of memory are used in a computer system. They vary in their
size, speed and location.
● A memory chip is a collection of storage cells together with associated
circuits needed to transfer information in and out of the storage.
● Anything and everything in a computer is stored in the form of bits known as
binary language or machine language.
● 8 bits make up a byte.
Input Devices & Output Devices:
● The Input Devices: - The input unit consists of input devices that are
attached to the computer. These devices take input from the user and convert
it into binary language that the computer understands. Some of the common
input devices are keyboard, mouse, joystick, scanner etc.
● The Output Devices: - The output unit consists of output devices that are
attached to the computer. It converts the binary data coming from CPU to
human understandable form. The common output devices are monitor,
printer, plotter etc.
CENTRAL PROCESSING UNIT:
● Central Processing unit or Processor is the brain of the computer .
● It is an electronic circuitry designed on the motherboard to carry out
operations of the computer system.
● It contains arithmetic and logic unit (ALU), control unit (CU) and an array
of registers.
● The processor runs a program by executing the instructions one by one.
● Each instruction execution cycle (IC) is made up of three phases;
-instruction fetch
-instruction decode
-instruction execution.
● System Clock: Within the CPU, events happen at a pace controlled by the
vibrations of a tiny quartz crystal called system clock.
● If CPU is the brain of the computer, then system clock is the heart of it.
● The system clock pulses at a rapid rate measured in millions of cycles per
second(MHz).
● A single beat of clock is called the clock cycle or clock tick.
● Depending on the type of processor, it may take one or more clock cycles to
carry out an instruction.
● System clock thus synchronises internal activities of the computer.
● As the clock speed is doubled, the number of instructions executed per
second is also doubled and so the operating speed of the CPU.
● Every CPU has essentially two sub components: Arithmetic Logic unit &
Control Unit.
● Arithmetic Logic unit:
Divide
● Control Unit:
-Acts as a supervisor by managing all resources of the computer system.
-It initiates, monitors and stops the flow of data from one part of computer
to the other.
-It neither process data nor store data, but simply directs and coordinates
the actions of other units of the system.
-The control unit controls and coordinates all operations of the CPU
by providing appropriate control and timing signals.
-
● Registers:
-The size of registers determines the amount of data with which the computer
can work at a given time.
CPU REGISTERS:
TYPE PURPOSE
● RAM is a volatile memory and loses its contents when the power is turned off.
● The circuit is so designed that any random location can be directly accessed
without the need to scroll up or down the memory.
● The program to be executed is loaded into RAM from the non-volatile backup memory.
● The processor reads instructions and data from the RAM, executes them and writes data
back to the RAM. Hence RAM is also known as the main memory of the computer.
● Everything from the RAM is copied to the non-volatile backup memory before the computer
is turned off.
● The two main types of RAM are Dynamic Ram and Static RAM.
● DRAM:
-Each memory cell in a DRAM is made of one transistor and one MOS capacitor, which
store one bit of data.
- However, this cell starts losing its charge and hence data can be retained for less than
thousandth of a second.
-So it needs to be refreshed thousand times a second, which takes up processor time.
-Each cell in SRAM is made of several transistors in a cross coupled flip flop
configuration that stores one bit.
- It retains its bit till the power supply is on and doesn’t need to be refreshed
like DRAM.
● ROM is a non-volatile memory and hence retains its contents even when the
computer is switched off.
● It is used for storing software that is rarely changed during the life of the
system, sometimes known as firmware.
● Almost every computer comes with a small amount of ROM containing the
boot firmware which is essential for the boot-up.
● BIOS is a firmware used to perform hardware initialization during the booting
process and to provide runtime services for operating systems and programs.
● The BIOS firmware comes pre-installed on the ROM and it is the first software
to run when the computer is powered on.
● ROM memories have gradually evolved from fixed read-only memories to
memories than can be programmed and then re-programmed.
1. ROM (Read Only Memory)
2. PROM (Programmable Read Only Memory)
3. EPROM (Erasable Programmable Read Only Memory).
4. EEPROM (Electrically Erasable Programmable Read Only Memory)
● CACHE MEMORY:
● Moving data between CPU registers and RAM is the most
time consuming task.
● Cache memory provides a solution for this problem.
● Cache memory is similar to RAM, but extremely faster.
● Once CPU reads data from RAM, it keeps one copy in the
registers and other in cache memory.
● For subsequent requirements of same data, CPU gets it from
the cache.
● Cache is made up of high speed semiconductor devices that
are costlier than RAM.
Memory hierarchy:
INPUT DEVICES:
● Devices that enables user to input data and commands into the computer.
● Such devices connects the external environment with the internal computer system.
● Input devices can send data to other devices, but can’t receive data from other
devices.
● Input unit performs the following functions:
-accept the data and instructions from the external environment.
-convert it into machine language.
-supply the converted data to computer system.
● Commonly used input devices are keyboard, mouse, microphone, webcam, MICR,
e-pens etc.
Keyboard:
● Most common input device for entering text and numbers.
● It accept text/numeric input from the computer user and sends that data
to a computer.
● Keyboards are available in many styles, but with identical layouts.
● The most common layout is the IBM enhanced keyboard with more than 100
keys which are grouped generally into 4 categories based on their usage.
Mouse:
● Palm sized pointing device with a ball built into the bottom.
● When mouse is moved over a smooth surface, the ball rolls and the pointer
on the display moves in the same direction.
● It allows to choose commands from easy to use menus and dialog
boxes.
● This frees the use from using a keyboard up to a large extent.
Microphone:
● Receives the sound generated by an input source and sends that sound to a
computer.
Electronic Pen:
● E-pens allow user to write directly on or point at a special pad or screen of a
pen based computer.
● Handwriting recognition being a complex task, pens are usually used for
making selection from a list or for creating short messages.
● This type of input is allowed by most of the handheld computers.
OUTPUT DEVICES:
Plotters:
-High speed.
-Difficult to manipulate
-Difficult to design
Operating System:
-Manage programs
-Manage memory
Compilation
● The object programming produced after code generation won’t be in a form
suitable for direct execution by a computer.
○ It may contain service requests from OS and other utility software.
○ The resultant object code created after compiling the modules in different files
may not have any link between them.
● The utility program linker acts in this context.
● Linker links several object modules, OS routines and other utility programs and
produce a complete executable program.
● The executable program is called the load module which will be stored as a file in
some storage device.
● Loader which is another utility software is responsible for loading this load module
from storage device to memory for execution.
● Therefore, compiler do not execute the program, but it convert the program into an
object program.
● The 3 steps process compile, link and load actually execute a program.
● Once compile and link steps are complete, program can be loaded and
executed repeatedly any number of times without returning to the source
code.
● Whenever a change is to be made in the program, it is done in the source
code and the modified source program is compiled and linked to produce a
new load module that can be directly executed.
Program execution process:
● A high level language program can't get executed directly. It requires some
translator to get it translated to machine language.
● There are two types of translators for high level language programs. They are
interpreter and compiler.
● In case of interpreter, prior execution, each and every line will get translated
and then executed.
● In case of compiler, the whole program will get translated as a whole and will
create an executable file. And after that, as when required, the executable
code will get executed.
● These translator programs, especially compilers, are huge one and so are
quite expensive.
INTRODUCTION - STRUCTURED PROGRAMMING:
● Structured programming is a method for designing and coding programs in a
systematic, organised manner.
● In structured programming, the program is executed by executing
instructions one after the other in a sequential manner.
● The languages that support Structured programming approach are C, C++,
Java, C# etc.,
● The structured program mainly consists of three types of statements:
○ Selection Statements
○ Sequence Statements
○ Iteration Statements
● Programs with complex flow of control are difficult to understand, modify and
debug.
● Structure programming controls the complexity of the flow of control by using
only a small number of simple, standard control structures.
● Also, a complex problem is divided into logical independent units and these
units are joined using basic structures.
Advantages of Structured program:
● Easy to understand and read since the modules can be considered
independently.
● Easier to understand since it is easier to identify the structure of a particular
program component.
● Easy to maintain & modify because individual modules can be changed
without changing the entire program.
● Easy to design since modules can be designed independently.
● Program uses single-entry and single-exit elements. Therefore a
structured program is well maintained, neat and clean.
● Development is easier as it requires less effort and time.
● Easy to Debug.
● When the principles of structured programming are applied the design of a
program, it leads to a well structured and easy to understand program.
● One of the goals of structured programming is;
○ to create programs those are easy for programmer to read(and
understand)
○ to create programs those are easy for users to run.
ALGORITHM:
● Program:Set of instructions expressed in a particular programming language.
● Algorithm:A solution to a problem that is independent of any programming
language.
● ie.,An algorithm consists of a set of explicit and unambiguous finite steps
when carried out for a set of initial conditions produce the required output and
terminate in a finite time.
● Algorithm has the following characteristics
○ Input: An algorithm may or may not require input
○ Output: Each algorithm is expected to produce at least one result
○ Definiteness: Each instruction must be clear and unambiguous.
○ Finiteness: If the instructions of an algorithm are executed, the algorithm
should terminate after finite number of steps.
● An algorithm consists of a number of elementary operations and instructions
about the order in which these operations are to be performed.
● To solve a particular problem, there can be several alternative algorithms.
● Generally, it makes sense to design an algorithm that is simple and easy to
understand since it will have the best chance of performing as intended.
● Algorithms are usually written independent of any programming language.
● But, it should be related to the features of the programming language which
we intend to use.
● Otherwise, actual coding phase would become difficult.
● In an algorithm, there are no rules for punctuation,spelling, vocabulary, use of
synonyms.
Eg1:Algorithm to find sum of two numbers:
● Step-1: Start
● Step-2: Input first number say A
● Step-3: Input second number say B
● Step-4: SUM = A +B
● Step-5: Display SUM
● Step-6: Stop
Eg2:Algorithm to find area and perimeter of a circle:
● Step-1 Start
● Step-2 Input Radius of Circle say R
● Step-3 AREA = 22.0/7.0 *R*R
● Step-4 PERIMETER = 2*22.0/7.0*R
● Step-5 Display AREA, PERIMETER
● Step-6 Stop
Flowchart:
● Graphical representation of an algorithm.
● It has been the primary logical tool of computer programmers.
● As the flowcharts are graphical representations, the logic of the programs
they represent is easily understood than that of a written program or
algorithm.
● It enable the programmer to finalise the logic of a problem independent of any
programming language.
● Flowcharts make use of special symbols , whose shape determines the
kind of operation to be performed.
● More than 25 symbols are there which are standardised by ANSI(American
National Standards Institution)
● Instructions are placed inside the symbols and connected by arrows to
indicate order of execution.
On-page connectors example;
● Flowchart for going to the market to purchase a pen.
These are some points to keep in mind while developing a flowchart −
● Flowchart can have only one start and one stop symbol.
● General flow of processes is top to bottom or left to right.
● Arrows should not cross each other.
● On-page connectors are referenced using numbers.
● Off-page connectors are referenced using alphabets.
Flow chart to find sum of two Flow chart to find area
numbers:
and perimeter of a
circle
Pseudo code:
● It is a program design technique that uses informal expressions in outline
form to describe the logic of a program.
● It is written in natural language rather than in a programming language.
● Pseudo code expressions are similar to those used in flowcharts,and is
intended to be independent from the programming language used.
● It has no syntax like any of the programming language and thus can't be
compiled or interpreted by the computer.
● It is the cooked up representation of an algorithm.
● Pseudo code,as the name suggests, is a false code or a representation of
code which can be understood by even a layman with some school level
programming knowledge.
Advantages of Pseudocode
● Improves the readability of any approach. It’s one of the best approaches to
start implementation of an algorithm.
● Acts as a bridge between the program and the algorithm or flowchart. Also
works as a rough documentation, so the program of one developer can be
understood easily when a pseudocode is written out.
● In industries, the approach of documentation is essential. And that’s where a
pseudo-code proves vital.
● The main goal of a pseudo code is to explain what exactly each line of a
program should do, hence making the code construction phase easier for the
programmer.
Disadvantages of Pseudocode
Input List: 5 1 4 2 8
procedure Bubble_Sort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end procedure