Introduction
to programming concepts
Computers belonging to different domains are used to perform
a variety of tasks. Do you know how a computer is able to perform different
tasks?
To carry out the work, the computer input, process and
adhere to the basic principle OUYPUT. Computer information in a set of
instructions called a program, process, and production activities is needed.
In this chapter input, process, output requirements and the
introduction of a computer. The instructions in a computer program and died PROGRMMING
explains the role of languages In
addition, it describes various tools to solve the problem solving.
Input process and output
You might have noticed computer being used at airline
reservation counters, shops, restaurants, and various other locations. At each
of these places, a user enters some values in the computer and the computer
generates an output, which is, either displayed on the computer screen or
printed on a paper.
Let us consider the example of an airline reservation
counter. When you want to resrve a seat on particular flight, you provide
information about your requirements, such as the destination, the data and time
of your departure, and the class in which a booking is required. The executive
at the reservation counter enters this information in a computer. The details
regarding the availability of tickets are then displayed on the screen.
The preceding automated process is not carried out as one
activity. It is broken into a set of activities that are carried out in
separate phases. Let us see the phases involved and the process followed for
each of the tasks.
Phases
To undersand what happens when you enter some values in a
computer, let us break the entire set of activities into separate phases.
Consider the example of the airline reservation counter. In
the first phase, flight requirements are keyed into the computer. This phase is
called the input phase. The flight requirement information is then processed to
determine whether seats are availble on the particular flight is known as the
processphase. Once the processing is complete, the result is displayed on the
computer screen, indicating the status of seat availability. This phase is
called the output phase.
The cycle of activities performed by a computer is also
known as the input - process- output (I-P-O) cycle.
I-P-O Cycle
A computer consists of several componets, such as a
keyboard, mouse, mouse, monitor, printer, and central processing unit (CPU).
Each computer participates in any one of the phases: process, or output. For example,
the keyboard and mouse are used for input. The CPU and memory inside the CPU
are used for processing. The monitor and printer are used for output. Following
figure depicts the (I-P-O) cycle.
Process
|
The (I-P-O) cycle
Programs
and programming languages
Have you ever wondered how a computer:
Ø
Remembers the steps to be followed to process
reservation requests
Ø
Manages to calculate your shopping bills at the
local store
Ø
Generates your report card at school
Is a computer an all-knowing machine? Do you have to buy a
different computer for each of the preceding activities? The answer to the last
two questions is ‘No’.
It is not an all-knowing machine, and you do not require a
separate computer for each of these activities.
Then, how does a computer function? A computer is an
electronic device. It needs a set of instructions called a program to perform
the I-P-O cycle. A program needs to be written in a specific language, called a
programming language.
Let us discuss programs and programming languages in detail.
Programs
All of us perform a number of tasks in our day-to-day lives.
Some of the tasks that we perform are:
Ø
Crossing the traffic signal while you are in a
car
Ø
Managing your budget
Ø
Troubleshooting why your printer is not
responding
Ø
Forming a strategy to reach the next level of a
computer game
To perform these tasks, you follow a step-by-step procedure.
For example, while driving a can, you comply with the following sequence of
steps to cross a traffic signal:
1.
Approach the light.
2.
If the light is red, you will stop.
3.
If the light is yellow, you will slow down and
then, stop.
4.
Otherwise, the light will be green and continue driving.
In the same way, a computer uses a step-by-step procedure to
perform any task. A computer is designed to accept an input, process it, and
generate an output. However, you need to provide a set of instructions that
specify:
Ø
The kind of input that will be provided. For
example, in the case of reservation requests, the flight date, flight time,
class, and destination will be some of the input.
Ø
The Processing that needs to be done. For
example, accepting the values, checking for availability of seats, and
displaying results will be some of the processes.
Therefore, for each job that you want the computer to
perform, you require a separate program. Depending upon the requirements,
instructions in a program can be:
Ø
Sequential:
instructions that are executed one after the other.
Decision making: instructions that evaluate an expression
(relation or condition) first and then, depending upon whether the value of the
expression is true or false, the control is transferred to a particular
statement.
?
|
Iterative: instructions that are executed repeatedly, depending
upon the value of an expression (relation or condition).
Any real-life problem that you solve with the help of a
computer will consist of a number of small jobs. Therefore, to solve such a
problem, you have a number of programs that together form an application. For
example, a gaming application might have the following two programs:
Ø
Program to run the game.
Ø
Program to record the scores of players.
Programming
languages
A computer is made up of a number of electronic components.
An electronic component can be either in an ON state or in an OFF state. The ON
state is represented by 1, and the OFF state is represented by 0. This implies
that a computer can understand only 0s and instructions. This collection is
called machine language code or
simply machine code. For example, the following table depicts machine code that
can be used to represent the commands, ADD and SUBTRACT.
Command
|
Machine code
|
ADD
|
1010000100
|
SUBTRACT
|
1010001000
|
The machine code of
the ADD and SUBTRACT commands
However, it is difficult for anybody to remember
instructions in the form of machine language code. Therefore, we use high-level
programming languages to write programs. A high-level programming language
consists of a set of instructions, which are represented using simple English
words. Therefore, when you want the computer to display the output on the
screen, you type an instruction like ‘WRITE NO SCREEN’ and not ‘00100000’.
There are many high-level programming languages available, such as C, C++, and
java.
All language have a vocabulary, which is a list of words
that have specific meanings in that language. Languages also have their own
grammar rules, which state the rules for combining words to form sentences. The
vocabulary of programming languages is known as the set of keywords of that
language, and the grammar is known as the syntax. For example, in a programming
language, you can the following statement to display the message, “MY first
program”:
Print (“MY first program”);
In the preceding statement, print
can be referred to as a keyword used in that programming language to display a
text on the screen. Further, as per the syntax of programming language, the
text that needs to be displayed is semicolon.
Compilers
Can computers directly interpret the instructions written in
any programming language? NO, they cannot. Then, how do you ensure a computer
interprets the instructions given in a program? For this purpose, a special
program known as a compiler is used. Consider a situation. You are in a foreign
country and trying to ask directions form a person who does not know your
language. How would you understand the directions?
You would need a person who can translate the direction
information into English or any other language that you can undersand. Similarly,
you need a translator to convert the instructions written in a programming
language into the machine language.
A computer is a special program
that processes the statements written in a particular programming
language and converts them into the machine language, at one go. Like
everything else in the computer, the compiler also follows the I-P-O cycle. It
takes the programming language instructions as an input. It then, processes
these instructions to convert them into the machine language. These instructions
can then be executed by the computer. This process of conversion is called
compilation.
0001110101
0100111100
1000101010
1010100010
1010101001
|
Compiler
|
EQUALS c
Programming language machine
language
For each programming language, there is a different compiler
available. For example, to compile a program written in the C language, you
require a C compiler; and for a java program, you require a java compiler.
Question a
------------is a special program that converts the programming language into
the machine language, at one go.
(Input-process output cycle)
(Central processing unit) this
is a answer= compiler?
(Compiler)
|
Tools used in problem solving
As seen, a program is a set of instructions to solve a
particular problem. In the process of writing a program, you need to derive a
technique to solve the problem. After deriving the technique, you need to
represent the technique in a way that is helpful in writing the program easily.
For this, you can various tools. The tools that you can use to represent the
technique to solve a problem are:
a)
Algorithm
b)
Flowchart
c)
Pseudocode
Algorithm
A.
An algorithm is a sequence of steps required to
solve a problem. It is a list of well-defined instructions to solve a problem.
According to D.E. Knuth, a pioneer in the field of computer science, an
algorithm has the following characteristics:
B.
An algorithm ends after a fixed number of steps.
C.
Each step in an algorithm clearly specifies the
action on be performed.
D.
The steps in an algorithm specify basic
operations. These operations can include calculations, input/output operations,
and comparisons.
E.
An algorithm accepts the input data, in a
defined format, before it can be processed.
F.
An algorithm generates one or more output after
the input is processed. The resultant information termed as output can be
displayed or stored for a future reference.
For example, when you approach a traffic signal while
driving a vehicle, the algorithm to take adecision will be:
Step 1: start the algorithm.
Step 2: approach traffic light.
Step 3: If the light is red, stop.
Step 4: if the light is yellow, slow down and then, stop
Step 5: otherwise, the light is green, continue driving.
Step 6: end the algorithm.
An algorithm follows the I-P-O cycle to solve a problem.
Here, the input is the color of the traffic light that is passed to your drain,
after seeing the traffic light. Information about the color is processed in the
brain. If the color is red, you need to stop. If the color is yellow, you need
to slow down and then, stop. Otherwise, you need to continue driving. Here,
your final action, after seeing the color, is the output.
Consider another example where you want to withdraw a
required amount from an ATM.
In this case, the algorithm will be.
Step 1: start the
algorithm.
Step 2: insert
your ATM card.
Step 3: the ATM
machine checks your card details.
Step 4: enter the
pin number.
Step 5: the ATM machine validates
the pin number.
Step 6: press the cash with
receipt button.
Step 7: select the amount you
want to withdraw.
Step 8: collect your ATM card.
Step 9: the ATM machine counts
the cash.
Step 10: collect the amount from
the ATM machine.
Step 11: collect the statement
from the ATM machine.
Step 12: end the algorithm.
Here, the input to withdraw a required amount from an ATM
would be the ATM card, pin code number, and amount to withdraw. These inputs
are that accepted, and the process of withdrawing the amount place. At the and
of the process, the amount, which is the output, is withdrawn.
Consider another example where a television company may
provide the following algorithm in the television manual to troubleshoot the
‘NO sound’ problem:
Step 1: start the algorithm.
Step 2: ensure that the mute
button is not pressed.
Step 3: ensure that the desired
volume is set.
Step 4: if the sound is still
inaudible, call the television engineer.
Step 5: end the algorithm.
Flowchart
A flowchart is a graphical representation of an algorithm.
It consists of a sat of symbols. Each symbol represents a specific kind of
activity depicted in the algorithm. A typical flowchart would involve accepting
instructions and data as inputs, processing these instructions and data and
displaying the result as an output. The processing would involve the
computations and the decision that are being taken.
For example, the following flowchart depicts how to take a
decision when you approach a traffic signal while driving a vehicle.
End
|
Stop
|
Slow down
|
Light is green,
continue driving
|
The flowchart to take a decision on a traffic signal
Advantages
of flowchart
Flowchart offers many advantages. Some of these advantages
are:
A.
Flowcharts help in analyzing the problems
effectively.
B.
Flowcharts act as a guide during program
development phase.
Disadvantages
of flowchart
Although a flowchart provides several advantages, it has a
few disadvantages as well. Some of these disadvantages are:
i.
A lengthy flowchart may extend over multiple
pages, which reduces readability.
ii.
Drawing a flowchart using any graphical tool is
a time-consuming process.
iii.
The changes made to a single step may cause
redrawing the entire flowchart.
Pseudocode
A Pseudocode is a detailed yet readable description of what
an algorithm must do. It is expressed in a formally styed natural language
rather than in a programming language. A Pseudocode is used as an initial step
in the process of developing. It provides the programming a detailed template
for writing instructions in a specific programming language.
For example, the following Pseudocode shows the process of
deciding the course of action when you approach a signal while driving a
vehicle.
Pseudocode for deciding
the course of action on a traffic signal
|
Begin
Character light, action
Accept light
If (light == ‘red’)
Begin
Action – ‘stop’
End
Else if (light == ‘yellow’)
Begin
Action = ‘slow down and then stop’
End
Else
Begin
Action = ‘continue driving’
End
|
Input process good h or mast bhi h
ReplyDeleteThanks
ReplyDelete