Vous êtes sur la page 1sur 22

Chapter One

Introductory Concepts

This book offers instruction in computer programming using a popular, structured programming language
called C. We will learn how programs can be written in C. In addition, we will see how problems that are
initially described in general terms can be analyzed, outlined and finally transformed into well-organized C
programs. These concepts are demonstrated in detail using many sample problems in this text.

1.1 INTRODUCTION TO COMPUTERS

Computers are present in almost all areas of life, such as in homes and home appliances, vehicles, mobiles,
offices, shopping malls, business showrooms, hospitals, banks, etc. Hence, there will be no exaggeration in
saying that “computers are everywhere”. It is common knowledge that a computer is an electronic device
which processes the data, i.e., it transforms the user inputs to desired outputs based on pre-defined instructions.
These pre-defined instructions are normally stored in a computed in the form of software. Hence, a computer
is a collection of hardware and software.
Today’s computers are significantly different from those initially developed. The first computer was devel-
oped in the 1940s and was bigger than a normal room. This model consumed an enormous amount of power
and dissipated so much heat that its continuous use was restricted to a few hours only. In spite of its huge size
and power requirements, this computer’s computing power was much less than today’s scientific calculators.
Today’s personal computers are smaller in size (desktops, laptops, palmtops, etc.) and at the same time are
much more efficient in terms of speed, storage, power-consumption and above all, are very easy to afford.
Figure 1 shows the two most common types of personal computers: desktop and laptop.
Besides these personal computers, there also exist more powerful computers like servers and super-com-
puters, whose storage capacity, processing power as well as speed are extremely high and such computers are
used for high-end applications, such as weather forecasting, space and nuclear research, etc.
Most of us are well aware and use Internet on our personal computers or mobile phones. It is impossible
to imagine life without the Internet today. Another common use of personal computers is in the form of of-
fice networks in many organizations, where several computers are connected with each other via a wired or
wireless network, and is supported by more powerful servers, etc.

Prog-C_01.indd 1 7/10/2018 2:01:06 PM


1.2 Programming with C

(a) (b)

  Fig. 1.1    (a) PC in form of a Desktop (b) PC in form of a laptop

1.2 What is a Computer

A digital computer is an electronic programmable machine that can process almost all kinds of data. The
programmable feature has made this machine unique as compared to other machines. The same machine can
be used in a small shop, in medical science, for space-research and various engineering activities. All this is
possible by using different programs for different environments/applications.
What is a program? A program is a set of instructions, written in a particular sequence in a computer-related
language. Details about programs will be discussed in Sections 1.4 and 1.6.

1.3 Block Diagram of A Computer

A computer has four main components (Fig. 1.2):


(i) Input device
(ii) Memory
(iii) Central Processing Unit (CPU)
(iv) Output device
The input devices (e.g., keyboard, mouse) are used to
input data to a computer. It converts the input informa-
tion into a form which is usable by a computer. Whatever
input is supplied by the input device(s), first goes to the
memory. The CPU acts as a computer’s brain. The CPU
is responsible for the overall working of all components
  Fig. 1.2    Block diagram of computer
of a computer. It consists of two parts: Arithmetic–Logic
Unit (ALU) and Control Unit (CU). The ALU performs
arithmetic operations and conducts the comparison of information for logical decisions. The Control Unit

Prog-C_01.indd 2 7/10/2018 2:01:07 PM


Introductory Concepts 1.3

is responsible for sending/receiving control signals from and to all components. The dotted lines in Fig. 1.2
represent the communication of control signals, whereas the solid lines denote the transfer of data. Under the
control of CU, the data comes from input device(s) to memory, it is processed in ALU and the result is stored
back in the memory. The processed results are converted to a form that can be understood easily by human
beings and is displayed with the help of an output device (e.g., monitor, printer).
The memory of a computer is of two types: primary memory and secondary memory. Primary memory is
faster in speed, less in size (normally few megabytes) and costlier. It consists of ROM (Read Only Memory)
and RAM (Random Access Memory). ROM is a very small amount of memory used to make the computer
ready for work (this process is called booting). RAM contains all types of intermediate and temporary data to
be used by the CPU. In fact, the CPU can work/process only that data which is present in the RAM. Any data
present in the secondary memory (e.g., hard disk, floppy, CD) needs to be first brought to RAM and only then
can it be used by the CPU. Secondary memory is usually a very large amount of memory (in gigabytes), which
is comparatively cheaper and slower than primary memory, but is permanent in nature. Thus, anything stored
in secondary memory remains available even if the computer is switched off. On the other hand, the contents
of RAM (not ROM, which is also permanent) are lost as soon as the computer is turned off because RAM is
volatile in nature.

1.4 COMPUTER CHARACTERISTICS


All digital computers, regardless of their size, are used to transmit, store, and manipulate information (i.e.,
data). Several types of data can be processed by a computer. These include numeric data, character data
(names, addresses, etc.), graphic data (charts, drawings, photographs, etc.), and sound (music, speech pat-
terns, etc.). The two most common types, for new programmer, are numeric data and character data. Scientific
and technical applications are concerned primarily with numeric data, whereas business applications usually
require processing of both numeric and character data.
The foregoing discussion illustrates two important characteristics of a digital computer: memory and capa-
bility to be programmed. A third important characteristic is its speed and reliability. We will discuss memory,
speed and reliability in the next few paragraphs. Programmability will be discussed at length throughout the
remainder of this book.

1.4.1  Memory
Every piece of information stored within a computer’s memory is encoded as some unique combination of
zeros and ones. These zeros and ones are called bits (binary digits). Each bit is represented by an electronic
device that is, in some sense, either “off” (zero) or “on” (one).
Small computers have memories that are organized into 8-bit multiples called bytes, as illustrated in
Fig. 1.3. Notice that the individual bits are numbered, beginning with 0 (for the rightmost bit) and extending
to 7 (the leftmost bit). Normally, a single character (e.g., a letter, a single digit or a punctuation symbol) will
occupy one byte of memory. An instruction may occupy 1, 2 or 3
bytes. A single numeric quantity may occupy 2 to 8 bytes, depending
on its precision (i.e., the number of significant figures) and its type bit number 7 6 5 4 3 2 1 0
(integer, floating-point, etc.).
A computer typically consists of many different types of memories
One byte
like RAM, ROM, cache, hard disk, etc. All such memories can be
classified into two categories:   Fig. 1.3 
Primary memory
Secondary memory

Prog-C_01.indd 3 7/10/2018 2:01:07 PM


1.4 Programming with C

Primary Memory  Primary memory is a semiconductor memory and is needed for all kinds of processing of
the CPU. In fact, CPU cannot work directly on the other type of memory, i.e., secondary memory. Whatever
data/program is needed by the CPU, it must be present in the primary memory. Hence, the primary memory is
relatively faster, lesser and costly than the secondary memory. There are mainly two types of primary memo-
ries:
Read Only Memory (ROM)
Random Access Memory (RAM)
As the name suggests, ROM is read only. This memory cannot be changed and can be used only by the
CPU. It is needed in the computer to store the Basic Input Output System (BIOS). BIOS is responsible
to make the computer ready for work by its users. This process is called booting and takes few minutes
normally. You can try switching on a computer and observe the messages appearing on the screen. During
that time, you cannot do any work on it. After a few minutes, the login-screen/desktop appears and then you
can resume work, e.g., playing a game, programming, printing, etc. The process starting from switching on
till the computer becomes ready to use, is called booting process. With the help of ROM-BIOS memory,
the booting starts and the necessary programs and data (both are part of Operating System) are loaded from
secondary memory to RAM. This memory is permanent in storage and is very small in size and present in
the form of an IC (Integrated Chip). This Read Only Memory’s presence is essential to start a computer.
The Random Access Memory (RAM) is a volatile memory, i.e., its contents get destroyed as soon as a
computer is switched off. All kinds of processing of the CPU are done in this memory. It means whatever
programs, letters, data, etc. a user types go first to the RAM and whatever output we see on the output screen/
printers, comes through this RAM. Whenever we start any game or any other utility program on a computer,
it gets run (executed) in the primary memory. RAM is a semiconductor memory and is always faster than
magnetic memories. As RAM is volatile, we always need some other memory which is permanent, i.e., where
data can be stored forever and can be loaded inside the RAM whenever needed.
The size of a computer’s memory is usually expressed as multiple of 210 = 1024 bytes. This is referred as
1 K. Modern small computers have primary memories whose sizes typically range from 2 GigaBytes (GB)
to 16 GigaBytes, where 1 GB is equivalent to 210, i.e., 1024 Mega-Bytes (MB), 1 MB is equivalent to 210,
i.e., 1024 KiloBytes (KB) and 1 KB is 1024 bytes. So 1 GB is equivalent to 210 * 210 ¥ 210 bytes.

Secondary Memory  The requirement of permanent storage is fulfilled by the presence of a secondary
memory in the form of a hard disk. Every computer consists of one or more hard disks for permanent stor-
age of all data. These are magnetic memories, quite large in size and quite cheap compared to RAM. In hard
disks, data is stored in the form of sectors, tracks and cylinders, where a sector is the smallest unit of storage
and is of 512 bytes. Every bit is stored with the help of magnetization of the hard disk’s surface. Typical size
of hard disks used in personal computers nowadays is 500 GB to 2 TB (TeraBytes). Here One TB is equal to
1024 GB. The size of the hard disks increases to many Tera Bytes for servers, etc. Due to the magnetization
mechanism, the data remains permanently stored even after switching off the computer. The CPU cannot
work on the secondary memory directly, so all the needed data and programs are transferred to RAM for
processing and stored back in the secondary memory after the work is complete. Pen drives, memory cards,
optical disks (DVDs, CDs), i-Pods and other kinds of secondary memories are usually removable in nature,
i.e., can be attached and removed to/from the computer whenever needed. In earlier times, floppy disks and
magnetic tapes were also used as removable storage, but they have become obsolete now.

Prog-C_01.indd 4 7/10/2018 2:01:07 PM


Introductory Concepts 1.5

1.4.2  Speed and Reliability


Because of its extremely high speed, a computer can carry out calculations within minutes that might require
many days, perhaps even months or years, if carried out by hand. For example, the end-of-semester grades for
all students in a large university can typically be processed in just a few minutes on a computer.
The time required to carry out simple computational tasks, such as adding two numbers, is usually
­expressed in terms of microseconds (1 m sec = 10–6 sec) or nanoseconds (1 nsec = 10–3 m sec = 10–9 sec).
Thus, if a computer can add two numbers in 10 nanoseconds (typical of a modern medium-speed computer),
100 million (108) additions will be carried out in one second.
This very high speed is accompanied by an equally high level of reliability. Thus, computers never make mistakes
of their own accord. Highly publicized “computer errors,” such as a person’s receiving a tax refund of several million
dollars, are the result of programming errors or data entry errors rather than errors caused by the computer itself.

1.5 Hardware vs Software

The physical components of a computer are known as hardware. CPU, RAM, hard disk, keyboard, mouse,
monitor, etc. constitute the hardware. In most simple applications of a computer, the hardware remains similar
and it is the software that helps in using the same hardware for different applications. A computer is incapable
of performing any task with the help of hardware alone. It requires a set of instructions to carry out the specific
task. This set of instructions is known as a program and one or more programs along with their documentation
are called software.
Software used on a computer can be classified as:
(i) System software
(ii) Application software
  (i) System software: This category includes softwares that are used to define the functioning of any ­computer
irrespective of the area/application where it is to be used. Operating systems, compilers, editors, etc.
are examples of system software.
(ii) Application software: In order to use a computer for a specific task, it needs to be instructed accord-
ingly. Such instructions are provided by application software. For example, railway ticket reservation
software. The required set of instructions for the railway ticket reservation/cancellation/enquiry written
in a specific language constitutes the railway ticket reservation software. Weather forecasting software,
online admission/examination software, etc. are other common examples of application software.

1.5.1  Operating System


Operating System (OS) is a system software that helps users to operate a computer efficiently. The primary
purpose of the OS is to act as an interface between a computer and the user. As discussed in Sec. 1.2, a
computer is a digital device which understands and works only in binary language (also called machine
language) made of 0’s and 1’s only. It is impossible for a common person to work in binary language. There
are three main responsibilities of an operating system:
1. To provide an interface between the user and the computer hardware
2. To manage the resources of the computer
3. To provide support for storing and executing other software

Prog-C_01.indd 5 7/10/2018 2:01:07 PM


1.6 Programming with C

Through the interface, the user is provided the support to copy, print, type, listen music, see movies, etc.
Earlier, most of these facilities were provided through various commands (e.g., in DOS, etc.). Nowadays,
all operating systems provide these facilities through graphical interface using icons, etc. This is known as
Graphical User Interface (GUI) and it helps users in easier and interactive operation of a computer. Another
important responsibility of the OS is to manage the various resources present in a computer. CPU, all types of
memory, various input and output devices, all are resources to be efficiently controlled and managed by the
operating system. Any kind of additional hardware to be attached will need the operating system’s support for
its correct working. The operating system also helps users in installing and executing other additional software
such as, new games, utility programs, various compilers, different application softwares, etc. In general, it can
be said that the operating system is the overall in-charge of the whole computer.
Since the inception of the operating system, there have been many OS in the market, and most of these can
be classified into three categories: DOS, Windows OS (GUI based OS), Unix/Linux OS.

1.  DOS  Disk Operating System (DOS) was developed in 1981 by Microsoft and named as MS-DOS. This
was a single-user, command-based operating system. Users were provided commands like dir, makedir, chdir,
copy, type, print, etc. and all actions were to be done through command prompt. Users needed to remember
these commands and their exact syntax in order to use the computer. MS-DOS continued up to 1994 with its
latest version as MS-DOS 6.22 and was replaced by MS-Windows.

2.  Windows OS (GUI Based OS)  The concept of using graphical interface was initiated by Apple Com-
puters on their Macintosh machines. The idea was to provide a more convenient way of working and the users
need not remember various commands. This concept was further extended by Microsoft by launching Win-
dows 3.1 software in 1991 which used to work over DOS. Windows 3.1 was not an OS but provided graphical
­support for many activities. Based on the popularity of Windows 3.1, Windows 95 was launched by Microsoft
in 1995 as an operating system which used to work through icons (graphical representation of many utilities
and commands). This was further improved as Windows 98, Windows 2000, Windows XP, Windows 7, and
Windows 8. The latest operating system in this series is Windows 10, which is a very user-friendly, powerful
and secure operating system. But all these operating systems are mainly useful on personal computers only.
Multiple users cannot work on the same computer using these operating systems.

3.  Unix/Linux OS  Unix operating system was developed to provide support for using the same computer
by many users at the same time. It was developed by AT&T, Bell Labs in 1969 and was a multi-user, multi-
tasking, time-sharing operating system. Many users were able to work on the same computer with help of
sharing the time of the CPU in small slots with help of multiple tasks being handled at the same time. The
time slots allotted to each user were quite small so that other users could hardly notice the delay, but this slot
was quite sufficient for each user to get some significant work completed on the CPU. Another very useful
advantage of the Unix OS was its security, which was very difficult to breach. This was also a command-
based operating system and provided commands like ls, mkdir, cd, cat, cp, mv, etc.
Subsequently, Unix also went through lot of changes and provided good GUI support for normal opera-
tions. Nowadays Unix has become a kind of trademark and there exist many Unix like OS, such as HP-UX,
Sun-Solaris, Linux, etc. All of these provide good GUI support and are very secure as compared to Windows
based OS. Due to their security, very few virus attacks are visible in Unix based operating systems. Linux has
further popularized this category by providing it almost for free. Linux is a Unix like OS developed under
GNU project and is totally free for non-commercial usage. Linux tried to break the monopoly of Windows
and their higher costs of OS and has become quite popular in recent years. Many different implementations
of Linux are available in the market now, e.g., Ubuntu, Fedora, Red hat Linux, SUSE, etc.—all of these

Prog-C_01.indd 6 7/10/2018 2:01:07 PM


Introductory Concepts 1.7

are GUI-based, free, very secure, multi-user operating systems. Many of these Linux OS have become very
popular because their cost is nil or minimum and they have almost all features which Windows OS provide.
At the same time, they are less prone to viruses and hence many users prefer to use Linux as an OS in their
computers. Moreover, these OS are being regularly improved by contributions from many developers across
the world, who work to improve these software without any remuneration.
The most visible use of an operating system for most of the users is in the form of storing and accessing
various files and folders. A folder is a collection of sub-folders and files. All operating systems provide
support for organizing the users’ data in the form of files and folders. Almost all operating systems maintain
their files and folders in different drives in the form of a tree structure for storage. A drive represents a
physical/logical partition of the secondary memory, e.g., hard disk, floppy disk, compact disc, pen drive, etc.
Each drive maintains a tree structure for organizing its data. In the tree structure, each file as well as folder
(or sub folder) will have a unique address, through which that file/folder can be accessed. The tree structure
shown in Fig. 1.4 resembles an upside down tree in real life. It starts with a root at the top, and consists of
many branches and leaves. One branch is equivalent to one folder or directory and the leaf represents a file.
Each of the branches can have further sub-branches (i.e., sub-folders or sub-directories) as well as leaves
(i.e., files). On the other hand, leaves are individual entities and cannot be further divided. For example, the
tree structure of Fig. 1.4 represents three folders at the root level, namely system, games and programming.
The programming folder further consists of two sub-folders, namely c_programs and cpp_programs, and
two files named as input.dat and output.txt. The folder c_program further contains three subfolders
and two files, as shown in the figure.

(represents root of the tree)

system games programming

...
windows.exe

c_programs cpp_programs input.dat output.txt

...

2018--19 simple difficult fact.c quad.c

. . . . . . ...
  Fig. 1.4 

Prog-C_01.indd 7 7/10/2018 2:01:07 PM


1.8 Programming with C

Usually a file name consists of an extension telling its type, written after the dot (i.e., .txt, .dat, .c, .doc,
.cpp, etc.) and the folder usually does not contain extensions, but it is not compulsory. No two files/folders
can have the same name within the same folder. However, files and folders of different levels can have the
same names. It means that there cannot be another file or folder with ‘system’ name within the root, but a
file or folder within ‘programming’ folder can easily have the name as ‘system’. The main advantage of this
tree structure is that each file and folder always has a unique and complete address, which starts from root
and reaches up to that file/folder covering all in-between folder names. For example, the complete address
of file quad.c is \programming\c_programs\quad.c. This complete address is unique for this file and can
be used from anywhere in the file system to refer to this file. Similarly, the complete address of the folder
games will be \games and that of windows.exe file will be \system\windows.exe. This complete address is
preceded by the drive name, e.g., C:, D:, A:, E, etc. So, if this tree structure of Fig. 1.6 was corresponding
to the C drive of the computer, the complete address of the file quad.c will be C:\programming\c_programs\
quad.c. This address is called absolute address as it is usable from anywhere in the file system. The files
and folders can be referred using relative addresses also, which is always dependent on our present work-
ing directory/folder (commonly referred as pwd). For example, if we are presently working in c_programs
folder, the file quad.c can be easily referred by name ‘quad.c’ instead of the lengthy absolute address. The
relative address of file input.dat from c_programs folder will be ‘..\input.dat’, where ‘..’ represents the need
to go one level up in the tree structure. If backslash (\) is used after the drive name, it represents the root
location of that drive and any subsequent use of backslash is done to separate the folder name from the
next folder/file name. The DOS and the Windows operating systems use backslash (\) in their addressing,
but Unix and Linux operating systems use forward slash (/) for the same purpose. So in case of Unix and
Linux, the absolute address of file quad.c will be /programming/c_progra,s/quad.c.

1.5.2  Compiler
In order to use a computer for different purposes, it needs a set of instructions to do that particular work. These
instructions are usually written in some English like languages known as high level languages. The computer
understands only machine language and thus cannot directly understand these programs and needs some trans-
lator. The problem is similar to a situation when an Indian goes to Germany. He/she will need a translator to
communicate with the German people. The translator will convert Hindi to German and vice versa. Similarly,
if a programmer wants to give some set of instructions written in any high level language, he/she needs a
conversion from high level language to binary language. This conversion is done with the help of a compiler.
A compiler is a kind of system software that translates programs written in high level language to machine
language. The original high level program is called the source program, and the resulting machine-language
program is called the object program. C-compiler, Pascal-compiler, Java-compiler are some examples of
compilers. Just as we will need a different translator if we go to France, a computer will also require different
compilers for different languages.
Interpreter is a software similar to a compiler. It is a system software which is used to convert high
level language programs to machine language but its working is different from that of a compiler. The
compiler converts the whole program to machine language. This converted program may be stored
somewhere in the memory and then may be executed without requiring the presence of the compiler.
But an interpreter converts high level language program one line at a time to machine language and then
executes that converted line. Then the next line is converted and executed, and so on. Thus, the presence
of the interpreter is a must while executing the program. Usually, an interpreter is a smaller and simpler
software than a compiler, but it may take more time for execution. LISP was one of the languages that

Prog-C_01.indd 8 7/10/2018 2:01:07 PM


Introductory Concepts 1.9

used an interpreter instead of a compiler. Programs are debugged using line-by-line execution with help
of interpreters. Nowadays, the Java interpreter is used to execute the byte code of Java.

1.6 How to develop a Program

Program writing is a systematic process. It is not all about coding only. It is not a magical or random process.
It requires following a certain methodology. In order to write a program, following steps must be formally/
informally followed:
1 . Analyze the problem to identify inputs, outputs and processing requirements.
2. Identify various processing steps needed to solve the problem and represent them in a particular way
(algorithm, pseudo code, flow chart, etc.)
3. Refine Step 2 in a way that all the processing steps are detailed enough such that every processing step
is equivalent to one instruction of the programming language.
4. Add the syntax of the programming language to the above representation and it becomes the program.
5. Type the program and compile it after removing all syntax related errors.
6. Run the program and check it with different types of input to verify its correctness. If the results are in-
correct for any combination of inputs, review all the processing steps, and identify the mistake. Correct
the mistake and again continue with Step 4 above.
Steps 2 and 3 of the above process ensure that the processing requirements are represented in a good
manner so that these can be easily converted to the program later. Three commonly used representations are
algorithm, pseudo-code and flow chart.

1.6.1 Algorithm
It is a complete step-by-step representation of the solution of a problem, represented in English like language.
An algorithm can be quite abstract or quite detailed. A detailed algorithm consists of every step, equivalent to
one instruction of a programming language (C, C++, Java, etc.). Following examples will help you in under-
standing the basis process of logic development and writing an algorithm.
Problem 1: Write a program to find addition of two numbers.
It is one of the simplest problems to be implemented. The problem states that two numbers should be read
and their addition result should be displayed. The corresponding algorithm for this problem can be written as
1. Read first and second number.
2. Add both numbers to get the result.
3. Print the result.
Each step in the above algorithm is simple and does not require any further sub-steps.
Problem 2: Write a program to find the bigger number out of two distinct numbers.
Problem 1 needed three simple steps in a sequence. Compared to that, this problem also requires a decision
to be made based on comparison of two values. This problem demands two numbers be read and the number
which is bigger than the other be displayed. The problem statement clearly mentions that both numbers are
distinct and thus they will never be equal. Its equivalent algorithm will be
1. Read first and second number.
2. Compare first number with second number.

Prog-C_01.indd 9 7/10/2018 2:01:07 PM


1.10 Programming with C

3. If first number is bigger than second then print the first number.
4. Otherwise print the second number.
As it can be observed, each step is still simple and does not require any further sub-steps.
Problem 3: Write a program to find the largest number out of three distinct numbers.
Compared to Problem 2, this problem needs three numbers to be read as input. Similarly, each number needs
to be compared with remaining two numbers to find the result (also called as output). Again note that all
numbers are distinct and thus no two numbers can be equal. It can be represented as the following algorithm:
1. Read first, second and third number.
2. Print first number if it is bigger than second and third.
3. Otherwise print second number if it is bigger than first and third.
4. Otherwise print third number.
Here, Steps 2 and 3 are written in an abstract way and these can be made more detailed. For example, first
number can be compared with other two numbers in two different ways: (a) compare first number with second
as well as third number in same step, and (b) compare first number with second initially and then with third
number in next step.
Following two algorithms will clarify the difference between the two approaches:
Algorithm 3 (a)
1. Read first, second and third number.
2. If first number is bigger than second and first number is bigger than third
3.   Print first number as largest
4. Otherwise (means that first is not largest)
5.   If second number is bigger than first and second number is bigger than third
6.    Print second number as largest
7.   Otherwise (means that second is not largest and first is also not largest)
8.    Print third number as maximum.

Algorithm 3(b)
  1. Read first, second and third number.
  2. If first number is bigger than second number
  3.   If first number is bigger than third number
  4.    Print first number as largest
  5.   Otherwise (means third is bigger than first and first was already bigger than second)
  6.    Print third number as largest
  7. Otherwise (means second number is bigger than first & thus second to be compared with third)
  8.   If second number is bigger than third number
  9.    Print second number as largest
10.   Otherwise (means third is bigger than second and second was already bigger than first)
11.    Print third number as largest.
Both algorithms 3(a) and 3(b) have each step equivalent to some programming language instruction and
thus, are complete solutions of the problem.
Problem 4: Write a program to find the average of n numbers.
After analysis of this problem, we can write an abstract algorithm as
1. Read the value of n.

Prog-C_01.indd 10 7/10/2018 2:01:07 PM


Introductory Concepts 1.11

2. Read n numbers.
3. Add these n numbers.
4. Divide the addition result by n to get the average.
5. Print the average.
In the above algorithm, Steps 2 and 3 are not simple. Both of these require repetition of the work. The
details of that repetition can also be specified to make the algorithm clearer. The detailed algorithm with the
repetition details are given below:
1. Read the value of n.
2. Assign 0 to sum.
3. Repeat n times.
4.   Read number.
5.   Add number to sum.
6. End repeat.
7. Divide sum by n to get the average.
8. Print average.

1.6.2  Pseudo Code


Another useful representation of the detailed step is pseudo code. It is a more formal representation than the
­algorithm. Here, we represent every step in a formal way which is very close to the actual programming lan-
guage representation. For example, Step 3 of the above algorithm describes repetition, which is implemented
via some loops (for, while-do, do- while). So in pseudo code representation, each of the steps will be written
via operators and statements equivalent to some programming language instructions. The only difference will
be that the exact syntax of the programming language will not be followed. All pseudo codes will start with
keyword ‘START’ and complete with keyword ‘STOP’ or ‘END’. Pseudo codes for problems 1, 2, 3(a), 3(b)
and 4 are given below and are self-explanatory in nature.
Pseudo Code for Problem 1 Pseudo Code for Problem 1
1. START 1. START
2. Read number1, number2 2. Read number1, number2
3. sum• number1 + number2 3. If number1 > number2 then
4. Print sum 4.   Print number1
5. STOP 5. Else
6.   Print number2
7. STOP
Pseudo Code for Problem 3(a) Pseudo Code for Problem 3(b)
1. START   1. START
2. Read number1, number2, number3   2. Read number1, number2, number3
3. If number1 > number2 AND number 1 > number 3 then   3. If number1 > number2 then
4.   Print number1   4.   If number1 > number3 then
5. Else   5.    Print number1
6.   If number2 > number1 AND number2 > number3 then   6.   Else
  7.    Print number3
  8. Else

Prog-C_01.indd 11 7/10/2018 2:01:07 PM


1.12 Programming with C

  7.    Print number2   9.   If number2 > number3 then


  8.   Else 10.    Print number2
  9.    Print number3 11.   Else
10. STOP 12.    Print number3
13. STOP
Pseudo Code for Problem 4
1. START Start, Stop
2. Read n
3. sum ¨ 0
4. for i ¨ 1 to n do Read, Print
5.   read numberi
6.   sum ¨ sum + numberi
7. end for Processing statements
8. average ¨ sum/n
9. print average
10. STOP Condition check

1.6.3  Flow Chart


Another very popular method to represent the steps of the Direction of flow
solution is the flow chart, which uses many graphical symbols
and thus, is more understandable. The symbols used for vari-
ous different types of statements/steps are shown in Fig. 1.5. Connectors
(for longer flow charts)
The flow charts for all of above problems are drawn below
in Fig 1.6 (Problem 1 and 2), Fig 1.7 (Problems 3(a) and 3(b))
and Fig 1.8 (Problem 4).   Fig. 1.5 

START
START

Read number1, number2


Read number1, number2

Is number1 >
number2 ?
sum ¨ number1 + number2

Print Number1 Print Number1


Read number1, number2
Yes No

STOP STOP
(a) (b)
  Fig. 1.6 

Prog-C_01.indd 12 7/10/2018 2:01:08 PM


Introductory Concepts 1.13

START

Read number1, number2, number3

Is number1 > number2


and number1 > number3 ?
No
Yes

Print number1 Is number2 > number1


and number2 > number3
Yes No

Print number2 Print number3

STOP

Problem 3(a)

START

Read number1, number2, number3

Is number1 > number2 ?


Yes No

Is number1 > Is number2 >


number3 ? number3 ?
Yes No Yes No

Print number1 Print number3 Print number2 Print number3

STOP

Problem 3(b)

  Fig. 1.7 

Prog-C_01.indd 13 7/10/2018 2:01:08 PM


1.14 Programming with C

1.7 SOFTWARE DEVELOPMENT LIFE CYCLE


START
In order to develop any program/software, the following five phases should
be followed:
1. Requirement Analysis Read n
2. Software Design
3. Software Coding
4. Software Testing
5. Software Installation and Maintenance sum 0
i 1
In the requirement analysis phase, the developer tries to identify what is
to be done. The exact specification of the work to be done is understood and Yes
documented in this phase. The second phase tries to find the solution of the
problem, i.e., how to do. This phase is most important in software develop- i <= n
?
ment and is used to plan the solution as a collection of sub-problems such that
each sub-problem can be easily solved. After the design, the coding is done in Yes
any programming language. The developed code is tested to find errors and
correct them in the software testing phase. Finally, the software is installed Read numberi
and maintained. In order to develop the software in a better way, it is strongly
recommended to use the concept of structured programming, which results No
into more reliable and quality design and coding.
sum sum + numberi

1.8 STRUCTURED PROGRAMMING

It refers to a programming strategy that encompasses a number of methodolo-


gies to produce good quality design and code, which can be easily understood,
tested, debugged, modified and maintained in future. There are three main average sum/n
principles of structured programming: i i + 1

1. Program design using top-down or bottom-up approach


2. Decomposition of program into components, i.e., modular
programming Print average
3. Structuring of control flow

1.8.1  Program Design


STOP
Program design concentrates on planning the solution as a collection of sub-
solution. It can be done in two ways—top down design or bottom-up design.   Fig. 1.8 
During top-down design, the divide and conquer policy is followed. The
problem is divided into smaller sub-problems. These sub-problems are further
divided into even smaller sub-problems. This process of ­decomposition progresses with an increasing level
of details and continues till each sub-problem is not solvable easily without any further sub-division. In the
top-down approach, the calling component is always designed before its sub-components. Thus, top-down
design represents a successive refinement of functionality of the program such that every refinement leads
to more clarity and takes us closer to the solution. A typical top-down design diagram is shown in Fig. 1.9.

Prog-C_01.indd 14 7/10/2018 2:01:08 PM


Introductory Concepts 1.15

The program A has been divided into 3 components—B, C and D. The D component is found to be solvable
easily and thus need not be further subdivided. However, B and C are further divided into sub-components
and the process continues till each sub-component is not found to be solvable without further sub-division.
The bottom-up design is the reverse of the top-down approach. Here, the process starts with the identifica-
tion of the smallest sub-components of the total program, which can be easily implemented. Such smallest

B C D

E F G H

I J K L M

  Fig. 1.9 

components are combined to reach to a more abstract level and plan components of the higher level. This
process is continued till the complete program does not get realized. The main drawback of this approach is
that it is rarely possible to identify smallest
sub-components needed for the program,
especially for bigger programs. The typical I J F K L M H D
method of developing a program using the
bottom-up approach will look the reverse of E G
Fig. 1.9, where the designed should be able
to identify smallest components D, F, H, I, J,
K, L and M, which do not require any other B C
component for their implementation. Then
these components need to be successively
grouped together so as to reach the final so-
lution for program A, as shown in Fig. 1.10.
A
1.8.2  Modular Programming
This principle of structured programming aims   Fig. 1.10 
to have logical divisions of the total solution.

Prog-C_01.indd 15 7/10/2018 2:01:08 PM


1.16 Programming with C

The program design already suggests various components to be used for planning the final solution. Each
component can be implemented as a part of the main program also, and the main program can become quite
lengthy. However, the modular programming principle suggests writing the code as a collection of many mod-
ules. A module is a portion of the program which is responsible to carry out a certain work, and can be used
with other modules. A module may be considered as decomposed into successively subordinate modules and
many modules can be combined also to form a superior module. A module can be repeatedly called, may be
with different input values and can also be reused in other programs with little/as modifications. The advantage
of developing modules is that the superior module need not physically include the codes of its subordinate
modules within itself. All the modules are developed separately and the superior module can call the subordinate
modules. This process is known as ‘calling’ of subordinate module by superior module. Alternatively, it can
be said that subordinate module is ‘called by’ the superior module. The superior module is called the calling
module and the subordinate module is called the called module. The advantages of modular programming are
better readability of a program, easy debugging, fault localization, ease of modification, better maintenance, etc.

1.8.3  Structuring of Control Flow


The traditional meaning of structured programming is taken in following a systematic flow of control throughout
a program. The idea behind structuring the flow of control is to reduce the complexity, which gets increased
due to abrupt control transfers, interconnections and cross connections among various parts of a program.
It strongly advocates the principle of single entry and single exit to all sub-parts of a program. Single entry
means that there should be only one way to enter into a sub-part (module, components or a control statement),
and similarly there should be only one exit from the sub-part. The advantage is that once the control enters the
sub-part, it executes the statements within the sub-part until it reaches the exit point. This can be achieved by
implementing all processing requirements using combinations of three basic control flows:
1. Sequence (steps one after the other)
2. Selection (choose one out of two/many)
3. Iteration (repeat steps many times)
It has been proven that all kinds of processing can be implemented using these three basic control flow struc-
tures and use of statements like GOTO must be avoided, which leads to a complex and confusing structure of
control flow. The programs written using three basic constructs are definitely more readable and easy to modify.

1.9 TYPES OF PROGRAMMING LANGUAGES

There are many different languages that can be used to program a computer. The most basic of these is machine
language—a collection of very detailed, cryptic instructions that control the computer’s internal circuitry. This
is the natural dialect of the computer. Very few computer programs are actually written in machine language,
however, for two significant reasons: First, because machine language is very cumbersome to work with and
second, because every different type of computer has its own unique instruction set. Thus, a machine-language
program written for one type of computer cannot be run on another type of computer without significant
alterations.
Usually, a computer program will be written in some high level language, whose instruction set is more
compatible with human languages and human thought processes. Most of these are general-purpose languages
such as C, Pascal, Fortran and BASIC. There are also various special-purpose languages that are specifically

Prog-C_01.indd 16 7/10/2018 2:01:08 PM


Introductory Concepts 1.17

designed for some particular type of application. Some common examples are CSMP and SIMAN, which are
special-purpose simulation languages, and LISP, a list- processing language that is widely used for artificial
intelligence applications.
As a rule, a single instruction in a high-level language will be equivalent to several instructions in machine
language. This greatly simplifies the task of writing complete, correct programs. Furthermore, the rules for
programming in a particular high-level languages are much the same for all computers, so that a program
written for one computer can generally be run on many different computers with little or no alteration. Thus,
we see that a high-level language offers three significant advantages over a machine language: simplicity,
uniformity and portability (i.e., machine independence).

1.10 INTRODUCTION TO C

C is a general-purpose, structured programming language. Its instructions consist of terms that resemble al-
gebraic expressions, augmented by certain English keywords such as if, else, for, do and while. In this respect
C resembles other high-level structured programming languages such as Pascal and Fortran. C also contains
certain additional features that allow it to be used at a lower level, thus bridging the gap between machine
language and the more conventional high-level languages. This flexibility allows C to be used for systems
programming (e.g., for writing operating systems) as well as for applications programming (e.g., for writing a
program to solve a complicated system of mathematical equations, or for writing a program to bill customers).
C is characterized by the ability to write very concise source programs, due in part to the large number of
operators included within the language. It has a relatively small instruction set, though actual implementa-
tions include extensive library functions which enhance the basic instructions. Furthermore, the language
encourages users to write additional library functions of their own. Thus, the features and capabilities of the
language can easily be extended by the user.
C compilers are commonly available for computers of all sizes, and C interpreters are becoming increasingly
common. The compilers are usually compact, and they generate object programs that are small and highly
efficient when compared with programs compiled from other high-level languages. The interpreters are less
efficient, though they are easier to use when developing a new program.
Another important characteristic of C is that its programs are highly portable, even more so than with other
high-level languages. The reason for this is that C relegates most computer-dependent features to its library
functions. Thus, every version of C is accompanied by its own set of library functions, which are written for
the particular characteristics of the host computer. These library functions are relatively standardized, how-
ever, and each individual library function is generally accessed in the same manner from one version of C to
another. Therefore, most C programs can be processed on many different computers with little or no alteration.
1.10.1 History of C
C was originally developed in the 1970s by Dennis Ritchie at Bell Telephone Laboratories, Inc. (now a part
of AT&T). It is an outgrowth of two earlier languages, called BCPL and B, which were also developed at Bell
Laboratories. C was largely confined to use within Bell Laboratories until 1978, when Brian Kernighan and
Ritchie published a definitive description of the language.* The Kernighan and Ritchie description is com-
monly referred to as ‘K&R C.’
Following the publication of the K&R description, computer professionals, impressed with C’s many desirable
features, began to promote the use of the language. By the mid 1980s, the popularity of C had become wide-
spread. Numerous C compilers and interpreters had been written for computers of all sizes, and many commercial
  * Brian W. Kernighan and Dennis M. Ritchie, The C Programming Language, Prentice-Hall, 1978.

Prog-C_01.indd 17 7/10/2018 2:01:08 PM


1.18 Programming with C

application programs had been developed. Moreover, many commercial software products that were originally
written in other languages were rewritten in C in order to take advantage of its efficiency and its portability.
Early commercial implementations of C differed somewhat from Kernighan and Ritchie’s original
definition, resulting in minor incompatibilities between different implementations of the language. These
differences diminished the portability that the language attempted to provide. Consequently, the American
National Standards Institute** (ANSI committee X3J11) has developed a standardized definition of the C
language. Virtually, all commercial C compilers and interpreters now adhere to the ANSI standard. Many
also provide additional features of their own.
In the early 1980s, another high-level programming language, called C++, was developed by Bjarne
Stroustrup*** at the Bell Laboratories. C++ is built upon C, and hence all standard C features are available
within C++. However, C++ is not merely an extension of C. Rather, it incorporates several new fundamental
concepts that form a basis for object-oriented programming—a new programming paradigm that is of interest
to professional programmers. We will not describe C++ in this book, except to mention that a knowledge of
C is an excellent starting point for learning C++.
This book describes the features of C that are included in the ANSI standard and are supported by ­commercial C
compilers and interpreters. The reader who has mastered this material should have no difficulty in ­customizing
a C program to any particular implementation of the language.

1.10.2  Structure of a C Program


Every C program consists of one or more modules called functions. One of the functions, that must be called,
is main. The program will always begin by executing the main function, which may access other functions.
Any other function definitions must be defined separately, either ahead of or after main (more about this
later, in Chaps. 7 and 8).
Each function must contain:
1. A function heading, which consists of the function name, followed by an optional list of arguments,
enclosed in parentheses.
2. A list of argument declarations, if arguments are included in the heading.
3. A compound statement, which comprises the remainder of the function.
The arguments are symbols that represent information being passed between the function and other parts
of the program. (Arguments are also referred to as parameters.)
Each compound statement is enclosed within a pair of braces, i.e., { }. The braces may contain one or more
elementary statements (called expression statements) and other compound statements. Thus, compound state-
ments may be nested, one within another. Each expression statement must end with a semicolon (;).
Comments (remarks) may appear anywhere within a program, as long as they are placed within the delimiters
/ * and * / (e.g., /* this is a comment * /). Such comments are helpful in identifying the program’s principal
features or in explaining the underlying logic of various program features.
These program components will be discussed in much greater detail later in this book. For now, we will
discuss an overview of the basic features that characterize most C programs.

***  ANSI Standard X3.159—1989. American National Standards Institute, 1430 Broadway, New York, NY, 10018. (See also Brian
W. Kernighan and Dennis M. Ritchie, The C Programming Language, 2d ed., Prentice-Hall, 1988.)
***  Stroustrup, Bjarne, The C++ Programming Language, 2d ed., Addison-Wesley, 1991.

Prog-C_01.indd 18 7/10/2018 2:01:08 PM


Introductory Concepts 1.19

  Example 1.1
Area of a Circle  Here is an elementary C program that reads in the radius of a circle, calculates its area and
then writes the calculated result.
/* program to calculate the area of a circle */ /* TITLE (COMMENT) */
#include <stdio.h> /* LIBRARY FILE ACCESS */
main() /* FUNCTION HEADING */
{
float radius, area; /* VARIABLE DECLARATION */
printf("Radius = ? "); /* OUTPUT STATEMENT (PROMPT) */
scanf("%f", &radius); /* INPUT STATEMENT */
area = 3.14159 * radius * radius; /* ASSIGNMENT STATEMENT */
printf("Area = %f", area); /* OUTPUT STATEMENT */
}
  The comments at the end of each line have been added in order to emphasize the overall program organiza-
tion. Normally, a C program will not look like this. Rather, it might appear as shown below.
/* program to calculate the area of a circle */
#include <stdio.h>
main()
{
float radius, area;
printf("Radius = ? ");
scanf("%f", &radius);
area = 3.14159 * radius * radius;
printf("Area = %f", area);
}

The following features should be pointed out in this last program.


1. The program is typed in lowercase. Please remember that we have to be careful in using lowercase or
uppercase letters. C is a case-sensitive language, meaning that it differentiates between lowercase and
uppercase characters and words. Most of the statements in a C program will be written in lowercase.
User defined variables can be defined in either case but conventionally lowercase is used most of the
times. Later in this book we will see some special situations that are characteristically typed in up-
percase.
2. The first line is a comment that identifies the purpose of the program.
3. The second line contains a reference to a special file (called stdio.h) which contains information
that must be included in the program when it is compiled. The inclusion of this required information
will be handled automatically by the compiler.

Prog-C_01.indd 19 7/10/2018 2:01:08 PM


1.20 Programming with C

4. The third line is a heading for the function main. The empty parentheses following the name of the
function indicate that this function does not include any arguments.
5. The remaining five lines of the program are indented and enclosed within a pair of braces. These five
lines comprise the compound statement within main.
6. The first indented line is a variable declaration. It establishes the symbolic names radius and area as
floating-point variables (more about this in the next chapter).
7. The remaining four indented lines are expression statements. The second indented line (print f)
generates a request for information (namely, a value for the radius). This value is entered into the
computer via the third indented line (scan f).
8. The fourth indented line is a particular type of expression statement called an assignment statement.
This statement causes the area to be calculated from the given value of the radius. Within this state-
ment the asterisks (*) represent multiplication signs.
9. The last indented line (print f) causes the calculated value for the area to be displayed. The numeri-
cal value will be preceded by a brief label.
10. Notice that each expression statement within the compound statement ends with a semicolon. This is
required of all expression statements.
11. Finally, notice the liberal use of spacing and indentation, creating whitespace within the program.
The blank lines separate different parts of the program into logically identifiable components, and the
indentation indicates subordinate relationships among the various instructions. These features are not
grammatically essential, but their presence is strongly encouraged as a matter of good programming
practice.
Execution of the program results in an interactive dialog such as that shown below. The user’s response is
underlined for clarity.
Radius = ? 3
Area = 28.274309

1.11 DESIRABLE PROGRAM CHARACTERISTICS

Before concluding this chapter let us briefly examine some important characteristics of well-written computer
programs. These characteristics apply to programs that are written in any programming language, not just C.
They can provide us with a useful set of guidelines later in this book, when we start writing our own C programs.
1. Integrity. This refers to the accuracy of the calculations. It should be clear that all other program
enhancements will be meaningless if the calculations are not carried out correctly. Thus, the integrity
of the calculations is an absolute necessity in any computer program.
2. Clarity refers to the overall readability of the program, with particular emphasis on its underlying logic.
If a program is clearly written, it should be possible for another programmer to follow the program
logic without undue effort. It should also be possible for the original author to follow his or her own
program after being away from the program for an extended period of time. One of the objectives in the
design of C is the development of clear, readable programs through an orderly and disciplined approach
to programming.
3. Simplicity. The clarity and accuracy of a program are usually enhanced by keeping things as simple
as possible, consistent with the overall program objectives. In fact, it may be desirable to sacrifice a

Prog-C_01.indd 20 7/10/2018 2:01:08 PM


Introductory Concepts 1.21

certain amount of computational efficiency in order to maintain a relatively simple, straightforward


program structure.
4. Efficiency is concerned with execution speed and efficient memory utilization. These are generally
important goals, though they should not be obtained at the expense of clarity or simplicity. Many
complex programs require a tradeoff between these characteristics. In such situations, experience and
common sense are key factors.
5. Modularity. Many programs can be broken down into a series of identifiable subtasks. It is good
programming practice to implement each of these subtasks as a separate program module. In C, such
modules are written as functions. The use of a modular programming structure enhances the accuracy
and clarity of a program, and it facilitates future program alterations.
6. Generality. Usually we will want a program to be as general as possible, within reasonable limits. For
example, we may design a program to read in the values of certain key parameters rather than placing
fixed values into the program. As a rule, a considerable amount of generality can be obtained with very
little additional programming effort.

  REVIEW QUESTIONS

1.1 Draw the block diagram of a computer and explain various blocks.
1.2 What are various types of memory? How do they differ from each other.
1.3 Compare the use and working of primary and secondary memory.
1.4 What is need of an operating system? What are its responsibilities?
1.5 What are the various types of operating systems? Compare their features and working.
1.6 Explain the tree structure of storage system. Differentiate between absolute addressing and relative
addressing of files.
1.7 Write an algorithm, pseudo code and flow chart for finding:
(i)  Minimum out of three numbers
(ii)  Maximum out of four numbers
(iii)  Factorial of a number
(iv)  Smallest out of n numbers
1.8 List various phases of software development and describe each briefly.
1.9 How is structured programming done? What problems might be faced if programs are not developed
in a structured way?
1.10 What is machine language? How does machine language differ from high-level languages?
1.11 Name some commonly used high-level languages. What are the advantages of using high-level lan-
guages?
1.12 What is meant by compilation? What is meant by interpretation? How do these two processes differ?
1.13 What are the major components of a C program? What significance is attached to the name main?
1.14 How can comments (remarks) be included within a C program? Where can comments be placed?
1.15 Are C programs required to be typed in lowercase? Is uppercase ever used in a C program? Explain.
1.16 Why are some of the statements within a C program indented? Why are empty lines included within
a typical C program?
1.17 Summarize the meaning of each of the following program characteristics: integrity, clarity, simplicity,
efficiency, modularity and generality. Why is each characteristic important?
1.18 Compare top-down and bottom-up approaches of program design.

Prog-C_01.indd 21 7/10/2018 2:01:08 PM


Prog-C_01.indd 22 7/10/2018 2:01:08 PM

Vous aimerez peut-être aussi