Chapter 1 INTRODUCTION TO PROGRAMMING Programming and Problem solving through C Language Page:1 Copyrights© 2008 BVU Amplify DITM.
Download
Report
Transcript Chapter 1 INTRODUCTION TO PROGRAMMING Programming and Problem solving through C Language Page:1 Copyrights© 2008 BVU Amplify DITM.
Chapter 1
INTRODUCTION
TO
PROGRAMMING
Programming and Problem solving through C Language
Page:1
Copyrights© 2008
BVU Amplify DITM
ALGORITHM
An algorithm is defined as the
finite set of steps to be followed in
order to solve the given problem.
Basically, it gives the set of
instructions
which
must
be
followed while solving the problem.
Programming and Problem solving through C Language
Page:2
Copyrights© 2008
BVU Amplify DITM
Example 1:
To find the largest of three numbers.
Algorithm:
Step 1: Read the three numbers, say A, B
and C.
Step 2: If (A>B) and (B<C) then A is the
largest number, go to step 4.
Step 3: If (B>A) and (A>C) then B is the
largest else C is the largest
number.
Step 4: Stop.
Programming and Problem solving through C Language
Page:3
Copyrights© 2008
BVU Amplify DITM
Example 2:
To find the average score of a student, for
the three test marks (i.e. three subject
scores).
Algorithm:
Step 1: Read the three test scores, say T1,
T2 and T3.
Step 2: SUM = T1+ T2+ T3.
Step 3: AVERAGE= SUM/3.
Step 4: Display Average
Step 5: Stop.
Programming and Problem solving through C Language
Page:4
Copyrights© 2008
BVU Amplify DITM
Example 3:
To find the area of a circle.
Algorithm:
Step
Step
Step
Step
1:
2:
3:
5:
Read the radius of the circle say r.
AREA = 3.14 * r * r.
Display Area.
Stop.
Programming and Problem solving through C Language
Page:5
Copyrights© 2008
BVU Amplify DITM
Characteristics of an Algorithm
Input:
An algorithm must be provided with any number
of input/data values.
Output:
As we go through the algorithm, step by step,
processing statements will yield some result. This
enables us to verify the algorithm. Hence at least
one output must be produced.
Programming and Problem solving through C Language
Page:6
Copyrights© 2008
BVU Amplify DITM
Definiteness:
Each statement must be very much clear,
and distinct. This ensures that the
statements must be unambigious.
Finiteness:
The algorithm must terminate after a finite
number of steps.
Programming and Problem solving through C Language
Page:7
Copyrights© 2008
BVU Amplify DITM
Let us illustrate this point with the help of an
example.
Algorithm:
Step
Step
Step
Step
Step
1:
2:
3:
4:
5:
Let a = 10.
If (a>10) then go to step 5.
X = Y * Z.
Print X and go to step 2.
Stop.
Programming and Problem solving through C Language
Page:8
Copyrights© 2008
BVU Amplify DITM
Let us not bother the purpose of above
algorithm, but simply try to trace it. Here
we notice that in the algorithm, nowhere the
value of ‘a’ is changed, which happens to
be controlling the flow and hence never
terminates. Such statements must be
avoided.
Effectiveness:
It must be possible in practice, to carry out
each step manually (using paper and
pencil).
Programming and Problem solving through C Language
Page:9
Copyrights© 2008
BVU Amplify DITM
Example 4
Algorithm for dividing two numbers.
Algorithm:
Step
Step
Step
Step
1:
2:
3:
4:
Read the two numbers say a, b.
c = a/b.
Print c.
Stop.
Programming and Problem solving through C Language
Page:10
Copyrights© 2008
BVU Amplify DITM
Now what happens to this algorithm, if
value of b is 0?
Yes, definitely your
“infinite”, right?
answer
will
be
Since the system doesn’t give you such
answer, hence a care must be taken
while you write the algorithm. One way
is to provide an appropriate error
message, like:
Programming and Problem solving through C Language
Page:11
Copyrights© 2008
BVU Amplify DITM
Modified Algorithm
Step 1: Read the two numbers say a, b.
Step 2: If (b = 0) then
print “denominator value is 0”
and go to step 5.
Step 3: c = a/ b
Step 4: Print c
Step 5: Stop
Programming and Problem solving through C Language
Page:12
Copyrights© 2008
BVU Amplify DITM
An algorithm can be expressed in many ways.
One way is to express it in natural language
(viz, English).
This way of representation helps to write a
program in any language, as it needs only the
conversion to respective languages.
Thus it appears more general. However, care
must be taken in such case to avoid ambiguity.
Another way, is to represent algorithm
graphically – a flowchart.
Programming and Problem solving through C Language
Page:13
Copyrights© 2008
BVU Amplify DITM
Algorithm Design Tools
1. First write the algorithm for a given
problem as if we are solving problem on a
paper.
2.Convert each statement of the algorithm
into its equivalent program statements
and then think of putting the control
statement.
We can make use of flowcharts for the
algorithm design.
Programming and Problem solving through C Language
Page:14
Copyrights© 2008
BVU Amplify DITM
Assignment
Given a list of five numbers
find the largest number in the
list.
Programming and Problem solving through C Language
Page:15
Copyrights© 2008
BVU Amplify DITM
Flowchart
It is a diagrammatic way of representing
the steps to be followed for solving the
given problem.
The main advantage of flowchart is that
since it is in the form of diagram one can
understand the flow of code very easily.
While drawing the flowcharts we make use
of certain symbols. These are shown below:
Programming and Problem solving through C Language
Page:16
Copyrights© 2008
BVU Amplify DITM
Flowchart Notation used
Symbol
Purpose
START/STOP
Assignment
Statements,
Expressions etc.
READ/PRINT
Programming and Problem solving through C Language
Page:17
Copyrights© 2008
BVU Amplify DITM
Flowchart Notation used
Symbol
Purpose
Decision making
Connector
Flow indication
Programming and Problem solving through C Language
Page:18
Copyrights© 2008
BVU Amplify DITM
Examples on Flowcharts
Example 1
Draw a flowchart to add two
numbers say ‘a’ and ‘b’. Store the
result in sum. Display the Print
“sum”.
Programming and Problem solving through C Language
Page:19
Copyrights© 2008
BVU Amplify DITM
Start
Read numbers
‘a’ and ‘b’
sum = a + b
Print the result
sum
Stop
Programming and Problem solving through C Language
Page:20
Copyrights© 2008
BVU Amplify DITM
Example 2
Draw a flowchart to find the
factorial of a given number.
Programming and Problem solving through C Language
Page:21
Copyrights© 2008
BVU Amplify DITM
Start
Read the number
= number
Yes
If number == 0
No
Initialize, Fact = 1
i=1
Print “ factorial for
0 is 1”
Fact = fact * i
i = i +1
Yes
If i<=number
Stop
Print “factorial of number
is “fact”
Programming and Problem solving through C Language
Page:22
Copyrights© 2008
BVU Amplify DITM
Stop
Assignment
Draw a flow chart to search a
number from a given list of n
numbers.
Programming and Problem solving through C Language
Page:23
Copyrights© 2008
BVU Amplify DITM
Pseudocode
Pseudocode generally does not
actually obey the syntax rules of any
particular language
There is no systematic standard form,
although any particular writer will
generally borrow style and syntax for
example control structures from
some
conventional
programming
language.
Programming and Problem solving through C Language
Page:24
Copyrights© 2008
BVU Amplify DITM
KEYWORDS
Several keywords are used to indicate
common input, output and processing
operations:
Input: Read, Obtain, Get
Output: Print, Display, Show
Compute: Compute, Calculate, Determine
Initialize: Set, Init
Add One: Increment, Bump
Programming and Problem solving through C Language
Page:25
Copyrights© 2008
BVU Amplify DITM
Example 1
Draw the flowchart and also give the
pseudocode to calculate the pay
Start
Read hours
Read rate
Pay = hours * rate
End
Programming and Problem solving through C Language
Page:26
Copyrights© 2008
BVU Amplify DITM
Pseudocode
Begin
input hours
input rate
pay = hours * rate
print pay
End
Programming and Problem solving through C Language
Page:27
Copyrights© 2008
BVU Amplify DITM
Assignments
Draw
the
pseudocode
problems:
flowchart
for
the
and
the
following
1.To find the sum of two numbers.
2.To find the average of three numbers.
Programming and Problem solving through C Language
Page:28
Copyrights© 2008
BVU Amplify DITM
IF-THEN-ELSE
Binary choice on a given Boolean condition is
indicated by the use of four keywords: IF, THEN,
ELSE, and ENDIF. The general form is:
IF condition THEN
sequence 1
ELSE
sequence 2
ENDIF
If the condition is true, sequence 1 is performed,
otherwise sequence 2 is performed.
Programming and Problem solving through C Language
Page:29
Copyrights© 2008
BVU Amplify DITM
Example
Draw the flowchart and also give the
pseudocode to calculate the pay with
overtime
Start
Read hours, rate
Is hours <= 40
No
Yes
Pay = hours * rate
Pay = 40 * rate +
(hours – 40) *1.5 * rate
End
Programming and Problem solving through C Language
Page:30
Copyrights© 2008
BVU Amplify DITM
Pseudocode
Begin
input hours, rate
if hours <=40 then
pay = hours * rate
else
pay = 40 * rate + (hours –
rate) * 1.5 * rate
endif
print pay
End
Programming and Problem solving through C Language
Page:31
Copyrights© 2008
BVU Amplify DITM
WHILE
The WHILE is used to specify a loop with a
test at the top. The beginning and ending
of the loop are indicated by two keywords
WHILE and ENDWHILE. The general form
is
WHILE condition
sequence
ENDWHILE
Programming and Problem solving through C Language
Page:32
Copyrights© 2008
BVU Amplify DITM
Example
Draw the flowchart and also give the pseudocode
to find the average of 10 numbers.
Start
i=1, sum=0
While i<=10
Yes
No
Avg = sum / 10
Input x
Print avg
sum = x + sum
Increment i
Stop
Programming and Problem solving through C Language
Page:33
Copyrights© 2008
BVU Amplify DITM
Pseudocode
Begin
i=1
sum=0
While i <=10
input x
sum = sum + x
i=i+1
end while
avg = sum/10
print avg
End
Programming and Problem solving through C Language
Page:34
Copyrights© 2008
BVU Amplify DITM
CASE
A CASE construct indicates a multiway branch
based on conditions that are mutually exclusive.
Four keywords, CASE, OF, OTHERS, and ENDCASE,
and conditions are used to indicate the various
alternatives. The general form is:
CASE expression OF
condition 1 : sequence 1
condition 2 : sequence 2
...
condition n : sequence n
OTHERS:
default sequence
ENDCASE
Programming and Problem solving through C Language
Page:35
Copyrights© 2008
BVU Amplify DITM
Example
CASE Title OF
Mr
Mrs
Miss
Ms
Dr
ENDCASE
: Print "Mister"
: Print "Missus"
: Print "Miss"
: Print "Mizz"
: Print "Doctor"
CASE grade OF
A
: points = 4
B
: points = 3
C
: points = 2
D
: points = 1
F
: points = 0
ENDCASE
Programming and Problem solving through C Language
Page:36
Copyrights© 2008
BVU Amplify DITM
REPEAT-UNTIL
This loop is similar to the WHILE loop
except that the test is performed at the
bottom of the loop instead of at the top.
Two keywords, REPEAT and UNTIL are
used. The general form is:
REPEAT
sequence
UNTIL condition
Programming and Problem solving through C Language
Page:37
Copyrights© 2008
BVU Amplify DITM
FOR
This loop is a specialized construct for
iterating a specific number of times, often
called a “counting” loop. Two keywords,
FOR and ENDFOR are used. The general
form is:
FOR iteration bounds
sequence
ENDFOR
Programming and Problem solving through C Language
Page:38
Copyrights© 2008
BVU Amplify DITM
Example
Write a Pseudode to find the average of 10
numbers using FOR loop.
Begin
sum = 0
for i = 1 to 10
input x
sum = sum + x
end for
avg = sum / 10
print avg
End
Programming and Problem solving through C Language
Page:39
Copyrights© 2008
BVU Amplify DITM
Programming Languages
Computers being electronic brains or having
artificial intelligence, it is still necessary for
humans to convey this sequence of instructions
to the computer before the computer can
perform the task.
Programming means designing or creating a
set of instructions to ask the computer to carry
out certain jobs which normally are very much
faster than human beings can do.
In order to do programming, we need to use
certain computer language to communicate
with the computer.
Programming and Problem solving through C Language
Page:40
Copyrights© 2008
BVU Amplify DITM
There are many computer languages out
there, some of the examples are C,
Visual Basic, Fortran, Cobol, Java, C++,
Turbo Pascal, Assembly language and etc.
The set of instructions and the order in
which they have to be performed is
known as an algorithm.
Programming and Problem solving through C Language
Page:41
Copyrights© 2008
BVU Amplify DITM
The
result
of
expressing
the
algorithm
in
a
programming
language is called a program.
The process of writing the algorithm
using a programming language is
called
programming,
and
the
person
doing
this
is
the
programmer.
Programming and Problem solving through C Language
Page:42
Copyrights© 2008
BVU Amplify DITM
Storage
Holds the data that the computer
will process and the instructions
that indicate what processing is to
be done.
In a digital computer, these are
stored in a form known as binary,
which means that each instruction
is represented by a series of bits.
Programming and Problem solving through C Language
Page:43
Copyrights© 2008
BVU Amplify DITM
Bits are conceptually combined
into larger units called bytes
(usually 8 bits each) and words
(usually 8 to 64 bits each).
Types of Storage device
1. Registers
2. Main memory
3. Secondary storage.
Programming and Problem solving through C Language
Page:44
Copyrights© 2008
BVU Amplify DITM
Registers
Registers are the fastest and most
costly storage units in a computer.
Normally
contained
processing unit
within
Registers hold data that are involved
with the computation currently being
performed.
Programming and Problem solving through C Language
Page:45
Copyrights© 2008
BVU Amplify DITM
the
Main Memory
Computer Memory is the internal storage
areas in the computer.
It comes in the form of chips capable of
holding data.
Two Types of Memory
1. ROM (Read Only Memory)
2. RAM (Random Access Memory)
Programming and Problem solving through C Language
Page:46
Copyrights© 2008
BVU Amplify DITM
ROM (READ ONLY MEMORY)
Date is prerecorded for read only,
which cannot be removed.
ROM is non-volatile.
Store critical programs such as the
program that boots the computer.
Programming and Problem solving through C Language
Page:47
Copyrights© 2008
BVU Amplify DITM
RAM (RANDOM ACCESS MEMORY)
Date can be read, write and remove
in any order.
RAM is volatile.
Caching
A small, fast memory system contains the
most frequently used words from a slower,
larger
main
memory.
Programming and Problem solving through C Language
Page:48
Copyrights© 2008
BVU Amplify DITM
Secondary Storage
Secondary storage is the slowest,
lowest-cost, and highest-capacity
computer storage area.
Programs and data are kept in
secondary memory when not in
immediate use, so that secondary
memory is essentially a long-term
storage medium.
Programming and Problem solving through C Language
Page:49
Copyrights© 2008
BVU Amplify DITM
Machine Language
For the first machines in the 1940s,
programmers had no choice but to write
in the sequences of digits that the
computer executed.
Machine language is a collection of
binary digits or bits that the computer
reads and interprets. Machine language
is the only language a computer is
capable of understanding.
Programming and Problem solving through C Language
Page:50
Copyrights© 2008
BVU Amplify DITM
Assembly Language
Since each component of a program stands for
an object that the programmer understands,
using its name rather than numbers should
make it easier to program.
By naming all locations with easy-to-remember
names, and by using symbolic names for
machine instructions, some of the difficulties of
machine programming can be eliminated.
A
relatively
simple
program
called
an
assembler converts this symbolic notation into
an equivalent machine language program.
Programming and Problem solving through C Language
Page:51
Copyrights© 2008
BVU Amplify DITM
High Level Language
High Level Languages (HLL) use words
English and are easier to write.
similar to
The first programming languages were developed in the
late 1950s.
If we want to compute |A + B − C|, and store the result
in a memory location called D, all we had to do was
write D = |A + B − C| and let a computer program, the
compiler, convert that into the sequences of numbers
that the computer could execute. FORTRAN (an
acronym for Formula Translation) was the first major
language in this period.
Programming and Problem solving through C Language
Page:52
Copyrights© 2008
BVU Amplify DITM
Edit-Compile-Link-Execute Process
Developing a program in a compiled
language such as C requires at least four
steps:
editing (or writing) the program
compiling it
linking it
executing it
Programming and Problem solving through C Language
Page:53
Copyrights© 2008
BVU Amplify DITM
Editing
Write a computer program with words and
symbols that are understandable to human
beings.
This is the editing part of the development
cycle.
Type the program directly into a window on
the screen and save the resulting text as a
separate file often called as source file.
Example: The text of a C program is stored
with the extension .C.
Programming and Problem solving through C Language
Page:54
Copyrights© 2008
BVU Amplify DITM
Compiling
Cannot execute the source code directly.
Source file must be translated into binary
numbers
understandable
to
the
computer.
Compiler converts the source file (.c for
C) into an object file with the extension
.obj.
Programming and Problem solving through C Language
Page:55
Copyrights© 2008
BVU Amplify DITM
Linking
Many languages come with library routines
which can be added to your program.
These routines are written by the manufacturer
of the compiler to perform a variety of tasks. So
these routines need to be added to your
program.
So the linker will convert the file with the
extension .obj to a file with the extension .exe
(executable file).
Programming and Problem solving through C Language
Page:56
Copyrights© 2008
BVU Amplify DITM
Executable File
Editor produces .c sources files.
The C source file are taken by the complier,
which will convert to a file with the extension
.obj.
The object file will be taken by the linker, which
will convert a file with extension .obj to a file
with extension .exe.
Now you can run .exe files simply by typing
their names.
Programming and Problem solving through C Language
Page:57
Copyrights© 2008
BVU Amplify DITM
Loader
Copying a program image from hard disk
to the main memory in order to put the
program in a ready-to-run state.
Testing
Testing means finding all possible bugs.
The program should be tested against all
possible inputs.
Programming and Problem solving through C Language
Page:58
Copyrights© 2008
BVU Amplify DITM
Debugging
Debugging is the process of analyzing
and locating the bugs when software
does not behave as expected.
Documentation
Write a brief and accurate comments at
the beginning of each function.
It becomes easy for other people
unfamiliar with the working of the
program.
Programming and Problem solving through C Language
Page:59
Copyrights© 2008
BVU Amplify DITM