0% found this document useful (0 votes)
8 views

Module 1 Pps Updated

The document provides an introduction to computer programming, detailing the components of a computer system, including hardware and software. It classifies computers based on size and capacity, explaining the features of supercomputers, mainframe computers, minicomputers, and microcomputers, as well as the types of memory and software. Additionally, it describes the basic components of a computer, including input, output, and processing units, along with examples of input and output devices.

Uploaded by

madhuri06.b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Module 1 Pps Updated

The document provides an introduction to computer programming, detailing the components of a computer system, including hardware and software. It classifies computers based on size and capacity, explaining the features of supercomputers, mainframe computers, minicomputers, and microcomputers, as well as the types of memory and software. Additionally, it describes the basic components of a computer, including input, output, and processing units, along with examples of input and output devices.

Uploaded by

madhuri06.b
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 43

1

Module-1
Introduction to programming
Computer:It is a machine made of electronic devices that enable user to enter data(Input),
process it(CPU) and store it in a computer memory.
 The word “computer” is derived from the word “compute”.
 The electronic device is known as “Hardware” and the set of instructions is known as
“Software”.

Hardware: Computer hardware includes the physical components of a computer. Personal


computers (PCs) are electronic devices that use microchips to process information and may
include desktop computers, laptops or tablets. It is the physical equipment which can be touch
and feel.
Software: It is the collection of instructions that perform specific task and allow the hardware to
do its job.
Components of the computer system: The computer hardware is the physical equipment. The
hardware component of computer system consists of 5 parts.

A. Input Devices. B. Central Processing Unit ( CPU ). C. Primary Storage. D. Output Devices.
E. Auxiliary Storage Devices.

The following figure shows the hardware components.

Input Devices: The input device is usually a keyboard where programs and data are entered in to
the computer. Other Input Devices : a touch screen , a mouse , a pen , an audio input unit.
2

Central Processing Unit ( CPU ): It is responsible for executing instructions such as arithmetic
calculations , comparisons among data and movement of data inside the system. Today’s
computers may have one or more CPU’s.

Primary Storage: It is also known as main memory . It is a place where the programs and data is
stored temporarily during processing. The Data in primary storage is erased , when we turn off a
personal computer or when we log off from a time-sharing computer ( volatile ).

Output Devices: The output device is usually a monitor or a printer to show output. If the output
is shown on the monitor, it is a soft copy and if the output is printed on the printer, it is a hard
copy.

Auxiliary Storage Devices ( secondary storage devices ): It is used for both input and output. It
is also known as secondary storage. It is a place where the programs and data are stored
permanently. When we turn off the computer the programs and data remain in the secondary
storage, ready for the next time when we need them.

Classification of Computers Based on Size and Capacity


Computer Classification:
A computer is a device that transforms unusable data into information. According to the set of
instructions the user gives it, it processes the input and generates the desired outcome. Modern
digital computers are classified on the basis of their size and capacity. The size and data handling
capabilities of the various types of computers may be used to categorize them into two groups.
1. Computers according to Size:
 Supercomputer.
 Mainframe computer.
 Personal computer.
 Workstation.
 Minicomputer.

2. Computers according to their Capacity to manage data:


 Digital computer.
 Hybrid computer.
 Analog computer.
Classification According to Size:
There are four different sorts of computers based on their size and how they are configured to
operate:
1. Supercomputers
 The most efficient computers in terms of processing data and performance are
supercomputers. These computers are used for research and exploratory purposes.
3

 Supercomputers are exceedingly large and highly expensive. It can only fit in large, air-
conditioned spaces.
 Supercomputers are used for a range of tasks, such as space exploration, seismic research,
and the testing of nuclear weapons.

Supercomputer Features:
 They make use of AI (Artificial intelligence)
 They are the fastest and strongest;
 They are very costly.
 They are enormous in size.
 They are employed by companies that manufacture goods.
 They process information at a rapid rate.

2. Mainframe Computers

 Despite being less efficient than supercomputers, mainframe computers are nevertheless
extremely expensive.
 Large corporations and governmental organizations frequently employ mainframe
computers to run everyday operations.
4

 They have the ability to store and analyze a lot of data. To maintain information on their
customers, students, and insurance policyholders, banks, colleges, and insurance
companies utilize them.
 They may also act as a server in a network environment.
 Hundreds of users may be managed simultaneously by them.

Mainframe Computer Features:


 They have enormous amounts of memory.
 They are capable of running several different operating systems.
 They have a significant number of CPUs with powerful processing speeds.
 Tightly Coupled Clustering Technology is employed.

3. Minicomputers
 Minicomputers are used by small businesses and industries. They go by the term
"Midrange Computers."
 These minicomputers frequently have several users, just as mainframe computers. They
are a bit slower than mainframe computers.
 For example, the manufacturing department may employ minicomputers to keep an eye
on specific production processes.

Features of Minicomputers:
 It is smaller than mainframes or supercomputers in terms of size.
 In comparison to a mainframe or supercomputer, it is less costly.
5

 It is able to perform many jobs at once.


 It may be utilized by several users simultaneously.
 It is utilized by small businesses.
4. Microcomputers.
 A microcomputer, sometimes referred to as a personal computer (PC), is a type of
computer that runs on a smaller scale than traditional computers .
 A component that is commonly referred to as a motherboard houses the central
processing unit (CPU), a microprocessor, memory in the form of ROM (Read Only
Memory), RAM (Random Access Memory), I/O ports, and a bus system of connecting
wires.
 They are the most affordable.

Features of Microcomputers:
 They are extensively employed for personal usage.
 They are smaller and comparably less expensive.
 Multi-user functionality is not supported.
 It has a limited computational capacity.
 They are quite simple to use.

Based on Capacity
According to fundamental operating principles, there are three different kinds of computers.
They are as follows:
6

1. Analogous Computers
 Analog computers process analog data. Temperature, pressure, weight, depth, and voltage
are a few examples of this type of data.
 These have an infinite range of values and are continuous quantities.
 The first computers were analog, and they laid the groundwork for today's digital
computers.

2. Digital Computers
 In digital computers, letters, numbers, and other special symbols are represented by
digits.
 On-off (ON-OFF) inputs are used by digital computers, and ON-OFF signals are also
generated by them.
 An ON is often represented by a 1 and an OFF by a 0, respectively. A digital computer is
capable of processing both numerical and non-numerical data.
 In addition to doing fundamental arithmetic operations like addition, subtraction,
multiplication, and division, it can also perform logical operations.

3. Hybrid Computers
 Computers that combine digital and analog components are called hybrid computers.
 It combines the best features of both types, having the speed of an analog computer with
the memory and precision of a digital computer.
7

 Hybrid computers are typically used in specific applications where both forms of data
need to be processed.
 As an example, a gas pump contains a processor that converts measurements of fuel flow
into information about quality and cost.

Memory is a storage part in a computer system. It is used to store the data, information, and
programs at the time of processing on the computer. It stores data either temporarily or
permanent. The main use of memory is saving and retrieving data.

Types of Memory:
Generally computer system consists of two types of memory −
Primary Memory or Volatile Memory
It is called the internal memory of the computer. And it is also known as main memory or
Temporary memory. It holds the data and instructions that are presently working on the system
8

or by the CPU. Primary Memory is called volatile memory, because when power is switched off
it loses all data.

Primary memory is generally of two types.


A) RAM B) ROM

RAM (Random Access Memory) − It stands for Random Access Memory. RAM is a read /writes
memory. It is referred as main memory of the computer system. It is a temporary memory. The
information stored in RAM is lost whenever the power supply to the computer is switched off.

RAM is also of two types which are as follows −


Static RAM − Static RAM also known as SRAM. In this RAM the information is stored as long
as the power supply is ON. SRAM are of higher coast and consume more power .They have
higher speed than Dynamic RAM

Dynamic RAM − Dynamic RAM also known as DRAM, This type of RAM stores information
in a very short time basically, a few milliseconds even though the power supply is ON. The
Dynamic RAM is cheaper and of moderate speed and also they consume less power.

ROM (Read Only Memory) − It stands for Read Only Memory. ROM is a permanent type of
memory. ROM information is not lost when power supply is switched off. The Content of ROM
is inserted by the computer manufacturer and permanently stored at the time of manufacturing.
ROM cannot be overwritten by the computer. It is also called Non- Volatile Memory.

ROM memory has three types’ names which are as following −

PROM (Programmable Read Only Memory) − It is used to write data once and read many.
Once a chip has been programmed, the recorded information cannot be changed. It is a non-
volatile memory.

EPROM (Erasable Programmable Read Only Memory) − EPROM chip can be programmed
by erasing the information stored earlier in it. Information stored in EPROM exposing the chip
for ultraviolet light.

EEPROM (Electrically Erasable Programmable Read Only Memory) − It is programmed


and erased by special electrical waves in milliseconds. A single byte of data or the entire
contents of the device can be erased.

Secondary Memory or Non Volatile Memory


9

It is an external memory of the computer. It is also known as Auxiliary memory or permanent


memory. It is used to store different programs and the information permanently. We call it a non-
volatile memory that means the data is stored permanently even if power is switched off.

The secondary storage devices are as follows −

Floppy Disks

Magnetic (Hard) Disk

Magnetic Tapes

Pen Drive

Winchester Disk

Optical Disk(CD,DVD)

Computer Software : Software is the collection of Programs ( instructions ) that allow the
hardware to do its job. They are two types of Computer Software.
A. System Software B. Application Software

The following figure shows the Computer Software.

System Software: System Software consists of programs that manage the hardware resources of
a computer and perform required information processing tasks.

These programs are divided in to three classes.

i. Operating System Software.

ii. System Support Software.

iii. System Development Software.

i. Operating System Software: It provides services such as a user interface, files and data
base access and interfaces to communication systems such as Internet protocols. The primary
purpose of this software is to keep the system operating in an efficient manner while
allowing the users access to the system.
ii. System Support Software : System Support Software provides system utilities and other
operating services. Examples of system utilities are sort programs and disk format programs.
Operating services consist of programs that provide performance statistics for the operational
staff and security monitors to protect the system and data.
10

iii. System Development Software: It includes language translators that convert programs in to
machine language for execution , debugging tools to ensure that programs are error - free and
computer -assisted software engineering ( CASE ) systems.

Application Software: It is directly responsible for helping users to solve their problems.
Application software is broken in to two classes.
i. General - Purpose Software ii. Application - Specific Software
i. General Purpose Software: It is purchased from a software developer and can be used for
more than one application. Examples: word processors, database management systems,
computer – aided design systems. They are labeled general purpose because they can solve a
variety of user computing problems.
ii. Application Specific Software: It can be used only for its intended purpose. Example: A
general ledger system used by accountants. They can be used only for the task for which
they were designed.They cannot be used for other generalized tasks.

Basic components of computer:

Input Unit:
 The input unit contains the devices required to input data into a computer system.
 The devices used to input data into the computer are called input devices.
 Computers do not react or operate on their own, the input unit is one of the main
components of the computer.
 This unit establishes a link between the user and the computer so that the user commands
and data into the computer.
 These devices take data from the users and send it to the computer in a systematic
manner for further processing. Like other electronic machines, the computer system also
accepts only raw data(binary data). Input devices convert the data inputted by the user into
binary form for the computer to understand. Data can be entered or given in the form of text,
alphabets, numbers, symbols, images etc.

The following are some of the commonly used input devices to perform the activities as an input
unit:

Mouse:

 The mouse is one of the primary input devices that acts as a point-and-click-device.
 It enables users to move the cursor and select specific objects on the computers screen or
monitor.
11

 The traditional mouse usually comes with three buttons, one on the left, one on the right
and one on the scrolling wheel.
 It can be connected to the computer both by wire and wireless.

Keyboard:

 The keyboard is another essential input device that consists of several keys.
 By pressing the keys, we can enter or input data like letters, numbers, signs and other
information on the computer. Like a mouse, it can also be wired and wireless.

Microphone:

 Microphone, short for mic, is an optional input device used on computers.


 When we need to insert audio into our presentation, do voice conferencing, recording,
broadcasting or giving voice commands to the computer, etc., we can connect a mic to
the computer.
 Microphones come in a variety of shapes and sizes.
 It enables us to give voice input to the computer system.
 The mics are designed to read sounds from the surroundings and convert this
sound(analog waves) into electrical signals.
 The signals are further converted into digital forms to be understood by the computer.
 Other examples of input devices include a webcam, scanner, touchscreen, barcode reader,
etc.

Central Processing Unit:

 The CPU is an essential electronic hardware component that controls and processes all
functions in a computer system, including arithmetic and logical operations.
 All simple to complex calculations, tasks, or comparisons are performed by the CPU.
 It takes raw data from the primary memory, processes it or converts it into meaningful
Information desired by the users and then sends the processed data back to the primary
memory.
 The CPU is called the brain of the computer because it controls the operation of all the
parts of the computer.
 Despite this, the computer’s processor(CPU) also handles the operation of many others
units within the computer system, such as the control unit, arithmetic logic unit and
memory unit.
 The Control Unit(CU) and the Arithmetic Logic Unit(ALU) are commonly called
components of the CPU because they both together make up the CPU.
12

Output Unit:

 The output unit contains the devices needed to receive and view information from a
computer system.
 The devices used to receive information or results from the computer are called output
devices.
 This unit is an essential component of a computer because without it we cannot obtain or
see information or desired results. Like the input unit, the output unit also establishes a
link between the user and the computer.
 As we discussed above, the results obtained after processing are stored in the primary
memory before sending to other units.
 The output unit retrieves processed data from the computer’s primary memory(main
memory), which is converted into a human-understandable form before being displayed
by the corresponding output device.

The following are some of the commonly used output devices to perform the activities of an
output unit:
Monitor:
 A monitor, often called a visual display unit, is one of the primary output devices used on
computers these days.
 It is connected to a computer to digitally view the output(such as text, images, videos) on
the screen. The output displayed on the computer’s monitor is called soft copy output.
 The monitor is typically made of glass, circuitry, adjustment buttons, power supply, etc
all enclosed within a casing.

Printer:A printer is another essential output device used with computer systems these days.
 Unlike a monitor, a printer helps us to make a hard copy of the desired output such as a
document, picture or other information on paper.
 The printer takes electronic data from the computer and creates an exact copy of it in
monochrome(black and white) or colors, depending on the type of printer.
Speakers:

 Speakers are used to receive audio/sound output from the computer system. They are
optional and they can be connected to the computer only when we need to listen to
something like music, movies, dialogues etc.
13

 Other examples of output devices include a projector, plotter, headphones, touchscreen


etc.

Computing Environment:

When we want to solve a problem using a computer, the computer makes use of various devices
which work together to solve that problem. There may be a various number of ways to solve a
problem. We use the various number of computer devices arranged in different ways to solve
different problems. The arrangement of computer devices to solve a problem is said to be a
computing environment. The formal definition of the computing environment is as follows.

Def: “Computing Environment is a collection of computers which are used to process and
exchange information to solve various types of computing problems.”

Types of Computing Environments


The following are the various types of computing environments.

1. Personal Computing Environment


2. Time-Sharing Computing Environment
3. Client-Server Computing Environment
4. Distributed Computing Environment
5. Grid Computing Environment
6. Cluster Computing Environment

Personal Computing Environment


Personal computing is a stand-alone machine. In a personal computing environment, the
complete program resides on the stand-alone machine and executed from the same machine.
Laptops, mobile devices, printers, scanners and the computer systems we use at home, office are
the examples for the personal computing environment.
14

Time-Sharing Computing Environment


The time-sharing computing environment is a stand-alone computer in which a single user can
perform multiple operations at a time by using a multitasking operating system. Here the
processor time is divided among different tasks and this is called “Time-sharing”. For example, a
user can listen to music while writing something in a text editor. Windows 95 and later versions
of Windows OS, iOS and Linux operating systems are the examples for this computing
environment.

Client-Server Computing Environment


The client-server environment contains two machines (Client machine and Server machine).
These both machines will exchange the information through an application. Here Client is a
normal computer like PC, Tablet, Mobile, etc., and Server is a powerful computer which stores
huge data and manages the huge amount of file and emails, etc., In this environment, client
requests for data and server provides data to the client. In the client-server environment, the
communication between client and server is performed using HTTP (Hyper Text Transfer
Protocol).
15

Distributed Computing Environment


In the distributed computing environment, the complete functionality of the software is not on a
single computer but is distributed among multiple computers. Here we use a method of computer
processing in which different programs of an application run simultaneously on two or more
computers. These computers communicate with each other over a network to perform the
complete task. In a distributed computing environment, the data is distributed among different
systems and that data is logically related to each other.

Grid Computing Environment

Grid computing is a collection of computers from different locations. All these computers work
for a common problem. A grid can be described as a distributed collection of a large number of
computers working for a single application.

Cluster Computing Environment


16

Cluster computing is a collection of interconnected computers. These computers work together


to solve a single problem. In a cluster computing environment, a collection of systems work
together as a single system.

Computer Language:

 Generally, we use languages like English, Hindi, etc., to make communication between
two persons. That means when we want to make communication between two persons we
need a language through which persons can express their feelings.
 Similarly, when we want to make communication between user and computer or between
two or more computers we need a language through which user can give information to
the computer and vice versa.
 When a user wants to give any instruction to the computer the user needs a specific
language and that language is known as a computer language.
The user interacts with the computer using programs and that programs are created using
computer programming languages like C, C++, Java, etc.,
Computer languages are the languages through which the user can communicate with the
computer by writing program instructions.
Every computer programming language contains a set of predefined words and a set of rules
(syntax) that are used to create instructions of a program.
Computer Languages Classification
Over the years, computer languages have been evolved from Low-Level to High-Level
Languages. In the earliest days of computers, only Binary Language was used to write programs.
The computer languages are classified as follows...
17

Low-Level Language (Machine Language)

 Low-Level language is the only language which can be understood by the


computer. Binary Language is an example of a low-level language. Low-level language
is also known as Machine Language.
 The binary language contains only two symbols 1 & 0. All the instructions of binary
language are written in the form of binary numbers 1's & 0's.
 A computer can directly understand the binary language. Machine language is also
known as the Machine Code.
 As the CPU directly understands the binary language instructions, it does not require any
translator.
 CPU directly starts executing the binary language instructions and takes very less time to
execute the instructions as it does not require any translation.
 Low-level language is considered as the First Generation Language (1GL).

Advantages

 A computer can easily understand the low-level language.


 Low-level language instructions are executed directly without any translation.
 Low-level language instructions require very less time for their execution.

Disadvantages

 Low-level language instructions are very difficult to use and understand.


18

 Low-level language instructions are machine-dependent, that means a program written


for a particular machine does not execute on another machine.
 In low-level language, there is more chance for errors and it is very difficult to find
errors, debug and modify.

Middle-Level Language (Assembly Language)

 Middle-level language is a computer language in which the instructions are created using
symbols such as letters, digits and special characters.
 Assembly language is an example of middle-level language. In assembly language, we
use predefined words called mnemonics.
 Binary code instructions in low-level language are replaced with mnemonics and
operands in middle-level language. But the computer cannot understand mnemonics, so
we use a translator called Assembler to translate mnemonics into binary language.
 Assembler is a translator which takes assembly code as input and produces machine code
as output.
 That means, the computer cannot understand middle-level language, so it needs to be
translated into a low-level language to make it understandable by the computer.
 Assembler is used to translate middle-level language into low-level language.

Advantages
 Writing instructions in a middle-level language is easier than writing instructions in a
low-level language.
 Middle-level language is more readable compared to low-level language.
 Easy to understand, find errors and modify.
Disadvantages
 Middle-level language is specific to a particular machine architecture, that means it is
machine-dependent.
 Middle-level language needs to be translated into low-level language.
 Middle-level language executes slower compared to low-level language.

High-Level Language
 A high-level language is a computer language which can be understood by the users. The
high-level language is very similar to human languages and has a set of grammar rules
that are used to make instructions more easily.
19

 Every high-level language has a set of predefined words known as Keywords and a set of
rules known as Syntax to create instructions.
 The high-level language is easier to understand for the users but the computer can not
understand it.
 High-level language needs to be converted into the low-level language to make it
understandable by the computer. We use Compiler or interpreter to convert high-level
language to low-level language.
 Languages like COBOL, FORTRAN, BASIC, C, C++, JAVA, etc., are examples of high-
level languages.
 All these programming languages use human-understandable language like English to
write program instructions. These instructions are converted to low-level language by the
compiler so that it can be understood by the computer.

Advantages
 Writing instructions in a high-level language is easier.
 A high-level language is more readable and understandable.
 The programs created using high-level language runs on different machines with little
change or no change.
 Easy to understand, create programs, find errors and modify.

Disadvantages
 High-level language needs to be translated into low-level language.
 High-level language executes slower compared to middle and low-level languages.
Understanding Computer Languages
The following figure provides a few key points related to computer languages.
20

Language Translators
These are the programs which are used for converting the programs in one language into
machine language instructions, so that they can be excuted by the computer.
Language Translators
These are the programs which are used for converting the programs in one language into
machine language instructions, so that they can be excuted by the computer.

Language Translators

These are the programs which are used for converting the programs in one language into
machine language instructions, so that they can be executed by the computer.
1)Compiler: It is a program which is used to convert the high level language programs into
machine language.
2) Assembler: It is a program which is used to convert the assembly level language programs
into machine language
3) Interpreter: It is a program, it takes one statement of a high level language program,
translates it into machine language instruction and then immediately executes the resulting
machine language instruction and so on.

Comparison between a Compiler and Interpreter


21

Comparison between a Compiler and Interpreter

Compiler Interpreter
A Compiler is used to compile an entire An interpreter is used to translate each line of
program and an executable program is the program code immediately as it is entered
generated through the object program

The executable program is stored in a disk The executable program is generated in


for future use or to run it in another computer RAM and the interpreter is required for each
run of
the program

The compiled programs run faster The Interpreted programs run slower
Most of the Languages use compiler A very few languages use interpreters.

CREATING AND RUNNING PROGRAMS

Computer hardware understands a program only if it is coded in its machine language. It is the
job of the programmer to write and test the program .There are four steps in this process:

1. Writing and Editing the program

2. Compiling the program

3. Linking the program with the required library modules

4. Executing the program


22

Writing and Editing Programs

The software used to write programs is known as a text editor. A text editor
helps us enter, change and store character data. Once we write the program in the text editor we
save it using

Compiling Programs

The code in a source file stored on the disk must be translated into machine
language. This is the job of the compiler. The Compiler is a computer program that translates the
source code written in a high-level language into the corresponding object code of the
low-level language.This translation process is called compilation. The entire high level
program is converted into the executable machine code file. The Compiler which executes C
programs is called as C Compiler. Example Turbo C, Borland C, GC etc.,

The C Compiler is actually two separate programs:

 The Preprocessor
 The Translator

The Preprocessor reads the source code and prepares it for the translator. While preparing the
code, it scans for special instructions known as preprocessor commands. These commands tell
the preprocessor to look for special code libraries. The result of preprocessing is called
the translation unit.
After the preprocessor has prepared the code for compilation, the translator does the
actual work of converting the program into machine language. The translator reads the
translation unit and writes the resulting object module to a file that can then be combined with
other precompiled units to form the final program. An object module is the code in the machine
language.

Linking Programs

The Linker assembles all functions, the program‘s functions and system‘s functions
into one executable program.
Executing Programs
To execute a program we use an operating system command, such as run,
to load the program into primary memory and execute it. Getting the program into memory is
the function of an operating system program known as the loader. It locates the
executable program andreads it into memory. When everything is loaded the program
takes control and it begin execution.
23

Algorithms
An algorithm is basically a procedure of steps that we exactly follow to solve a particular task or
problem. We can say that it is a set of rules that we need to follow while developing a program code
during problem-solving.

Features of an algorithm
The features of an algorithm are as follows:

 Clarity
The algorithm should be very clear and unambiguous in its meaning. It should be
simple so that one can understand it easily.
 Well-defined inputs
A program may require to take input from the user. Therefore, the algorithm
should clearly define the inputs.
 Well-defined outputs
The algorithm should clearly specify the output that the program will produce.
 Finiteness
The algorithm should have a termination point. This means that the algorithm
should not be such that it runs infinite times or end up in loops.
 Feasibility
The algorithm should be such that we can implement it easily. Hence, it should be
simple and practical to implement.
 Language independent
It should be in simple English language. Since the code implementation should
result in the same output no matter which programming language we use while writing the code.

 What are the main steps in problem-solving?

A1. A successful problem-solving process depends on the following factors:

 understanding the problem.


 designing proper algorithms and flowcharts.
 implementing the algorithm.
24

 What is problem-solving?
A2. We can define problem-solving as a process of understanding the problem, finding solution for
the problem, and finally implementing the solution to it.
Example of Algorithm
Problem 1: Find the area of a Circle of radius r.
Inputs to the algorithm:
Radius ‘r’ of the Circle.
Expected output:
Area of the Circle

Algorithm:
Step1: Read\input the Radius r of the Circle
Step2: Area PI*r*r // calculation of area
Step3: Print Area

Problem2: Write an algorithm to read two numbers and find their sum.
Inputs to the algorithm:
First num1.
Second num2.
Expected output:
Sum of the two numbers.
Algorithm:
Step1: Start
Step2: Read\input the first num1.
Step3: Read\input the second num2.
Step4: Sum num1+num2 // calculation of sum
Step5: Print Sum
Step6: End
25

Flowcharts
It is basically a diagrammatic representation of an algorithm. Furthermore, it uses various symbols
and arrows to describe the beginning, ending, and flow of the program. Moreover, the programmers
use it to depicting the flow of data and instructions while problem-solving.

Flowchart Symbols

Here is a chart for some of the common symbols used in drawing flowcharts.

Symbol Symbol Name Purpose

Used at the beginning and end of the


Start/Stop algorithm to show start and end of the
program.

Indicates processes like mathematical


Process
operations.

Used for denoting program inputs and


Input/ Output
outputs.

Stands for decision statements in a


Decision program, where answer is usually Yes or
No.
26

Shows relationships between different


Arrow
shapes.

Connects two or more parts of a


On-page Connector
flowchart, which are on the same page.

Connects two parts of a flowchart which


Off-page Connector
are spread over different pages.

Guidelines for Developing Flowcharts

These are some points to keep in mind while developing a flowchart −


 Flowchart can have only one start and one stop symbol
 On-page connectors are referenced using numbers
 Off-page connectors are referenced using alphabets
 General flow of processes is top to bottom or left to right
 Arrows should not cross each other

Flowchart to calculate the average of two numbers.


27

Find the area of a circle of radius r.

Flowchart for an algorithm which gets two numbers and prints sum of their value
28

Part-2

Introduction to C language
C is a general-purpose high level language that was originally developed by Dennis
Ritchie for the Unix operating system. It was first implemented on the Digital
Equipment Corporation PDP-11 computer in 1972.

The Unix operating system and virtually all Unix applications are written in the C language.

C has now become a widely used professional language for various reasons.
 Easy tolearn
 Structuredlanguage
 It produces efficientprograms.
 It can handle low-levelactivities.
 It can be compiled on a variety ofcomputers.
Facts about C
 C was invented to write an operating system called UNIX.
 C is a successor of B language which was introduced around1970
 The language was formalized in 1988 by the American National
Standard Institute (ANSI).
 By 1973 UNIX OS almost totally written inC.
 Today C is the most widely used System Programming Language.
Why to use C?
C was initially used for system development work, in particular the programs that make-
up the operating system. C was adopted as a system development language because it
produces code that runs nearly as fast as code written in assembly language. Some
examples of the use of C might be:

 Operating Systems ,Language Compilers , Assemblers


 Text Editors , PrintSpoolers , NetworkDrivers , ModernPrograms
 DataBases , Language Interpreters , Utilities
29

History of C language:

The founder of the ‘C’ language, Dennis Ritchie is known as “Father of C” and also “Father
of UNIX”.

The following are the language before ‘c’ & various versions of ‘c’.
1. CPL (Common Programming Language)
The CPL was invented by Martin Richards at the University of Cambridge in the early of
1960s.

2. BCPL (Basic Combined Programming Language)


The BCPL was invented by Martin Richards at the University of Cambridge in the year of
1966. It was a popular programming language at that time. BCPL allows the user, direct access
to the computer memory. BCPL is the extension of CPL.

3. B Language
B language is derived from BCPL. It was introduced in the year of 1969 by Ken
Thompson and Dennis Ritchie at Bell Laboratory, USA. The B language is similar to BCPL.

4. C Language
C language is derived from the B language. It was introduced in the year of 1972 by Dennis
Ritchie at Bell Laboratory, USA. The C language was mainly developed to create an operating
system called UNIX. The name C is given based on the previous language B and BCPL. Ninety
percent of the UNIX operating system code is written in C language. During the 1970s, the C
language became a very popular programming language. Many universities and organizations
began creating their version of C language for their respective projects. So, C language has got
many variants at that time. Later it was standardized.

5. ANSI C (C89)
In the year of 1983, the ANSI (American National Standards Institute) formed a committee to
frame standard specifications for the C language. In the year of 1989, this committee introduced
a standard version of C with the name "ANSI C" with standard library files. The ANSI C is also
called as C89 in short form.
30

6. C90
In the year of 1990, the ANSI C was got ISO (International Organization for Standardization)
standardization with the inclusion of a few new features like new library files, new processor
commands. And it was also added with keywords const, volatile and signed, etc... ISO
standardized ANSI C as ISO/IEC 9899:1990. This version is called as C90 in short form.

7. C99
In the year of 1995, many new features were added to the C90 to create a new version of it. This
new version of C was got ISO standardization in the year of 1999 with the name ISO/IEC
9899:1999. In the short form, it is called as C99. Later C99 became the official standard version
of C.

BASIC STRUCTURE OF C PROGRAMMING

1. Documentation section: The documentation section consists of a set of comment


lines giving the name of the program, the author and other details, which the
programmer would like to uselater.
2. Link section: The link section provides instructions to the compiler to link
functions from the system library such as using the #includedirective.
3. Definition section: The definition section defines all symbolic constants such
31

using the #definedirective.


4. Global declaration section: There are some variables that are used in more than
one function. Such variables are called global variables and are declared in the global
declaration section that is outside of all the functions. This section also declares all
the user-definedfunctions.
5. main () function section: Every C program must have one main function section.
This section contains two parts; declaration part and executable part
1. Declaration part: The declaration part declares all the variables used in the
executable part.
2. Executable part: There is at least one statement in the executable part. These
two parts must appear between the opening and closing braces. The program execution
begins at the opening brace and ends at the closing brace. The closing brace of the main
function is the logical end of the program. All statements in the declaration and
executable part end with a semicolon.
6. Subprogram section: If the program is a multi-function program then the
subprogram section contains all the user-defined functions that are called in the main ()
function. User- defined functions are generally placed immediately after the main ()
function, although they may appear in any order.

The C Compilation Model

The Preprocessor
The Preprocessor accepts source code as input and is responsible for
 Removing comments
 Interpreting special preprocessor directives denoted by #.
Forexample
 #include -- includes contents of a named file. Files usually called header
files.e.g
o #include <math.h> -- standard library mathsfile.
o #include <stdio.h> -- standard library I/Ofile
 #define -- defines a symbolic name or constant. Macrosubstitution.
o #define MAX_ARRAY_SIZE100
32

C Compiler
The C compiler translates source to assembly code. The source code is received from the
preprocessor.

Assembler
The assembler creates object code. On a UNIX system you may see files with a .o
suffix (.OBJ on MSDOS) to indicate object code files.
Link Editor
If a source file references library functions or functions defined in other source files the
link editor combines these functions (with main()) to create an executable file.

General rules for any C program

1. Every executable statement must end with a semicolon symbol (;).


2. Every C program must contain exactly one main method (Starting point of the program
execution).
3. All the system-defined words (keywords) must be used in lowercase letters.
4. Keywords can not be used as user-defined names(identifiers).
5. For every open brace ({), there must be respective closing brace (}).
6. Every variable must be declared before it is used.

C TOKENS

C tokens are the basic buildings blocks in C language which are constructed together to write a C
program.
Each and every smallest individual unit in a C program is known as C tokens.
C tokens are of six types. They are
Keywords (eg: int,while),
Identifiers (eg: main,total),
Constants (eg: 10,20),
Strings (eg: ―total‖,―hello‖),

Special symbols (eg: (), {}),


Operators (eg: +, /,-,*)
33

C KEYWORDS

C keywords are the words that convey a special meaning to the c compiler. The keywords
cannot be used as variable names.
The list of C keywords is given below:

auto break case char const

continue default do double else

enum extern float for goto

if int long register return

short signed sizeof static struct

switch typedef union unsigned void

volatile while

C IDENTIFIERS

Identifiers are used as the general terminology for the names of variables, functions and
arrays. These are user defined names consisting of arbitrarily long sequence of letters and
digits with either a letter or the underscore(_) as a first character.
There are certain rules that should be followed while naming c identifiers:
 They must begin with a letter or underscore (_).
 They must consist of only letters, digits, or underscore. No other special character is
allowed. It should not be a keyword.
 It must not contain whitespace.
 It should be up to 31 characters long as only first 31 characters are significant.

Some examples of c identifiers:


Name Remark
_A9 Valid
Temp.var Invalid as it contains special character other than the underscore
void Invalid as it is a keyword
C CONSTANTS
34

A C constant refers to the data items that do not change their value during the program
execution. Several types of C constants that are allowed in C are:
Integer Constants

Integer constants are whole numbers without any fractional part. It must have at least one digit
and may contain either + or – sign. A number with no sign is assumed to be positive.
There are three types of integer constants:
Decimal Integer Constants

Integer constants consisting of a set of digits, 0 through 9, preceded by an optional – or +


sign.

Example of valid decimal integer constants: 341, -341, 0,8972

Octal Integer Constants

Integer constants consisting of sequence of digits from the set 0 through 7 starting with 0 is
said to be octal integer constants.

Example of valid octal integer constants : 010, 0424, 0,0540


Hexadecimal Integer Constants

Hexadecimal integer constants are integer constants having sequence of digits preceded by
0x or 0X. They may also include alphabets from A to F representing numbers 10 to 15.
Example of valid hexadecimal integer constants: 0xD, 0X8d, 0X, 0xbD
It should be noted that, octal and hexadecimal integer constants are rarely used in programming.
Real Constants

The numbers having fractional parts are called real or floating point constants. These may
be represented in one of the two forms called fractional form or the exponent form and may
also have either + or – sign preceding it.
Example of valid real constants in fractional form or decimal notation :
0.05, -0.905, 562.05, 0.015
Representing a real constant in exponent form

The general format in which a real number may be represented in exponential or


scientific form is
35

mantissa e exponent

The mantissa must be either an integer or a real number expressed in decimal notation.
The letter e separating the mantissa and the exponent can also be written in uppercase
i.e. E a nd, the exponent must be an integer.
Examples of valid real constants in exponent form are: 252E85, 0.15E-10,-3e+8
Character Constants

A character constant contains one single character enclosed within single quotes.
Examples of valid character constants: ‘a‘ ,’Z‘,’5‘

It should be noted that character constants have numerical values known as ASCII
values, for example, the value of ‘A‘ is 65 which is its ASCII value.
String Constants: A string constant is a sequence of characters enclosed in double quote, the
characters may be letters, numbers, special characters and blank space etc .

EX : “rama” , “a” , “+123”


Escape Characters/ Escape Sequences

C allows us to have certain non graphic characters in character constants. Non graphic
characters are those characters that cannot be typed directly from keyboard, for example, tabs,
carriage return, etc.
These non graphic characters can be represented by using escape sequences represented by a
backslash() followed by one or more characters.
NOTE: An escape sequence consumes only one byte of space as it represents a single character.

Escape Sequence Description


a Audible alert(bell)
b Backspace
f Form feed
n New line
r Carriage return
t Horizontal tab
v Vertical tab
\ Backslash
36

― Double quotation mark


‗ Single quotation mark
? Question mark
Null

Special Symbols

The following special symbols are used in C having some special meaning and thus, cannot
be used for some other purpose.
[] () {} , ; : * … = #
Braces{}: These opening and ending curly braces marks the start and end of a block of
code containing more than one executable statement.
Parentheses(): These special symbols are used to indicate function calls and function
parameters.
Brackets[]: Opening and closing brackets are used as array element reference. These
indicate single and multidimensional subscripts.

VARIABLES

 A variable is nothing but a name given to a storage area that our programs can
manipulate.
 Each variable in C has a specific type, which determines the size and layout of
the variable's memory;
 The range of values that can be stored within that memory; and the set of
operations that can be applied to the variable.
 The name of a variable can be composed of letters, digits, and the underscore
character.
 It must begin with either a letter or an underscore.
 Upper and lowercase letters are distinct because C is case-sensitive.
 The following basic variable types −

Type Description

char Typically a single octet(one byte). This is an integer type.


37

int The most natural size of integer for the machine.

float A single-precision floating point value.

double A double-precision floating point value.

void Represents the absence of type.

C programming language also allows defining various other types of variables like
Enumeration, Pointer, Array, Structure, Union, etc.

Variable Definition in C
A variable definition tells the compiler where and how much storage to create
for the variable. A variable definition specifies a data type and contains a list of one or
more variables of that type as follows−

type variable_list;
Here, type must be a valid C data type including char, w_char, int, float, double,
bool, or any user-defined object; and variable_list may consist of one or more identifier
names separated by commas. Some valid declarations are shown here −

int i, j, k;
char c, ch;
float f,
salary;
double d;

The line int i, j, k; declares and defines the variables i, j, and k; which instruct the
compiler to create variables named i, j and k of type int.
Variables can be initialized (assigned an initial value) in their declaration. The
initializer consists of an equal sign followed by a constant expression as follows−

type variable_name = value;


Some examples are −
38

extern int d = 3, f=5; // declaration of d and f.


int d = 3, f=5; // definition and initializing d and f.
char x= 'x'; // the variable x has the value'x'.

For definition without an initializer: variables with static storage duration are implicitly
initialized with NULL (all bytes have the value 0); the initial value of all other variables
are undefined.
Variable Declaration in C
 A variable declaration provides assurance to the compiler that there exists a
variable with the given type and name so that the compiler can proceed for
further compilation without requiring the complete detail about the variable.

 A variable definition has its meaning at the time of compilation only; the
compiler needs actual variable definition at the time of linking the program. A
variable declaration is useful when multiple files are used.
Data types

 Data types in the c programming language are used to specify what kind of value can be
stored in a variable.
 The memory size and type of the value of a variable are determined by the variable data
type.
 In a c program, each variable or constant or array must have a data type and this data type
specifies how much memory is to be allocated and what type of values are to be stored in
that variable or constant or array.
Data types are classified as follows...

1. Primary data types (int ,char ,float ,double)


2. Derived data types (arrays, pointers, functions)
3. User Defined data type(structure ,union, enumeration)
39

Primary data types


The primary data types in the C programming language are the basic data types. Primary data
types are also called as Built-In data types. The following are the primary data types in c
programming language...

1. Integer data type


2. Floating Point data type
3. Double data type
4. Character data type
40

Integer Data Types

Integers are whole numbers without a fractional part. In C, the keyword int is used to represent
integer quantity. They generally require 2 bytes of memory for their storage. It is a signed data
type, i.e. it can be used to store both positive as well as negative values. The range of values that
variables of this type can hold lies between -32768 to +32767.

Type Size(bytes) Range Specifier


int or signed int 2 -32,768 to 32767 %d
unsigned int 2 0 to 65535 %d
short int or signed 1 -128 to 127 %d
short int
unsigned short int 1 0 to 255 %u
long int or signed 4 -2,147,483,648 to 2,147,483,647 %u
long int

Floating Point data types


Floating-point data types are a set of numbers with the decimal value. Every floating-point value
must contain the decimal value. The floating-point data type has two variants...

 float
 double

The keyword "float" is used to represent floating-point data type and "double" to represent
double data type in c. Both float and double are similar but they differ in the number of decimal
places. The float value contains 6 decimal places whereas double value contains 15 or 19
decimal places.

Type Size(bytes) Range Specifier


float 4 1.2E-38 to 3.4E+38 %f
double 8 2.3E-308 to 1.7E+308 %ld
long double 10 3.4E-4932 to 1.1E+4932 %ld

Character data type


The character data type is a set of characters enclosed in single quotations.
Type Size(bytes) Range Specifier

Char(signed char) 1 -128 to 127 %c


Unsigned char 1 0 to 255 %c
41

void data type


The void data type means nothing or no value. Generally, the void is used to specify a function
which does not return any value. We also use the void data type to specify empty parameters of a
function.

Enumerated data type


An enumerated data type is a user-defined data type that consists of integer constants and each
integer constant is given a name. The keyword "enum" is used to define the enumerated data
type.
Input / Output (I/O) Functions : In C language, two types of Input/Output functions are available,
and all input and output operations are carried out through function calls. Several functions are
available for input / output operations in „C‟. These functions are collectively known as the standard
i/o library.
Input: In any programming language input means to feed some data into program. This can be given
in the form of file or from command line.
Output: In any programming language output means to display some data on screen, printer or in
any file.

C programming treats all the devices as files. So devices such as the display are addressed in the
same way as files and the following three files are automatically opened when a program executes to
provide access to the keyboard and screen.
Standard file File pointer Device
Standard input Stdin Keyboard
Standard output Stdout Screen
Standard error Stderr Your screen
Input / Output functions are classified into two types
42

Formated I/O Functions : Formatted I/O functions operates on various types of data.
printf() : output data or result of an operation can be displayed from the computer to a standard
output device using the library function printf(). This function is used to print any combination of
data.
Ex : printf(“%d”,3977);
printf() statement another syntax :
Syntax : printf(“fomating string”);
Formating string : It prints all the character given in doublequotes (“ “) except formatting specifier.
Ex:
printf(“ hello “);-> hello
printf(“a”); -> a
scanf() : input data can be entered into the computer using the standard input „C‟ library function
called scanf(). This function is used to enter any combination of input.
Syntax : scanf(“control string “,&var1, &var2,----, &varn);
The scanf() function is used to read information from the standard input device (keyboard).
Ex : scanf(“ %d “,&a);
Each variable name (argument) must be preceded by an ampersand (&). The (&) symbol gives the
meaning “address of “the variable.
Unformatted I/O functions:
a) Character I/O b) String I/O

a) character I/O:

1. getchar(): Used to read a character from the standard input


2. putchar(): Used to display a character to standard output
3. getch() and getche(): these are used to take the any alpha numeric characters from the standard
input
 getche() read and display the character
 getch() only read the single character but not display
4. putch(): Used to display any alpha numeric characters to standard output

b)String I/O:
1. gets(): Used for accepting any string from the standard input(stdin)
2. puts(): Used to display a string or character array Eg:puts()
3. cgets():read a string from the console eg; cgets(char *st)
4. cputs():display the string to the console eg; cputs(char *st)
43

Ex: Pgm using getchar and putchar:


#include <stdio.h>
int main( )
{
int c;
printf( "Enter a value :");
c = getchar( );
printf( "\nYou entered: ");
putchar( c );
return 0;
}
Ex: pgm using gets and puts
#include <stdio.h>
int main( )
{
char str[100];
printf( "Enter a value :");
gets( str );
printf( "\nYou entered: ");
puts( str );
return 0;
}

You might also like