[go: up one dir, main page]

0% found this document useful (0 votes)
242 views169 pages

Nuk

tr numerico

Uploaded by

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

Nuk

tr numerico

Uploaded by

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

Ethiopian Civil Service College

Institute of Urban Development Studies


Department of Urban Engineering

COMPUTER PROGRAMMING
& NUMERICAL METHODS
UE 261
Lecture Notes

By: Shimelis B. Dessu


Haileleul T.

Addis Ababa
Ethiopia

June 12
Ethiopian Civil Service College Computer Programming & Numerical Methods

GENERAL COURSE DESCRIPTION:


This course coverage is divided in two sections: Computer Programming and Numerical
Methods. The first part is introductory in nature and attempts to provide a basic review
of concept, theories and application of computer programming. The C++ programming
language will be used for demonstration. The second part focuses on how to develop, and
use computer algorisms in solving numerical problems.
COURSE OBJECTIVES:
The first part of this course is intended to enable LEARNERS:
 To appreciate the importance of computer in solving numerical engineering
problems,
 To understand the principles of programming and operation of computer
 To acquaint learners with the skill of planning, coding and execution of C++
programming language
 To utilize different techniques and algorithms in solving engineering problems.
The second part will enable LEARNERS to:
 Use the problem solving steps in program development
 Convert problems written in natural languages to computer languages using
pseudo-codes and flow charts
 Convert pseudo-codes and flow charts to programs in high level languages (such
as C++)
 Write and debug programs in high level languages
 Divide one big problem into many smaller problems
 Use modularization techniques to solve a problem in team
OUTLINE OF TOPICS:

As detailed in the Table of Content

Last Update: Jun. 23, 12 ii


Ethiopian Civil Service College Computer Programming & Numerical Methods

Evaluation
Evaluation: Instructor
Assignment (5x2%) 10 % Name: Shimelis Behailu (M.Sc.)
Project (3x10%) 30 % E-mail: shimelisbehailu@yahoo.com
Mid-Exam 20 % sbehailu@nilebasin.org
Final Exam 40 % Tel. +251-911-411357 [Mob]
Total 100 % +251-116-267552 [Res.]

Text Book:

References:
Computer Programming




Numerical Methods
 Numerical Methods for Engineers, S.C. Chapra, and R.P. Canale
 Numerical Methods for Engineers and Scientists, A.C. Bajpai, I.M. Calus and
J.A. Fairley
 Elementary Numerical Analysis, S.D. Conte and C. de Boor
 Numerical Recipes, by W. H. Press et al
 Numerical Analysis – Schaum’s Outline Series

Web URL
 http://www.bestsamplequestions.com/technical-questions/cpp-sample-questions/cpp-
sample-questions.html
 http://www.csse.monash.edu.au/~jonmc/CSE2305/index.html

Last Update: Jun. 23, 12 iii


Ethiopian Civil Service College Computer Programming & Numerical Methods

Table of Contents
PART ONE: COMPUTER PROGRAMING ..................................................................... 1
1. Introduction to Computer Programming ..................................................................... 1
1.1. Introduction ......................................................................................................... 1
1.2. Information/Data Transfer .................................................................................. 1
1.3. Computer Organization ....................................................................................... 2
1.4. What is programming? ........................................................................................ 3
1.5. Why do we learn programming?......................................................................... 6
1.6. Algorithm and Program ...................................................................................... 6
1.7. Classification of Programming Languages ......................................................... 7
1.7.1. Machine Languages .................................................................................... 7
1.7.2. Low-level Languages .................................................................................. 8
1.7.3. High-Level Languages ................................................................................ 8
1.8. Program Process................................................................................................ 10
1.9. Running a computer program ........................................................................... 11
1.10. Program Development .................................................................................. 12
1.10.1. Qualities of a good program ..................................................................... 12
1.10.2. Program development stages .................................................................... 14
1.11. Representation of Algorithms ....................................................................... 17
1.11.1. Representation Algorithms using Step form ............................................. 17
Example: ............................................................................................................... 17
1.11.2. Representing Algorithms using Pseudo-code ........................................... 17
Examples:.............................................................................................................. 20
1.11.3. Representing Algorithms using Flowchart ............................................... 24
Examples:.............................................................................................................. 26
1.12. Review Exercise and Assignment ................................................................. 29
2. Introduction to C++ .................................................................................................. 30
2.1. History of C++ .................................................................................................. 30
2.2. C++ Program Structure ..................................................................................... 31
2.3. Basic Elements of C++ Program ...................................................................... 33
2.4. Names ............................................................................................................... 34
2.4.1. Identifiers .................................................................................................. 34
2.4.2. Keywords .................................................................................................. 34
2.5. Comments ......................................................................................................... 35
2.6. Input/Output, I/O............................................................................................... 35
2.6.1. Input Statement ......................................................................................... 35
2.6.2. Output Statement ...................................................................................... 35
2.6.3. I/O manipulation ....................................................................................... 36
2.7. Variables ........................................................................................................... 37
2.7.1. Data types.................................................................................................. 37
2.7.2. Declaration of Variables ........................................................................... 37
Integers .................................................................................................................. 38
Real Numbers: ...................................................................................................... 39
Characters: ............................................................................................................ 40
String ..................................................................................................................... 41
2.7.3. Scope ......................................................................................................... 41
2.8. Constants ........................................................................................................... 42
2.9. Special characters (Escape sequences) ............................................................. 43
2.10. Review Exercise............................................................................................ 44

Last Update: Jun. 23, 12 iv


Ethiopian Civil Service College Computer Programming & Numerical Methods

3. Expressions in C++ ................................................................................................... 45


3.1. Arithmetic Operators ........................................................................................ 45
3.2. Relational Operators ......................................................................................... 46
3.3. Logical Operators.............................................................................................. 46
3.4. Bitwise Operators.............................................................................................. 47
3.5. Increment/Decrement Operators ....................................................................... 48
3.6. Assignment Operator ........................................................................................ 48
3.7. Compound Assignment operators ..................................................................... 49
3.8. Conditional Operators ....................................................................................... 49
3.9. Comma Operator ............................................................................................... 50
3.10. sizeof Operator ......................................................................................... 50
3.11. Operator Precedence ..................................................................................... 51
3.12. Type Conversion ........................................................................................... 51
3.13. Review Exercise and Assignment on C++ Expressions ............................... 52
4. Program Control Statements in C++ ......................................................................... 53
4.1. Simple and Compound Statements ................................................................... 53
4.2. Selection Statement ........................................................................................... 54
4.2.1. If statement ............................................................................................ 54
If … else statement: ....................................................................................... 54
4.2.2. Conditional expression.............................................................................. 55
4.2.3. Switch Statement ................................................................................... 56
4.3. Iteration (or Looping Statements) ..................................................................... 57
4.3.1. While statement ...................................................................................... 57
4.3.2. Do…While statement............................................................................. 57
4.3.3. For statement ........................................................................................... 58
4.4. Jump statement.................................................................................................. 59
4.4.1. continue Statement .............................................................................. 59
4.4.2. break Statement...................................................................................... 60
4.4.3. goto Statement ........................................................................................ 61
4.4.4. return Statement ................................................................................... 61
4.5. Review Exercise and Assignment on C++ Statements ..................................... 62
5. Array ..................................................................................................................... 63
5.1. Static Array Implementation ............................................................................. 63
5.1.1. One dimensional array .............................................................................. 63
5.1.2. Multidimensional Arrays .......................................................................... 65
5.2. Review Exercise and Assignment ..................................................................... 66
6. Functions ................................................................................................................... 67
6.1. Modularization .................................................................................................. 67
6.2. Pre-defined functions ........................................................................................ 71
6.3. User defined Functions ..................................................................................... 72
6.3.1. Defining a user-defined functions ............................................................. 72
6.3.2. Using a user-defined functions ................................................................. 73
6.4. Function Prototypes .......................................................................................... 77
6.5. Writing Test Drivers ......................................................................................... 78
6.6. Visibility and Lifetime of Variables ................................................................. 82
6.6.1. Local Variables: ........................................................................................ 83
6.6.2. Global Variables: ...................................................................................... 83
6.6.3. Static Variables ......................................................................................... 86
6.7. Function Parameters.......................................................................................... 87

Last Update: Jun. 23, 12 v


Ethiopian Civil Service College Computer Programming & Numerical Methods

6.7.1. Default Parameters .................................................................................... 87


6.7.2. Passing Parameters.................................................................................... 92
6.8. Overloading Functions ...................................................................................... 97
6.9. Inline Functions .............................................................................................. 100
PART TWO: NUMERICAL METHODS ...................................................................... 103
1. Number System and Error Analysis in Numerical Methods .................................. 103
1.1. Introduction ..................................................................................................... 103
1.2. The Error Problem .......................................................................................... 104
1.2.1. Elementary theory of errors .................................................................... 104
1.2.2. Absolute and relative errors .................................................................... 104
1.3. Number Representation in the Computer ....................................................... 105
1.3.1. Fixed Point Representation: .................................................................... 105
1.3.2. Floating Point Representation: ................................................................ 106
1.4. Number Storage within the Memory of the Machine ..................................... 107
1.5. The Rounding off Problem ............................................................................. 107
1.5.1. Chopping: ................................................................................................ 107
1.5.2. Symmetrical Rounding off :.................................................................... 107
1.6. Numerical Errors ............................................................................................. 108
1.6.1. Error Committed by Chopping ............................................................... 108
1.6.2. Error Committed by Symmetrical Rounding off .................................... 109
1.6.3. Relative Error: ......................................................................................... 110
1.7. Computational Problems and Algorithms ....................................................... 110
2. Solutions of Non-Linear Equations ........................................................................ 113
2.1. Introduction ..................................................................................................... 113
2.2. Methods Used in Root Finding ....................................................................... 113
2.3. The Bisection Method ..................................................................................... 114
3. Interpolation and Approximation ............................................................................ 116
3.1. Introduction ..................................................................................................... 116
3.2. Polynomial Interpolation ................................................................................ 116
3.3. Rational Function Interpolation ...................................................................... 119
3.4. Splines and Cubic Spline ................................................................................ 121
3.5. Interpolation in Two or More Dimensions ..................................................... 123
3.5.1. Using higher order Interpolation for accuracy ........................................ 123
3.5.2. Higher order for smoothness ................................................................... 123
3.6. General Flowchart ........................................................................................... 127
References ....................................................................................................................... 127
References ....................................................................................................................... 128
Annex .............................................................................................................................. 129
I. Solution to Review Exercises & Assignments........................................................ 129
I.1. Introduction to Programming.......................................................................... 129
I.2. Introduction to C++ ........................................................................................ 131
I.3. Expressions in C++ ......................................................................................... 134
I.4. Statements in C++ ........................................................................................... 135
I.5. Arrays .............................................................................................................. 140
II. Review Questions ................................................................................................... 141
II.1. Introduction to Programming.......................................................................... 141
II.1.1. Algorithm Design and Representation .................................................... 141
III. Selected Group Activity Reports ........................................................................ 147
III.1. Projects ........................................................................................................ 160

Last Update: Jun. 23, 12 vi


Ethiopian Civil Service College Computer Programming & Numerical Methods

List of Tables
Table 2-1 List of some C++ keywords ............................................................................. 34
Table 2-2 Data types and their value range ...................................................................... 37
Table 2-3 List of Escape sequences in C++ (Special characters) ..................................... 43
Table 3-1 C++ Arithmetic operators ................................................................................. 45
Table 3-2 Relational operators .......................................................................................... 46
Table 3-3 Logical operators .............................................................................................. 46
Table 3-4 Bitwise operators .............................................................................................. 47
Table 3-5 How the bits are calculated in 8-bit machine ................................................... 48
Table 3-6 Increment and decrement operators.................................................................. 48
Table 3-7 Compound Assignment operators .................................................................... 49
Table 3-8 Operator precedence levels............................................................................... 51

List of Figures

Figure 1-1 Block Diagram of Computer organization ........................................................ 2


Figure 1-2 Stages in programming ................................................................................... 10
Figure 1-3 Illustration of typical programming process ................................................... 11
Figure 1-4 Program development life-cycle ..................................................................... 14

Last Update: Jun. 23, 12 vii


Ethiopian Civil Service College Computer Programming & Numerical Methods

PART ONE: COMPUTER PROGRAMING


1. Introduction to Computer Programming
1.1. Introduction
Having computers around us make our life better. You know what a computer is. It is a
device that will always be characterized by its two main components called the hardware
and the software. The hardware is the part of the computer that we see and touch. The
software is the most mystical part of the computer since it cannot be seen and touched but
has a great part. The hardware without the software is nothing but a set of equipments
that are inactive and lifeless.

As an end user, one can buy a PC, Personal Computer, and use it by loading the
necessary software. We have been using sophisticated word processing programs,
spreadsheet software, engineering design software and many more. We’ve been enjoying
all the pleasant features of these programs. But today, let’s think about one question.
Who is behind these programs?

In this regard, some people give all the thanks to the computer itself. These people feel
that computer is a magic box sensing its environment and capable of performing anything
by itself once it is constructed and be a computer. They think that the most venerable
people are the people that construct the hardware part of the computer system. They are
not able to see that software is also playing a significant role in making the computer a
computer. Of course, we couldn’t blame them since they should see what they could see.
But what is the realty?

The answer to the previously raised question is very simple. It is the programmer who is
behind these sophisticated programs. Programmers are people who prepare and write set
of instructions to solve a given problem. Unless these people prepare the set of
instructions which will be followed by the computer to solve every individual problem,
no one could use the computer to perform any of the tasks.

1.2. Information/Data Transfer


Information or data transfer takes place between human beings for different purposes. If
there is no any communication between you and your environment, then what will
happen to you? This might be an extreme case. Let’s think of a simpler condition. Even
what happens if you go to a place where you don’t really know the language that the
people there are using to communicate? What if you are thirsty or hungry? You might
answer that you are going to use signs. It is wonderful to express your needs using signs
if the other party can fully understand you. But still, you may not be able to express
everything that you want to say.

Here is the conclusion: Communication is very important. Communication between


human beings is achieved by using human languages and signs. Both methods can be
considered as languages. However, the information transfer between human being is not
perfect. Your friend might understand what you say oppositely and be disappointed. The

Last Update: Jun. 23, 12 1


Ethiopian Civil Service College Computer Programming & Numerical Methods

things that you are saying may not tell to the other party the correct message. In short,
man-to-man communication is contextual. It depends on the current situation and
understanding of the other party.

But if we consider the information transfer between man and the machine (computer),
then it is more consistent and less ambiguous. The tools used in this case are
programming languages. Programming languages have limited number of words and
rules. To instruct the computer, you need to use one of the existing programming
languages correctly. ‘Correctly’ implies by following the keywords and the rules strictly.
Example of programming languages are BASIC, ForTran, Pascal, C, C++, Visual Basic,
C#, etc.

Programming languages differ from human languages in that:


They have strict grammatical rules.
They have a fixed set of words, known as keywords, each with an exact
predefined meaning.
Compared to human languages, they have small number of words.
From these one can see that programming languages are not ambiguous.

1.3. Computer Organization


All computers have a common internal organization, shown in the block diagram in
Figure1.1.

External
Memory

Internal
memory

Input Output
Processing unit

ALU

CPU

Figure 1-1 Block Diagram of Computer organization

The processing unit or processor is the part of the computer that controls all the other
parts. The processor accepts input values and stores them in the memory. It also interprets

Last Update: Jun. 23, 12 2


Ethiopian Civil Service College Computer Programming & Numerical Methods

the instructions in a computer program. If we want to add two values, the processor will
retrieve them from the memory and send them to the arithmetic logic unit or ALU. The
ALU performs the desired addition, and the processor then stores the result in the
memory. If we desire, we may also direct the processor to print the result on paper. The
processing unit and the ALU use a small amount of memory, the internal memory, in
their processing; most data is stored in external memory. The processor, internal memory,
and ALU are collectively called the central processing unit or CPU. Thus, a
microprocessor is a CPU, but a microcomputer is a CPU with input and output
capabilities.

1.4. What is programming?


A computer program is a sequence of instructions that a computer has to perform in
order to carry out a certain task. Various programs have been developed in the past that
are usually meant for different purposes. When working with computers, you will often
hear the terms software and hardware. Software refers to the programs that direct
computers to perform operations, compute new values, and manipulate data. Hardware
refers to the physical components of the computer, such as the memory unit, the
processor, and the ALU. A person who works with software might write computer
programs, and a person who works with hardware might design new components or
connect devices. For example, a hardware engineer might design the interface equipment
necessary to connect a microprocessor to an input terminal.

The following are few of the software that are in common circulation:
o Word processors: MS Word, WordPerfect
o Spreadsheets: MS Excel, Lotus123
o Computer aided design: AutoCAD, SAP
o Geographic Information System: ArcInfo, ArcView, IDRISI
Programs are written and fed into the computer in a certain specific rules. These rules,
which must be followed in writing a program make up the programming language. Read
and understand the following analogy example:
Example 1:
Suppose that there is a boy named Bob. Bob can understand only some instructions, but
amazingly Bob can do anything if you can instruct him using only those instructional
words. Assume that Bob also knows some other common descriptive English words.
Note that each job should be disintegrated into instructions, which can be described using
only these words. The instructions with their meanings (for Bob) are the following:
Instruction Meaning/description
Ask Inquire the stated thing.
Record Put (save) it in your mind.
Add Add the given two numbers.
Subtract Subtract two given numbers.
Multiply Multiply the given numbers.
Divide Divide the given numbers.
Speak out Speak the mentioned thing.

Last Update: Jun. 23, 12 3


Ethiopian Civil Service College Computer Programming & Numerical Methods

Let us instruct him now to do different jobs:

Job 1: Say Bob should add two numbers for your friend. Instruct him (tell him the right
things to do, in their right order with only the words he can understand) to perform this
job.
Instructions:
What shall you tell him to do first?
He must get the numbers to be added. Where can he get this information? It is just from
your friend and to have these numbers, he has to ask your friend to tell him. How? By
which word can you instruct him? Ask is the word used to tell Bob to ask something.
Therefore, the first instruction has to be:
Ask like ‘Tell me the two numbers’.
As your friend tells him the numbers, Bob should be able to record the numbers in his
mind to add them later. So for Bob you have to tell him to record the numbers. And the
second instruction will be the following:
Record the two numbers.
And the other instruction can be followed as the following:
Ask like ‘Tell me the two numbers”.
Record the two numbers.
Add the numbers.
Record the sum.
Speak out the sum.
Job 2: I need this: Bob will take from me two numbers and tell me the sum of the
numbers, the result when the second number is subtracted from the first one, the product
of the numbers and the quotient found when the first number is divided by the second
one.
Instructions:
You may instruct Bob as the following:
Ask her two numbers
Record the numbers
Add the numbers
Record the sum
Subtract the second number from the first one.
Record the difference
Multiply the numbers
Record the product
Divide the first number by the second one
Record the quotient
Speak out sum
Speak out the difference
Speak out the product
Speak out the quotient
OR

Last Update: Jun. 23, 12 4


Ethiopian Civil Service College Computer Programming & Numerical Methods

Ask her two numbers


Record the numbers
Add the numbers
Record the sum
Speak out t sum
Subtract the second number from the first one.
Record the result
Speak out result
Multiply the numbers
Record the product
Speak out product
Divide the first number by the second one
Record the quotient
Speak out quotient

Both set of instructions have the same final output, but their approach is different. So, one
can have another approach to get the same output. This shows us problems (jobs) can be
solved using different methods. We will discuss the significance of this truth later.

Try the following jobs:


Job 3: Let him ask for a, b and x from the user and tell the value of y in the following
equation, y = ax + b.
Job 4: In his fourth job, Bob has to find the average of three numbers for me.
Job 5: Given the base and height of a right angle triangle, Bob will find the area of the
triangle.
Job 6: Bob will find the perimeter of a square.
If there is any possibility to store these instructions in Bob’s mind (let’s hope there is)
then, there is no need to tell Bob these instruction repeatedly, but it would be enough just
to provoke him with some predefined action for each particular job so as to make Bob
carry out the set of the instructions starting from the very first one up to the last. Now it is
time to put side by side the example about Bob and computer programs and
programming:

Here Bob is the computer, you (who tells (prepare and store) him the instruction in their
right order) are the programmer, and the one who initiate Bob with the help of some
action is the end user of the program. The process of telling him the instructions is
programming.
From Bob Instructions to Computer Programs
Computers are just like Bob (may be primitive than him) but obedient enough to listen to
instructions and perform them with a great speed and accuracy. Because of their speed
and accuracy, they seem to be special (of course they are). If the output of a certain
computer program has an error, then 99% the problem should be from you, either from
the instructions stored or the entered user’s data. If you instruct Bob to ask the question,
‘kick me once’, to the user and expects Bob to say, ‘Enter the first number’, then that is
your problem. Laugh at yourself and edit the instructions or if you are the end user then

Last Update: Jun. 23, 12 5


Ethiopian Civil Service College Computer Programming & Numerical Methods

edit your input. Bob is always volunteer and ready. So, as a programmer, all your
instructions should be clear and unambiguous.

1.5. Why do we learn programming?


It is now very easy to understand that programs are kind of bridge between the end user
and the computer specifically the hardware. Whatever it is done by using the computers,
it is done because there are programs written for it.

So, we learn programming because it is still our job to solve problems and present the
solutions in a form which is completely understandable for the computer. Actually,
solving problems is the most difficult part. But it is also necessary to present solutions
using instructions written in programming languages.
You might say
“Why, in the world, we trouble ourselves to solve our problems, then again to
write instructions in programming languages and ask (may be beg) the
computer to carry out the instructions, after we have been through much to
solve the problems?”
It is because computers are fast, accurate, and efficient than us. Once problems are solved
and presented for the computer, the computer can repeatedly work on it efficiently, with
high speed and accuracy. The other reason is that all people cannot be programmers. The
available programmers must solve problems and prepare instructions for the computer to
solve problems for large number of end users. Definitely, the number of end users will be
greater than the number of programmers. Hence, programmers are responsible for the
preparation of programs for the end users.

You need to know something. Programming is very interesting but it has also challenges.
When you program, you will fight to hide the ‘pseudo intelligence’ of the computers by
making them to speak, listen, communicate and react with a very good user-interface.

1.6. Algorithm and Program


Any programmer when prepared to write a program, should first think thoroughly about
the problem and come up with the best, reliable, simpler, and efficient way to solve it.
This stage is called ‘preparing an algorithm’. An algorithm is a series of step-by-step
procedures for accomplishing a task. In Bob’s instruction to perform Job 2 (above), I
have presented two different sets of instructions. And we have concluded something –
“Everyone has his/her own way to perform something”. Therefore, algorithms depend on
one’s idea. In fact, there is always the best and efficient algorithm for particular
problems.
So, what is the difference between an algorithm and a program?
Algorithms may not necessarily be written as programs with a language understood by
the computer – but just by your own words or by using any other methods like flowcharts
and pseudo-code (combination of computer’s language with our natural language).

Last Update: Jun. 23, 12 6


Ethiopian Civil Service College Computer Programming & Numerical Methods

But programs are set of instructions which are presented using one of the programming
languages using the keywords and following the rules.

1.7. Classification of Programming Languages


There are varieties of programming languages. Basically, they can be divided into three
as:
1. Machine Languages
2. Low-level Languages
3. High-Level Languages
We will see each of them turn by turn.

1.7.1. Machine Languages


You probably easily see what kind of languages they might be when you see the name
itself. Machine languages are original languages of the computer. From your basic
understanding, computers directly understand only 0’s and 1’s.That is right! Since
machine languages are the languages of the computers, they consist of machine
instructions composed of strings of 0’s and 1’s.

A machine instruction usually has got two parts namely the opcode (operation code) and
the operand addresses. The opcode specifies the thing that should be done and the
operand addresses will lead to the operands needed to the function specified in the
opecode.

Example: 11000001

This is an eight-bit machine instruction. The first three digits (110) represent the opcode
which is the code for the move function in this particular case. The move function
instructs the computer to move some data from one place to another. Since moving needs
the specification of two places (source and destination of the data to be moved), we
expect to have two operands. Consequently the next two digits (00) represent the first
operand and the other remaining three bits (001) represents the second operand. In this
case, both operands are registers, which are very small memory cells found within the
CPU. 00 is the code for the register named AX and 001 is for BX. Finally when this
instruction is translated and practiced, data which was saved in BX will be moved to AX.

Obviously, machine languages are not completely pleasant. Some of the limitations and
problems of machine languages are given below:
1. Programming with machine languages is very boring.To write a program that can add
two integers using machine language, you might sit, bend your head and scan the
opcodes and operands used in each instruction. It is a must to remember the
opcode of the different limited number of functions and you have to keep track
of the storage locations of data.
2. It is an error-prone process. No one blames for frequent errors when you are writing
programs in machine languages. If you simply forget a 0 or a 1, then that is going
to be a big bug.

Last Update: Jun. 23, 12 7


Ethiopian Civil Service College Computer Programming & Numerical Methods

3. Maintenance is very difficult. Maintenance refers to actions taken to correct errors, to


modify the program because of different reasons etc. Therefore for programs
written in machine languages it is very difficult to find errors and to modify it.
1.7.2. Low-level Languages
Computers still understand only 0’s and 1’s. But it is not fair and tolerable to leave
machine languages as the only tools to communicate with the computer, as they are
inconvenient to write programs. Therefore, to avoid this complexity and burden of
programmers, some simple mnemonic codes were developed to describe machine
instructions. Mnemonics are group of letters (symbols) used for writing a program. The
set of these simple codes constitute a low-level language.
Example: Add Ax,Bx
This instruction instructs the computer to add the content of the registers Ax and Bx and
put the sum in register Ax.
If you try to see the equivalent machine language instruction of the above low level
language instruction (in 8088/86 computers) it will be as follows: 10100001.

It is very clear that remembering this code is more difficult and unpleasant than the
previously given low level language instruction (Add Ax,Bx). Therefore, the main
difference between low-level languages and machine languages is that machine
languages use binary codes (0’s and 1’s) where as low-level languages use symbolic
language.

But it doesn’t mean that the computers started to understand directly low-level language.
They still understand directly only machine language. Therefore, there will be a
translation process from low-level instructions to machine instructions. This translation is
done by using a special translation program.
Limitations of low-level language include:
1. It is machine dependant. Assembly language is a very good example of low-level
languages. It is totally machine dependant meaning that it works only on machines of
particular type. As an example, take Assembly 8088/86 which works only on machines
having microprocessor of the 8088/86 family.
2. Even if the instructions are easier to work with (relative to machine language), many
instructions are still needed to perform a very simple task. This is because the
correspondence between machine languages instructions and low-level
languages instructions is one-to-one.
1.7.3. High-Level Languages
Because of the limitations of low-level languages, programming was still a very tough
task. Eventually, low-level languages had to be replaced by other convenient languages.
What we said happened and the languages are called High-level programming languages.

As high-level programming languages were the result of the complaint of inconvenience


of low-level languages, they are very much better for the programmer. In these

Last Update: Jun. 23, 12 8


Ethiopian Civil Service College Computer Programming & Numerical Methods

languages, you will have set of keywords which look like English words. In addition to
that, there will be different rules to use the available commands (keywords).
Example:
Pascal is one of the high-level programming languages. Try to see the following small
program written in Pascal to observe that the keywords used have similarity with English
words.
Program First
Begin
Writeln (“This is my first program”);
Write (“Thank you”);
End.
The first line of this program shows the name of the program. The next line remarks the
beginning of the program. The third and the fourth lines will print the texts specified on
the screen, i.e. what ‘write’ or ‘writeln’ (read as write line) means in Pascal. The last line
shows the end of the program. Additionally, this program consumes only five lines where
as if the same program is rewritten in Assembly, it might take more than 10 lines
(instructions).

Other examples of high-level programming languages are ForTran, COBOL, BASIC, C,


C++, Visual Basic, Java, etc.
Advantages of high-level languages:
1. They are easier to learn and understand.
2. Less time is required to develop the programs and maintenance (modification) is
easier.
3. It is completely machine independent or else programs written in high-level
programming languages can be run in slight modifications on different types of
machines.
Drawbacks of high-level languages:
1. Efficiency of programs will decrease. This is because when coming to high-level
languages, we are being far from the machine. The machine still understands only
machine languages and we are developing programs in high-level languages. There
is a big gap between these two language types. So the translation process will be
longer and this takes some time to suppress the overall speed and efficiency of the
program.

2. In most cases, it does not allow to program system software such as operating
system. This is because when using high - level language the programmer has no
direct control over the memory locations, which is required in system software
development. In case of machine languages (or low-level languages) you can
easily keep track of the RAM of the system. For example Mov [1000],ax will copy
the data in the register Ax to the memory location 0004. When using high-level
languages and when copying and moving data, it is the operating system that
tracks the memory for your program.

Last Update: Jun. 23, 12 9


Ethiopian Civil Service College Computer Programming & Numerical Methods

1.8. Program Process


Programs written in high-level or low-level languages need translation before the
computer can process them. These programs will take three stages when they are
processed. The three stages are:

Machine Dependent
Machine Independent

+ other object

+ test data
+ Libraries

OS loads
Assembler
Compilers

modules
Links
Source
Object Executable
code Output
Code file

Link Runtime
Syntax error
errors errors
+ Warnings

Figure 1-2 Stages in programming

a. Translation: Translation consists of interpreting the instructions of the programs


(which were written in low-level or high level) into machine language instructions. This
translation is done by special translating programs. The original program written in high-
level or low-level languages is called the source program (Source code) and the
translated program is called the object program (Object code).
There are three types of translating programs:
i. Assembler: This is a program used to translate source programs written in Assembly
into its equivalent object code.

ii. Compiler: compilers translate high-level language source code into their equivalent
object code. Compilers translate all the source code into an object code before loading the
program into RAM so that it can be executed.

iii. Interpreter: this one has the same function as compilers. The difference here is the
way the translation is handled. The translation will be instruction by instruction. A single
instruction will be taken and translated into an object code instruction and automatically
loaded into the RAM. Then, the next instruction will continue.

b. Loading and Linking: The translated object program will be loaded into the RAM so
that it could be available for the CPU. If there are any separate program units (like the
different built in header files of C++), they will also be linked in this stage to form a
single complete executable program.

Last Update: Jun. 23, 12 10


Ethiopian Civil Service College Computer Programming & Numerical Methods

c. Execution: The instructions of the loaded program will be performed or processed by


the processor (CPU).

Source
Program

Assembler or
Compiler
Object
Code

The time taken in the


compilation is called the
compile time.

The time during which the CPU


object program is executed
is known as run time

Figure 1-3 Illustration of typical programming process

Note: In C++, the source code will be saved with extension .cpp. Then when the
compiler compiles the source code, it will produce a file with same filename but with
different extension which is .obj. This is the object code file. Then the linker will load
and link the object code to create a file with the same name but with extension .exe. This
is the executable file which means the file containing the code which is ready to run. It is
the executable file which will be executed (processed) by the CPU.

1.9. Running a computer program


A compiler is a program that translates ahigh-levellanguage to machine language. This
compilation step is the first step in running a computer program. As the compiler
translates statements, it also checks for syntax errors. Syntax errors, also called compiler
errors, are errors in the statements themselves, such as misspellings and punctuation
errors. If syntax errors (often referred to as "bugs") are found, the compiler will print
error messages or diagnostics for you. After correcting the errors ("debugging"), you
must rerun your program, again starting with the compilation step.

Once you have compiled your program without errors, a linkage editor program performs
the final preparations so that it can be submitted to the execution step. It is in the
execution step that the statements are actually performed. Errors can also arise in the
execution step; these are called logic errors, run-time errors, or execution errors. These

Last Update: Jun. 23, 12 11


Ethiopian Civil Service College Computer Programming & Numerical Methods

errors (also called "bugs") are not in the statement syntax but are errors in the logic of the
statements, which are detected only when the computer attempts to execute the statement

1.10. Program Development


Now, we are convinced that communication between man and the computer is necessary
to instruct the computer and this can be accomplished by using programming languages
which are referred as more consistent and less ambiguous than human languages.

In short, programmers should develop programs. But all programs are not good enough.
Therefore, programmers should follow certain steps, procedures and strategies to produce
best programs as much as they can. A computer stream that studies about these
techniques of developing programs is called software engineering.

1.10.1. Qualities of a good program


There are some factors to evaluate programs. Programs should be:
a. Reliable: The program must perform what it intends to perform. Additionally, it should
handle all possible circumstances (invalid and extreme cases).
Example: Assume a program is developed to produce the sum of three numbers.
The expected output of the program must be correct. If the three
numbers are 2, 4,-7 then the output has to be -1.
What about handling all possible cases? For instance, the program
should have some way (like producing an error message) to manage
the situation when an alphabetic letter is entered as an input instead of
one of the numbers. If the programmer didn’t consider this kind of
situations, the program will fail easily.
b. Maintainable: Most of the time, programs could be needed to be modified. This need
can arise from unsuspected error or from the inquiry of end users. At this point, the
program must be easily adjustable.

c. Portable: Sometimes programs running correctly in one type of computers might not
be able to run in other systems. As much as possible, the program must be easily portable
with a little modification.

d. Efficient: The resources we have in a computer system are scarce. The available
memory space and the CPU time are some examples of resources which are inadequate.
Your program should not waste memory space by reserving memory space for unused
values. Moreover, the program must not waste the CPU time by executing unnecessary
instructions. It has to avoid needless repetitions.
Example (on efficiency of programs):
Let’s go back to the former example about Bob. Consider the first job given to
Bob which was adding two numbers for your friend.

Ask like ‘Tell me the two numbers’. This was the set of instructions which we
Record the answer of my friend. were convinced to tell Bob for getting done
Add the numbers. the given task
Record the sum.

Last Update: Jun. 23, 12 12


Ethiopian Civil Service College Computer Programming & Numerical Methods

Multiply the sum by 1.


Record the product.
Speak out the Product.
Please look at the next set of instructions which are also told to Bob to do the same
thing.
Ask like ‘Tell me the two numbers’. What will be the final answer you expect
Record the two numbers. after telling Bob these instructions? The
Add the numbers. same answer like the above one or a
Record the sum. different one?
Speak out the sum.

Consider that your friend gave the two numbers as 9 and 6 and their exact conversation
was as the following:
Bob: “Tell me the two numbers” {According to the first instruction}
Your friend: “Thank you very much Bob. My numbers are 9 and 6”

The first instruction in both set of instructions is the same. But the other instructions
differ. So, we better execute them separately as follows:
The First Set The Second Set
nd nd
2 Inst: Record the two numbers 2 Inst: Record the answer of my friend
Bob will keep in his mind only the Bob will keep in his mind all the word of your
numbers 9 and 6. friend’s reply. This constitutes 9 words and 2
numbers (9 and 6).
3rd Inst: Add the numbers
3rd Inst: Add the numbers
Bob will add 9 and 6.
Bob will add 9 and 6.
4th Inst: Record the sum
th
4 Inst: Record the sum
Bob will keep in his mind the sum
which is 15. Bob will keep in his mind the sum which is 15.
5th Inst: speak out the sum 5th Inst: Multiply the sum by 1
Bob will tell your friend the Bob will multiply 15 by 1.
number 15. And that is the final th
6 Inst: Record the product
answer.
Bob will record in his mind the product, i.e. is 15.
7th Inst: Speak out the product
Bob will tell your friend the number 15. And that
is the final answer.

Both set of instructions yield the same output which is the expected one. But the second
set is not efficient. To be sure of this idea, take the second instruction. Is it really
necessary to store all the words that your friend replies? This really doesn’t have any
advantage but wastes space in Bob’s mind. The only data that Bob needs are the numbers
9 and 6.

And take the 5th and 6th instructions. Why do you need to multiply the sum by 1 and to
record the product? These are unnecessary instructions. Even if these instructions don’t

Last Update: Jun. 23, 12 13


Ethiopian Civil Service College Computer Programming & Numerical Methods

have any harm and affect the final output, they take the time of Bob which he can use to
do other things.

Therefore, this program is inefficient. Put side by side the space of Bob’s mind and the
memory space of the computer system. Since both have a limited size, they should be
used efficiently. Similarly the time of Bob and the CPU time can be interrelated since
time is another precious resource in every case. Using time effectively implies end users
get a response with in a short period of time.

1.10.2. Program development stages


To produce a program with the above qualities, you need to follow steps and techniques
used to develop programs or software. There are five stages as shown in Figure 1-4:

Problem
Definition
& analysis

Maintenance Program Algorithm


Development Development
Life-Cycle

Test &
Debugging Coding &
Documentatio
n

Figure 1-4 Program development life-cycle


1. Problem Analysis, Problem Identification and Requirement Analysis
This is the very first stage in any program development. Knowingly, or unknowingly
every programmer goes through this step.

In this stage, the main thing to do is to understand the given problem. Problem
identification simply means understanding what to be done. Ask yourself this question:
What must I do?
What is needed or expected?
Specifically, the things that should be known at the end of this stage are:
i. Identifying input data to the problem
ii. Identifying output data from the problem
iii. Identifying procedures needed to achieve the result.
Note: in this stage the thing that must be worked out is what must be done not how to do
it.
2. Algorithm Design

Last Update: Jun. 23, 12 14


Ethiopian Civil Service College Computer Programming & Numerical Methods

When you pass to this stage, you have all the information about what to be done. But you
still don’t think about how to do the specified thing. This is the purpose of this step.

Programs are the children of algorithms. If the algorithm is not correct or not efficient,
then the program will also be inefficient and incorrect since programs are the direct
translation of algorithms in programming languages.

Therefore, to make your program of good quality, you must design the algorithm
carefully. And it is advisable to test the algorithm (instead of the program) to check for
reliability, maintainability and efficiency.

Since an algorithm is set of steps to perform the thing that is defined in the previous
stage, it should be represented in one way or another to be available and be a program in
the next step. Algorithms could be represented in different ways. You might use the
narrative way (writing the steps in English), pseudo-code (‘false code’) or flowcharts. We
will discuss about the representation of algorithms in another section of the same chapter.
3. Coding or Implementation
Right now, the algorithm is available. You know how to achieve the goal specified in the
problem identification stage. But the steps represented in the previous phase are not still
in the form understandable for the computer. Therefore, it is necessary to rewrite the
steps of your algorithm by using instructions in the selected programming language. This
process is called coding.
4. Testing and Debugging
You must be very lucky if your program works perfect at its first completion. But it is not
also fair to complain about the unlucky situation of a program’s fail at its first
completion, because it is a usual thing to happen.

Therefore, it is more than necessary to test a program for all possible inputs including
invalid and extreme cases. If a program cannot perform as expected, then the program is
said to have errors. Errors in programs are usually called bugs. Whenever, there is a bug,
you need to remove it until your program is error free. Removing bugs is called
debugging.
There are three types of errors that probably be encountered in a program:
Syntax errors (compilation errors): Syntax is the general format of writing a command
(statement) in programming languages. Syntax errors are errors created due to the
violation of the syntax of a command or a statement.

If there are any syntax errors in C++, then they will be detected and notified in the
compilation time. Therefore, a program having a syntax error could not be run without
removing the errors. Generally, there are three sorts of syntax/compiler errors:
Ordinary errors - the compiler will attempt to continue compilation after one or more
of these errors has occurred, e.g. Missing ENDIF statements
Fatal errors - the compiler will not attempt further compilation after detecting a fatal
error, e.g. Program too complicated -too many strings

Last Update: Jun. 23, 12 15


Ethiopian Civil Service College Computer Programming & Numerical Methods

Warnings - these are not strictly errors, but are intended to inform you that you have
done something unusual which might cause problems later, e.g. Expression in IF
construct is constant
Example: In C++, at the end of each statement (command), there should be a semicolon.
This is one of the grammatical rules in C++. If you miss a semicolon at the
end of a statement in C++ program, then it will be a syntax error.

Run-time errors: These types of errors are errors that will appear when the program is
allowed to run†.
Example: division by zero. If there is any place (instruction) which attempts to divide any
number by zero, then this error will not be syntax error instead run time error.
When the computer reaches and executes this instruction, it will find out that
this is invalid and the execution will be interrupted by announcing the error.

Logical Errors: These are the most dangerous errors. These errors are errors which cause
the program to produce a wrong output. Logical errors cannot be detected in the
compilation time as well as in the run time. It is the programmer’s job to test the program
for such types of errors by providing all the possible inputs and check whether the
outputs are the expected ones.
Debugging (particularly for logical errors) can be done in different ways:
Manually: Before entering the program instruction into the computer, the programmer
can trace the program by checking each and every instruction and by recording the
values (outputs) of each instruction.
Tools included in the compiler: most compilers have got debugging tools which show
you how each instruction after being executed affects the values of your variables
and outputs.
At times a program will give numerical answers to a problem which appear inexplicably
different from what we know to be the correct mathematical solution. This can be due to
rounding error, which results from the finite precision available on the computer.
5. Maintenance
Writing program for a given problem is an endless task. At the end of the day, you might
find undiscovered errors in the above phases or otherwise users may come up with a new
need. Therefore modification is expected even after finishing and implementing the
program. Modifying programs to make them error free (more!), to satisfy user’s need, or
to expand the capability of the program is called maintenance.

To make programs easier to modify, documentation play a major role. Starting from the
first step, documentation should be done. A typical document may contain the following
about a developed program:
A statement of the problem
A description of the system, which involves the description of program functions
and the system specifications


Running programs means executing them.

Last Update: Jun. 23, 12 16


Ethiopian Civil Service College Computer Programming & Numerical Methods

A description of the program which involves program flowcharts, program


listings, test data and results.
Instructions for installing and running the program.
1.11. Representation of Algorithms
It has already pointed out that algorithms need to be represented so that they could easily
be translated to programs later. Algorithms can be represented in various ways. We will
see here three popular methods to represent algorithms which are using step form,
pseudo-code and flowchart.

1.11.1. Representation Algorithms using Step form


The step form is the simplest representation of algorithm. It consists of a sequence of
numbered steps or points written in plain English. In this method variables and keywords
(eg. If … , then…, Repeat, goto, etc.) can be used.

Example:
1. Prepare an algorithm for making a cup of tea using the step form representation.
1. Prepare a kettle and a tea cup
2. If the kettle doesn’t have water, then fill it
3. Plug the kettle into the power point
4. If the tea cup is not empty, then empty the cup
5. Place tea leaves in the tea cup
6. If the water in the kettle is not boiling, then wait till it boils
7. Switch the power off
8. Pour the water from the kettle into the tea cup.

2. Develop an algorithm in step form that counts all vowels on a given page of text.
Assume n is the number of letters on that page.

1. Set current to 1
2. Set last to n
3. Set count to 0
4. Read letter at current
5. If letter is vowel, then increment count by 1
6. Increment current
7. If current <= last, then go to step 4

1.11.2. Representing Algorithms using Pseudo-code


A pseudo-code is a kind of structured English for describing an algorithm. The steps of
the algorithm will be represented using English phrases without the use of any specific
programming language.

Last Update: Jun. 23, 12 17


Ethiopian Civil Service College Computer Programming & Numerical Methods

When designing an algorithm for a given problem, the designer is advised to use a
pseudo-code so that he/she could be able to focus on the algorithm or the method of
solving the problem instead of worrying to write instructions in the correct syntax of a
particular programming language. Moreover, once an algorithm is represented, coding
will be translating the steps written in the pseudo-code in one of the programming
language. We can say if a well-written algorithm is prepared, the challenge of
programming is defeated.

There is no standard for pseudo-code writing. You may see different ways of pseudo-
code representations in different books. Even you can develop your own way of writing
pseudo-cde for an algorithm. But, there are some useful points that you have to know to
start writing pseudo-code. There are three basic constructs for flow of control from which
any algorithm is implemented. Therefore a pseudo-code may be made up of these three
logic structures. They are:
1. Sequence: Sequence structures show the processing of steps one after the other
sequentially. Whenever there are any series of steps, they will be represented in
pseudo-cde by writing one after the other in their right order just like the
following illustration:
Step 1
Step 2 A sequence can consist of only one
Step 3 instruction. But if the steps are more that
. one, then they are usually enclosed
. between begin….and …end.
.
step n

2. Selection: Selection happens whenever there are things to be done based on the being
true of a certain condition. There will be usually two or more choices. This one
is illustrated as:
If condition1 then
Sequence 1
Else if condition2 then
Sequence 2 This part is optional,
.
. i.e, it may appear or
. not.
Else
Sequence n
End if

3. Iteration (Loop): This condition refers to a situation in which there are one or more
instructions which should be performed several times depending on some
condition. This is what we call loop. Loop is implemented in pseudo-code like
the following (i.e. of course one way)
While condition
Sequence
End while
Additionally, the following keywords are common:
a) To show the beginning and the end of the algorithm: Begin, End

Last Update: Jun. 23, 12 18


Ethiopian Civil Service College Computer Programming & Numerical Methods

b) To accept input value: Read, Get


c) To show an output value: Write, Display or print
d) To perform calculation: Compute, Calculate
e) To initialize variables: Set
f) To add one to the value of a variable: Increment
g) To use Operators: +, -, *, /, <, >, <=, >=, <>

Note: indentation will also play a major role in making the represented algorithm
readable and easily modifiable. When you start a different sequence, indent the
steps to differentiate the set of the steps in this sequence from the previous one.

Last Update: Jun. 23, 12 19


Ethiopian Civil Service College Computer Programming & Numerical Methods

Examples:

Design an algorithm for solving the following problems. Represent the algorithm in
pseudo-code.
1. Add two given numbers.
Pseudo-code can be completely formed from only English phrases or alternatively
English phrases and code like statements can be mixed in a pseudo-code. For this
problem both types are shown below. But for the other examples, the second form is used
to implement the pseudo-code.
Begin
Read the two numbers Completely described
Compute the sum by adding the numbers in English phrases
Display the sum
End

Begin
Read a,b The second style consisting of keywords,
sum a+b operators and sometimes English phrases
Display sum
End
Try to see how the steps are clear. For this problem, the only thing that is left is to rewrite
the steps in the selected programming language. In C++, the following program could be
produced from the above pseudo-code:
#include <iostream.h>
main()
{
int a,b,sum;
cout<<”Please enter the two numbers”; //To ask the user for the two
numbers
cin>>a>>b; //To read the numbers
sum=a+b; //To add the numbers
cout<<sum; //To display the sum
}
Don’t worry if you don’t really understand the above program since it is not the right
time to complain. This program is just here to show you how a program in one
programming language could be produced from a pseudo-code.
2. Produce the result of the following computation for four given numbers a, b, c and d

Result=(a-b-c)+d
Begin
Read a, b, c, d
Result (a-b-c) + d
Display result
End

Last Update: Jun. 23, 12 20


Ethiopian Civil Service College Computer Programming & Numerical Methods

3. Find the average of three numbers.


Think about the given problem. How would you find an average of three numbers if you
are asked? Are you going to ask your friend to do it for you? The method is very simple:
Add the three numbers and them divide the sum by 3. Therefore, the following will be the
pseudo-code representation of this algorithm:
Begin
Read a,b,c
Average (a+b+c)/3
Display Average
End

4. Find the average mark of a student for given four subject marks.
This is not really a different question than Q.No.4. Therefore it will be done like the
following:
Begin
Read mark1,mark2,mark3,mark4
sum mark1+mark2+mark3+mark4
Average sum/4
Display Average
End

5. Obtain average marks for five students for given four subject marks.
This problem and the above one are basically the same. The only difference between the
two is that in the first case, we had only one student where as in this problem we have
five students for whom the same task to be done. What are we going to do to treat this
problem?

One way (may be foolish way) is to repeat the instructions for all of the five students
resulting to have 20 steps (later to be instructions) in the algorithm. What if we have 100
students? Is this method really feasible?

Therefore we use a loop for such cases. A loop refers to set of instructions, which will be
repeated for a number of times based on some condition. Since we have five students
here, the loop should run for five times. How can we implement this?

Let’s first start by writing the steps using English phrases:


Begin
While there is still a student
Read the four marks of the student
Compute the sum by adding the mark values
Compute the average by dividing the sum by 4
Display the Average
End of the while clause
End

Last Update: Jun. 23, 12 21


Ethiopian Civil Service College Computer Programming & Numerical Methods

‘While loop’ is going to be used in implementing the above algorithm. How do I tell the
‘while loop’ that there are five students and it should run exhaustively for five times?
Even how will I stop the ‘while loop’? To continue or to stop, you need to count the
students naturally.

‘While loop’ needs an initialization in which the condition is going to be based on. A
certain variable will be initialized to count the number of times the loop is run. Usually
the counter must be initialized to 1 to start counting the loop from 1.

The pseudo-code representation of the above algorithm is presented below:


Begin
counter 1
while counter<=5
Read mark1,mark2,mark3,mark4
sum mark1+mark2+mark3+mark4
Average sum/4
Display Average
counter counter+1
end while
End
The above algorithm will be executed like the following:

The first step is to assign the value 1 to a variable called ‘counter’. This is the
initialization process. The value of ‘counter’ is going to be used for counting the loop.
The loop is started now. All loops have a certain condition to be tested. In our case, the
condition is set to check whether the current value of the variable ‘counter’ is less than or
equal to 5. If this condition is evaluated to true, then the instructions found within the
loop will be executed for one more time. If the evaluation of the condition is false, then
the loop will be stopped and the very next instruction after ‘end while’ will be executed.
Once a loop is stopped, it will never be returned back.

Success to start
the loop once
Begin again because
Checking
the
counter 1 the condition
possibility while counter<=5 stated is true
to add one Read mark1,mark2,mark3,mark4
more loop sum
mark1+mark2+mark3+mark4
Average sum/4
Display Average
Otherwise, when
counter counter+1 the condition is
end while false, all the
End instructions will
be jumped.

Last Update: Jun. 23, 12 22


Ethiopian Civil Service College Computer Programming & Numerical Methods

First round:
Counter=1
Since 1<5, the loop will be entered and as a result the average for the first student will be
computed by reading his/her marks. Then after displaying the computed average, the
statement counter counter+1 will be processed. According to this instruction, the sum
of the current value of the variable counter and 1 will replace the previous value of
counter. So after the execution of this instruction, the value of counter is 2.

Second round:
Counter=2
One more execution of the loop will be carried out and the value of counter will be
incremented.

The same will happen to the third, fourth and fifth round. At the end of the fifth round,
the value of counter becomes 6. Right now, it is not possible for the ‘while loop’ to
continue since the logical comparison counter<=5 (which is 6<=5) yields false.
6. Compute the following: 20+21+22+……..+29

Begin
sum 0
i 0
while i<=9
sum sum+2i
i i+1
end while
Display sum
End

7. Check whether a number is even or odd.


Even numbers when divided by 2, the remainder will be zero.
Begin
Read number
Rem=number mod 2 //mod is an operator used to produce the remainder
from a division.
If rem=0 then
Display message as to tell the user that the number is even
else
Display message as to tell the user that the number is odd
end if
End

Last Update: Jun. 23, 12 23


Ethiopian Civil Service College Computer Programming & Numerical Methods

1.11.3. Representing Algorithms using Flowchart


The other way to represent an algorithm is the flowchart method. A flowchart is a
graphical way of representing algorithms. The flowchart will be used to present the
operations and their flow using the standard symbols.

The list of the standard symbols and their meaning is shown below:

Terminal
Decision
Connectors
Input / Output

Processing Flow line

Any flowchart can be built from the above standard symbols. Below is explanation of the
standard symbols.

Terminal: This standard symbol is used to show the beginning and the end of the
algorithm. You usually write ‘start’ and ‘stop’ in the terminal symbols at the beginning
and end of the flowchart.
Start
The body of
the algorithm

Stop

Input/output: Whenever there is an input to be accepted and an output to be produced, we


use this standard symbol. It is customary to write the specific input and output after the
usage of keywords like read (for input), display or print (for output).

Read a

Display sum

Last Update: Jun. 23, 12 24


Ethiopian Civil Service College Computer Programming & Numerical Methods

Processing: operations to be done will be expressed using this symbol. These operations
are of two types: arithmetic operations (addition, subtraction, multiplication and division)
and data movement instructions (copying some data from a variable to another one).

sum a+b

x y

x 4

Decision: we know that there are two types of operations that can be done by the
computer. The first types are arithmetic operations where as the other type is logical
operation. Logical operation consists of the evaluation of logical expressions. Logical
expressions are expressions which are built from logical operators (like <,>,=,<=,>=) and
evaluated only to the values true or false. Therefore, sometimes decisions can be made
based on the value of a logical operation. A good example for this is the statement ‘if a>b
then max a’. When you have such conditions to be tested, then you could use the
decision symbol to represent the situation.

To implement the above if clause using the decision symbol, you could do the following:

No Is Yes
max b max a
a>b

The decision standard symbol will have one entry point and at least two exits.
Flow line: shows the direction of logical flow. This arrow connects standard symbols
used to represent sequence of steps.

Connectors: There are two types: on-page connector and off-page (Interpage) connector
respectively as shown above in the figure.
...On-page: is used for connecting two points in a program (algorithm) without
drawing flow lines. A pair of identically labeled connector symbols denotes
that the indicated points of the flowchart are connected.

..Interpage: is used as an exit to or any entry from another part of the


flowchart on another page.
Start

A labeled on-page
1 connector connecting
one point from the
flow chart to another
1 one

Stop

Last Update: Jun. 23, 12 25


Ethiopian Civil Service College Computer Programming & Numerical Methods

There are some guidelines that you have to follow when drawing flowcharts:
a) A flowchart should start and end with the terminal symbol having ‘start’ or
‘begin’ and ‘stop’ or ‘end’ respectively.
b) Crossing flow lines should be avoided. And to avoid this, you need to use
connectors.
Before seeing the examples that we have, let’s list some of the advantages of flowcharts:
a) They can be used by both programmers and non-programmers alike to illustrate
the entire process graphically in a smart manner.
b) While drawing a flowchart one is not concerned with the details of the elements
in programming language. Hence, it facilitates program writing.
c) Since it shows the flow of operations in pictorial form, it allows to easily detect
logical errors in the algorithm.
d) Since it is independent of programming languages, it can be translated into more
than one language.
We will draw flow charts for some of the same examples given before (in the pseudo-
code section).

Examples:
1. Find the largest of three given numbers. Take the assumption that the numbers
entered will never be equal.
Start
Begin
Read a,b,c 1.1.1. S
max a Read a,b,c t
if max<b then a
max b r
max a t
end if
if max<c then
max c
max b max<
end if
b
Display max
No
End

max< Yes max c


c
No

display
max
Stop

Last Update: Jun. 23, 12 26


Ethiopian Civil Service College Computer Programming & Numerical Methods

2. Add two given numbers.


It is very easy to draw a flowchart starting from the already constructed pseudo-code.
Start Start
Begin
Read a, b
Sum a +b Read a,b Read a,b
display sum
End sum a+b sum a+b
=
display sum display sum

Stop Stop

3. Design and represent an algorithm to obtain the sum of the first 50 counting numbers.
Start
Begin
number 1
while number<=50 number 1
sum sum+number sum 0
number number+1 1
end while No number<
Display sum
=50
End
Display Yes
sum
sum sum+number
Stop 1
number number+
1

4. Check whether a number is even or odd.


Begin
Read number
Rem=number mod 2
If rem=0 then
Display message as to tell the user that the number is even
else
Display message as to tell the user that the number is odd
end if
End

Last Update: Jun. 23, 12 27


Ethiopian Civil Service College Computer Programming & Numerical Methods

Start

Read number

rem number mod 2

Display message No rem=0


‘ n is even’

Yes

Display message
‘ n is odd’

Stop

5. Suppose that Birr 1000 is deposited in a savings account in 1993 and the bank pays 10
percent interest, compounded annually. Draw a flowchart of an algorithm which
prints the year and the amount in the account, until amount first exceeds Birr 10,000
and which also finds the number of years it takes for amount to reach Birr 10,000 or
more. {From ‘Introduction to computer Science’ by Dr. Dida Midekso}

Develop your own algorithm after understanding the problem. Write the pseudo-code
form of your algorithm and draw the flowchart. The following is an algorithm presented
here:
Start

amnt 1000
year 1993
cnt 1

amnt amnt+0.10*amnt

year year+cnt Yes Amnt>=100


00
No
Display year,amnt,cnt
cnt cnt+1 1
Stop

Last Update: Jun. 23, 12 28


Ethiopian Civil Service College Computer Programming & Numerical Methods

1.12. Review Exercise and Assignment


Exercise:
1. Write an algorithm for making a cup of tea.
2. Develop an algorithm that counts all vowels on a given page of text. (assume n is
the number of letters on that page)
3. Write an algorithm that sums all even numbers between 1 & 20 inclusive and
displays the sum.
4. Develop an algorithm in pseudo-code & flow chart format to find the roots of a
quadratic equation.
Assignment:
Develop an algorithm (both pseudo-code and flow chart) for the following problems:
1. Calculate the average of the first n whole numbers and display the average
2. Determine and display the factorial of an integer
3. Generate the Fibonacci sequence, where the user enters the value of n.
F1 = F2 = 1
Fn = Fn-1 + Fn-2

Last Update: Jun. 23, 12 29


Ethiopian Civil Service College Computer Programming & Numerical Methods

2. Introduction to C++
We have been writing algorithms in pseudo-code from in the last chapter. And we have
said many times that these are only algorithms not real programs which implies the
algorithms have to be rewritten in a program form by using a certain programming
language that is completely understood by the computer.

Our language in this course is C++. Since its introduction about a decade ago, C++ has
experienced a growing acceptance as a practical object-oriented programming language
suitable for teaching, research, and commercial software development. The language has
also rapidly evolved during this period and acquired a number of new features (e.g.,
templates and exception handling) which have added to its richness.

2.1. History of C++


You know one thing about C++ and that is it is one of the high level programming
languages. Below is brief the history of C++ and general trend of programming
languages.

C++ programming language has got its name from C programming language. The ++
sign is derived from the ++ operator which is found both in C and C++ languages. It tells
us that C++ includes all features of C and many more.

C language was derived, in turn, from the B programming language which again derived
from BCPL. The languages BCPL and B are earlier versions of the C language; hence
they do not concern us. The C programming language developed, by Dennis Ritchie, at
AT&T Bell Laboratories in the 1970s. It was first used for writing the UNIX operating
system and later its use was extended to write any sort of program. Hence C is general-
purpose language. But the popularity of C language is highly tied with the UNIX
operating system. If someone wanted to maintain his UNIX, he had to know C language.

C language is unique in that it is a high-level language with many of the features of low-
level language. It is between the two extremes. Like low-level C language programs can
directly manipulate the computers’ memory. On the other hand, it is easier to read and
write programs in C language than Assembly, which is feature of high-level languages.
This has made C an excellent choice for writing system programs. But for other programs
C is not as easy to understand as other high- level languages.

To overcome this and other shortcomings of C, in the early 1980s a new language known
as C++ was developed, by Bjarne Stroustrip, at AT&T Bell Laboratories. C++ was
designed to be a better C. In other word C is a subset of C++. Unlike C, C++ has features
to do Object-Oriented Programming (OOP).

Last Update: Jun. 23, 12 30


Ethiopian Civil Service College Computer Programming & Numerical Methods

2.2. C++ Program Structure


Even if we can not come up with a standard format representing all C++ programs, the
following could be taken as a general structure of C++ programs:

#include <iostream.h>
main()
{
variable declaration; #include <iostream.h>
statement 1; main()
statement 2; {
. cout<<”Hello World”;
. return 0;
. }
statement n;
return 0;
}

To understand the general structure given above, let's discuss about each line separately:
Line 1: The Preprocessor Directive: #include <iostream.h>
“iostream.h” is one of the several header files available in C++ compilers. Header
files are files which are external and saved in a certain path. These files contain the code
for different functions and operators used in your C++ program.

You have heard about compilers of C++. You can have a different kind of them. But
there is another program called preprocessor which always run before the compiler each
time one starts the compiler. The function of the preprocessor program is to look for all
lines which begin with the pound symbol (#) through out the program. And having
these lines of the program, the preprocessor will ‘read in’ or copy the content of the
header files specified after include into the program to make the program ready to be
compiled. It is after this time the compiler starts acting on the program (translating and
checking for syntax errors).

Example 1: #include <iostream.h> is used to copy and insert all the content of
the mentioned header file which is iostream.h into the source program (our own
program).

You might be thinking about one thing right now! Why do we need to include the code
written in an external file to our own programs while we believe that our program files
are complete? The answer is straightforward. It is because your program needs the copied
code. Since you will use predefined (pre-coded) operators and functions in your program,
the code written for each of these has to be included in your program. Otherwise, the
compiler will not recognize the operators and the functions used and will refuse to
compile the program.

Last Update: Jun. 23, 12 31


Ethiopian Civil Service College Computer Programming & Numerical Methods

Example 2: Look at the statement in line 4 of the C++ program given as example right to
the general format above. It is cout<<”Hello World”; ‘cout<<’ is an operator
whose code is written in the iostream.h header file. If the content of the header file is
not included prior to the compilation process, the compiler will not recognize this
statement at all.

Therefore, this line of the program (as well as all include directives) is used to include
header files. Note that there are a number of header files with their own content and it is
possible to include multiple header files. If you intend to use one of the operators or
functions defined in one of these files, include them at the beginning of your program.

Line 2: The main function: main()


Let us first define the term ‘function’ in the sense of C++. The meaning of this word is
more related with the modular (structural) programming technique. This technique can be
generally explained as follows:

Given a big problem, divide it into smaller sub-problems. Then prepare programs (code)
for each smaller problem. Finally combine and integrate the small programs to build a big
software. The small programs which are here used as a building block to form a larger
program are called functions. Functions in a program will communicate to each other.
This communication technically is called calling. Therefore you could say one function
call another one. The output of one function can be used as an input to another one etc.

In this sense, a C++ program can be defined as set of functions. And one of these
functions is the function to be executed first when the program is run automatically. This
function is known as the main function.

A function syntactically can be expressed as it is indicated in the following format:


Function header
{ The function header may contain the name
Statement 1; of the function and other important
Statement 2; information about the function. Obviously,
. the braces enclose the body of the function
. that contains the statements (instructions
. included in the function).
Staement n;
}

The main function in a C++ program is entitled as main() so that execution will be
started from the first instruction of this main function. Even if in our simple C++
programs in this course we will not have multiple functions, we will give the title
main() as the header of our only and main function.

Last Update: Jun. 23, 12 32


Ethiopian Civil Service College Computer Programming & Numerical Methods

Line 3: The opening brace: {


It marks the beginning of the program. Note that this brace is not lonely. Braces should
always be in pair. An opened brace has to be closed later in the program otherwise a
syntax error will occur. The last line of this format contains the closing brace ( } ). Not
only is the body of the main function but also other blocks (group of instructions) in C++
programs are enclosed by these braces.

Line 4: Variable declarations


Variables are already a little defined and I believe you have the notion that a variable
represents a reserved memory space in the RAM. If so, to make (order) the computer
reserve a certain amount of space in memory for various variables, our program should
ask this before using the variables. This process is called variable declaration. We will
discuss about how declarations can be specified and other important things about the
same topic later in the same chapter.

As a wise beginner programmer, try to declare all your variables which will be used in
your program at the beginning of your program. And note that variable declaration may
contain multiple lines.

The next lines (Statements): Statement 1, ..., Statement n


A statement is a computation step which may produce a value or represent the actual
executable instructions of the program. End of a statement is always marked by
semicolon, ;. By using the different available keywords, operators and functions of C++,
you can write your instructions which are generally called statements.

The last executable statement: return 0;


The return statement is the last statement in any function. It is used to return control to
the calling function after finishing the execution of all instructions in the called function.
The return statement in the main function will return control to the operating system since
it is the operating system that initiates the execution of the main function.

If you omit this statement from the main function, then the compiler will not refuse to run
the program but at least will give you a warning telling you that any function should
return a value. If you want to omit the return statement without any warning, add void
before main as:
void main()

2.3. Basic Elements of C++ Program


Once you see the general format (structure) of a C++ program, it is now time to see the
different components of C++ programs. Even if no one could conclude that all these
components always exist in a given C++ program, it is true that most of them do have
them.

Last Update: Jun. 23, 12 33


Ethiopian Civil Service College Computer Programming & Numerical Methods

The following are some of the C++ program elements:


Names, Identifiers and Keywords
Input and output
Data types, Variables and Constants
Expressions
Statements
2.4. Names
Programming languages use names to refer to the various entities that make up a
program. Category of names include: Variable names, function names, type names and
macro names. Names allow you to organize what would otherwise be quantities of plain
data into meaningful and human readable collections. As a result, no trace of a name is
left in the final executable code generated by a compiler. For example, a temperature
variable eventually becomes a few bytes of memory which is referred to by the
executable code by its address, not its name.

2.4.1. Identifiers
Identifiers are names given to a variable. A variable has a unique name in a program and
there are some restrictions when naming variables. C++ imposes the following rules for
creating a valid identifier
Should consist of sequence of letters, numbers and underscore;
Could not begin with a number,
Should not use keywords (eg. Void = 2 can not be valid since Void has specific
meaning for the compiler),
Identifiers in C++ are case sensitive and sometimes their size is limited.
C++ imposes no limit on the number of characters in an identifier. However, most
implementations do. But the limit is usually so large that it should not cause a concern
(e.g., 255 characters).

2.4.2. Keywords
Certain words are reserved by C++ for specific purposes and may not be used as
identifiers. These are called keywords or reserved words and are summarized Table 2-1.

Table 2-1 List of some C++ keywords


asm continue float new signed try
auto default for operator sizeof typedef
break delete friend private static union
case do goto protected struct unsigned
catch double if public switch virtual
char else inline register template void
class enum int return this volatile
const extern long short throw while

Last Update: Jun. 23, 12 34


Ethiopian Civil Service College Computer Programming & Numerical Methods

2.5. Comments
A comment is a piece of descriptive text which explains some aspect of a program.
Program comments are totally ignored by the compiler and are only intended for human
readers. C++ provides two types of comment delimiters:

Anything after // is considered a comment.


//is comment until the end of the line on which it appears
Anything enclosed by the pair /* and */ is considered a comment.
/* Comments should be used to enhance (not to hinder)
the readability of a program. The best guideline for
how to use comments is to simply apply common sense */
The following two points, in particular, should be noted:
 A comment should be easier to read and understand than the code which it tries
to explain.
 Over-use of comments can lead to even less readability.
 Use of descriptive names for variables and other entities in a program and proper
indentation of the code can reduce the need for using comments.
2.6. Input/Output, I/O
The most common way in which a program communicates with the outside world is
through simple, character oriented Input/Output (IO) operations.
2.6.1. Input Statement
C++ uses the input operator >> to get data from the input stream named on its left. We
usually use cin (pronounced “see-in”) input stream, which refers to the keyboard.
cin >> num // pauses the system, waiting for input, num.
Once a value is input, it is assigned to num and the execution continues.

2.6.2. Output Statement


In C++, output statement is analogous to input statement. In this case the data flows out
to the stream named on the left side of output operator, <<. An object that links to
standard output device is named cout (pronounced “see-out”), which refers to the screen
of monitor.
cout << 20 // would display the number 20 on the screen.
It is obvious that the directions of the operators are in the direction of data flow.

Last Update: Jun. 23, 12 35


Ethiopian Civil Service College Computer Programming & Numerical Methods

2.6.3. I/O manipulation


Input/output manipulation requires the iomanip.h header file. For example:
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
void main ( )
{clrscr ( )
float a = 0.1, b = 1.0;
cout << a << "," <<b <<endl;
cout <<setprecision(6)<<a <<","<<b <<endl;
cout <<fixed<<setprecision(3)<<a << "," <<b <<endl;
cout <<scientific<< a << "," <<b <<endl;
cout <<setw(8) << "34" <<setw(10) << "45" << endl;
getch();
}

Last Update: Jun. 23, 12 36


Ethiopian Civil Service College Computer Programming & Numerical Methods

2.7. Variables
In programs, data is needed to be stored in memory. This is to store input values to
retrieve it for later use, or to assign a certain value (either the result of a computation or a
constant value) and in the mean time track intermediate results.

A variable in C++ programs is a symbolic name given to a location in memory space in


which data can be stored and subsequently recalled. I have to remind you that the
memory (RAM) is logically divided into series of locations each called memory
allocation. These allocations internally are given number addresses sequentially. It will
not be really convenient for the programmer to refer, access and use memory allocations
using these number addresses.

Therefore, variables will give an easy and flexible way to access the RAM in your
program. The value of variables can be changed in any part of the program as a result of
your program instructions. The kind of value a variable can assume depends on its type.

2.7.1. Data types


The data type of a variable refers to the type of the data which will be stored in the
variable. For instance, if you declare a variable to be type of integer, then the implication
is you can store only numbers with no decimal point and of course within a certain
interval. Data type is established when the variable is declared. Once defined, the data
type of a C++ variable cannot be changed. Table 2-2 describes the data types in C++
programming language.

Table 2-2 Data types and their value range


Data/variable Type Bytes Range
Integers Short 2 -32768 – +32767
Int * Short or Long (System Dependent)
Long 4 -231 to +231 –1
Real Numbers Float 4 -3.4 10-38 to 3.4 10+38
Double 8 –1.7 10-308 to 1.7 10+308
Long double 10 - 1.2 10-4932 to + 1.2 10+4932
Characters Char 1 -128 to 127
* We also have a void data type.

2.7.2. Declaration of Variables


Having all these ideas, you must also note that to tell the compiler about the existence
and the characteristics (such as name and type) of your variables, you have to declare
them.

A variable has to be declared before using it. The declaration is to tell the compiler the
name of your variable with its data type so that a memory space is allocated for your
variable. The syntax of a variable declaration is:
<Type> <identifier/ variable name>;

Last Update: Jun. 23, 12 37


Ethiopian Civil Service College Computer Programming & Numerical Methods

Multiple variables of the same type can also be declared like the following:
Type varname 1, varname 2, …, varname n;

Example: int a;
float x,y,z;

Initialization is assigning of a variable for the first time. The syntax is:

<Type> <identifier > = <Value>;

It is important to ensure that a variable is initialized before it is used in any computation.


It is a good programming practice to define a variable and initialize it at the same time,
because it pre-empts the possibility of using a variable prior to it being initialized.

Example: int a = 5;
float x = 9.5;

Integers

An integer variable may be defined to be of type short, int, or long. The only
difference is that an int uses more or at least the same number of bytes as a short, and
a long uses more or at least the same number of bytes as an int.

By default, an integer variable is assumed to be signed (i.e., have a signed representation


so that it can assume positive as well as negative values). However, an integer can be
defined to be unsigned by using the keyword unsigned in its declaration. The keyword
signed is also allowed but is redundant.
1. int (or signed int)
int variablename; OR
signed int variablename;

Just like unsigned int, Turbo C++ will allocate 2 bytes for each variable declared as
int or signed int. Note that int and signed int are the same. The 16 bits
given for such a variable will possibly be used to store one of the signed numbers within
the following interval:
-2n-1 to +2n-1 –1
-216-1 to +216-1 –1
-32768 to +32767
The same type of the previous problem that is exhibited in the case of unsigned int
will occur if a signed number out of this range is tried to be assigned to this variable.

Since 32768 is a positive signed number which is out of the range and which is found
next to the right boundary number, it makes the compiler to loop back to the left
boundary (to the first and the smallest number of the interval). Similarly the assignment

Last Update: Jun. 23, 12 38


Ethiopian Civil Service College Computer Programming & Numerical Methods

of the value –32769 to the variable x in the above example makes the compiler to loop
back to the last number because it is found just before the first number of the interval.

In all the cases when a value out of the interval of its type is assigned to a variable, it will
wrap around and start over forming a cycle.
2. signed short int (or short int)
The short int (unsigned short int) and int (signed int) are the same
in the compiler that we are using.
3. signed long int (or long int)
Probably, you yourself can see easily that 4 bytes will be reserved in memory for long
int variable just in the case of unsigned long int. The difference comes when
we consider the interval of numbers to be stored in the variable.

The interval becomes:


-231 to +231 –1
-2,147,483,648 to 2,147,483,647
Try the same thing for long int by assigning a variable out of the above interval.

Summary in the middle way:


Once you come to a decision to use a variable to hold integers, then choose one of the above six
data types to declare the variable based on your application.

And before leaving the discussion about integers, look at the following examples:
int x; int x; int x;
x = 12.0; x = 12.3; x = 12.6;
cout<<x; cout<<x; cout<<x;
Output is 12 Output is 12 Output is 12
Even if the declared variable is an integer, we assign real numbers (numbers with a
decimal point and a fractional part) in all the three cases. But as shown, the output from
all code fragments is 12.

What does this indicate? An integer variable (whatever the integer type is) when assigned
a real number will truncate (not round) the fractional part and store only the integer part.
Truncating is cutting the fractional part.

Real Numbers:

Real numbers are numbers having both integer and fractional part. Whenever there is a
need to declare a variable for storing real numbers, one of the following data types could
be used:
1. float
float variablename;

Last Update: Jun. 23, 12 39


Ethiopian Civil Service College Computer Programming & Numerical Methods

Turbo C++ compiler will allocate 4 bytes for a variable of type float. These bytes can be
used to represent one of the floating-point numbers in the following interval: -3.4 10-38
to 3.4 10+38

2. double
double variablename;
‘double’ is also a floating point data type but be capable of storing very large or very
small numbers than float. The compiler will allocate 8 bytes for a double variable type.
The interval is: –1.7 10-308 to 1.7 10+308.

Note: Real numbers in C++ can be written in scientific form as follows:


Example: 1.2e+6 = 1.2 10+6

The implication is it is possible to write the following assignment statement:


float x; float x; float x;
x = 1.2e+6; x = 1.2E+6; x = 2;
cout<<x; cout<<x; cout<<x;
The outputs of the first two fragment codes are the same which is 1.2e+6. The third one
will produce 2 as a result.

Characters:

A character variable occupies a single byte which contains the code for the character.
This code is a numeric value and depends on the character coding system being used (i.e.,
is machine dependant). The most common system is ASCII (American Standard Code for
Information Interchange). For example, the character A has the ASCII code of 65, and the
character a has the ASCII code 97. When a character variable is declared, one byte (8
bits) will be given in memory. The data type that is usually used to declare a character
variable is char.
char variablename;
All variables declared as ‘char’ are represented in their ASCII number code. A total of
256 printable and non-printable characters are possibly represented using the one byte
allocated.

Character variables are assigned as follows:


char x; char x; char x; char x; char x;
x=’A’; x = ‘a’; x = ‘?’; x = ‘~’; x = ‘2’;
cout<<x; cout<<x; cout<<x; cout<<x; cout<<x;
Output is A Output is a Output is ? Output is ~. Output is 2.
Note that when assigning characters, you have to use a pair of single quotation marks to
enclose them.

Like integers, a character variable may be specified to be signed or unsigned. By the


default (on most systems) char means signed char. However, on some systems it

Last Update: Jun. 23, 12 40


Ethiopian Civil Service College Computer Programming & Numerical Methods

may mean unsigned char. A signed character variable can hold numeric values in
the range -128 through 127. An unsigned character variable can hold numeric values in
the range 0 through 255. As a result, both are often used to represent small integers in
programs (and can be assigned numeric values like integers).

Due to the aforementioned relation, an int value can be assigned to a char variable and
a character value can be assigned to an int variable. This is because the representation
of characters is internally in numbers specifically unsigned integers. Therefore, a
character variable assigned an integer will store the corresponding ASCII character
instead of taken as incompatibility (mismatch) type problem. The following two
examples illustrate variables of type int being assigned a character so that they can store
the ASCII value of the character:
char ch; int y;
ch=97; y=’a’;
cout<<ch; cout<<y;
output is a. output is 97.
Example:

String

A string is a consecutive sequence (i.e., array) of characters which are terminated by a


null character. Unlike other programming languages, such as Pascal, C++ has no defined
data for string data type. Obviously a variable declared as a character can not be used to
store a string since it can hold only a single character. Hence, a string variable is defined
to be of type char* (showing C++ that you can have possibly more than one character
A string variable, therefore, simply contains the address of where the first character of a
string appears.
char* x;
x = “Hello”;
cout<<x;
Output will be Hello.
When assigning string, use a pair of double quotation marks instead of single quotation
marks to enclose the string.

A common programming error results from confusing a single-character string (e.g., “A”)
with a single character (e.g., ‘A’). These two are not equivalent. The former consists of
two bytes (the character ‘A’ followed by the character ‘\0’), whereas the latter consists of
a single byte. The shortest possible string is the null string (“”) which simply consists of
the null character.

2.7.3. Scope
Scope of an identifier is part of the program where it can be used. The scope of a variable
begins at its declaration and ends at closing brace in which the variable was declared. A
variable defined at the program scope level (Beginning of the program) is said to be
global variable. Each block in a program defines a local scope. Variables defined within a

Last Update: Jun. 23, 12 41


Ethiopian Civil Service College Computer Programming & Numerical Methods

local scope are visible to that scope only and are called local variables. Local scopes may
be nested, in which case the inner scopes override the outer scopes.

Generally, global variables last for the duration of program execution, while local
variables are created when their scope is entered and destroyed when their scope is
exited. The memory space for global variable sis reserved prior to program execution
commencing, whereas the memory space for local variables is allocated on the fly during
program execution.

Example: Illustration of variable scope


#include <iostream.h>
int x = 9; // x as Global variable
Void main ()
{
Cout<< “x = “<< x ; // gives x = 9
{
int x = 5, y = 3; // x & y as local variable
cout << “x = “<< x << “y = “<< y ; // gives x = 5 y = 3
{
int x = 15; // x as local variable
cout <<“x = “<<x <<“y = “<<y ; // gives x = 15 y = 3
y = y – 1 ;
x = x + 3 ;
cout <<“x = “ <<x <<“y = “<<y ; // gives x = 18 y = 2
}
cout << “x = “<< x << “y = “<< y ; // gives x = 5 y = 2
}
Cout<< “x = “<< x ; // gives x = 9
}

2.8. Constants
A constant could either be defined or declared. A constant could be defined as:
# define x 35
# define y 390
Preceding a variable definition by the keyword const declares that variable as a
constant and read-only (i.e. a symbolic constant). A constant must be initialized to some
value when it is defined. For example:
const int maxsize = 128;
const float PI = 3.14;

Once defined, the value of a constant cannot be changed:


maxsize = 256; //illegal!
A constant with no type specifier is assumed to be of type int:
const maxsize = 128; // maxsize is of type int
Following are some representation of constants

Last Update: Jun. 23, 12 42


Ethiopian Civil Service College Computer Programming & Numerical Methods

i. Integers:
Decimal: int x = 1776, y = 364, z = 0, w = -342;
Octal (Base 8): int x = o113 // (113)8
Hexadecimal: int x = ox4b; // (4b)16 = 4x1b + 11
ii. Floating Point numbers
float pi = 3.141593;
float av = 6.02e23;
float ec = 1.6e-19
iii. Character and strings
char x = ‘a’
char y = “the” // illegal!
Example:
main()
{ a = 3; // Error: it has not been declared yet.
int a; // Scope(life) of a begins here
{ a = 17; // Now it is okay
b = 34; // Error because b is not yet declared
int b; // Scope(life) of b begins here
b = 36; // Now it is okay
} // Scope(life) of b ends here
b = 4; // Error: it has not been declared yet.
}

2.9. Special characters (Escape sequences)


Table 2-3 List of Escape sequences in C++ (Special characters)
Character Description
\n New line
\r Carriage return (First position)
\t Tabulation ( 8 spaces)
\v Vertical tabulation
\b Back space
\f Page feed – new page
\a Alert (Beep) sound
\‘ Single quote
\“ Double quote
\? Question mark
\\ Back slash
Example: What will be the output of the following C++ code fragments;
cout << "This is\na new line"<<endl;
cout << "This is " << endl << "a new line \n";
cout <<"These\b\b\t book\n"<<"is\rwritten by \"Mr."<<"Holmes\" ";
//Output

Last Update: Jun. 23, 12 43


Ethiopian Civil Service College Computer Programming & Numerical Methods

2.10. Review Exercise


Exercises
1. Which of the following represent a valid identifier?
identifier
seven_11
_unique_
full-name
full#name
4by4
default
avg_wt_of_stdnt
variable
object.oreiented

2. Which of the following represent a valid variable definition?


int n = -100;
unsigned int i = -100;
signed int = 2.9;
long m = 2, p = 4;
int = 2k;
double x = w * m;
float y = y * 2;
unsigned double z = 0.0;
double d = 0.67F;
float f = 0.52F;
signed char = -1786;
char c = ‘#’ + 2;
sign char h = ‘\111’;
char *name = “Shimelis B”;
unsigned char *name =
“411357”;

3. Write a program that converts a temperature reading in degrees Fahrenheit to its


equivalent degree Celsius value.

Assignment
1. Write a simple calculator program which displays the sum, difference, product
and quotient of two integers entered by the user.
2. Develop algorithm in both pseudo-code and flow chart format:
a. To calculate the LCM and GCF/GCD of two numbers,
b. To check whether a number is prime or not.

Last Update: Jun. 23, 12 44


Ethiopian Civil Service College Computer Programming & Numerical Methods

3. Expressions in C++
This section introduces the built-in C++ operators for composing expressions. An
expression is any computation which yields a value.

When discussing expressions, we often use the term evaluation. For example, we say that
an expression evaluates to a certain value. Usually the final value is the only reason for
evaluating the expression. However, in some cases, the expression may also produce
side-effects. These are permanent changes in the program state. In this sense, C++
expressions are different from mathematical expressions.

C++ provides operations for composing arithmetic, relational, logical, bitwise, and
conditional expressions. It also provides operators which produce useful side effects,
such as assignment, increment, and decrement.

3.1. Arithmetic Operators


C++ provides five basic arithmetic operators as summarized in Table 3-1.

Table 3-1 C++ Arithmetic operators


Operator Name Example
+ Addition 12 + 4.9 // gives 16.9
- Subtraction 3.98 – 4 // gives -0.02
* Multiplication 2 * 3.4 // gives 6.8
/ Division 9 / 2.0 // gives 4.5
% Remainder (Modulus) 13 % 3 // gives 1
Except for the reminder (%) operator all other arithmetic operations can accept a mix of
integer and real operands. The remainder operator (%) expects integers for both of its
operands. It returns the remainder of integer-dividing the operand. Generally, if both
operands are integers then the result will be an integer. However, if one or both of the
operands are real then the result will be a real (or double to be exact).

When both operands of the division operator (/) are integers then the division is
performed as an integer division and not the normal division we are used to. Integer
division always results in an integer outcome (i.e., the result is always rounded down).
Example: 9 / 2 // gives 4, not 4.5!;
-9 / 2 // gives -5, not -4!;
Unintended integer divisions are a common source of programming errors. To obtain a
real division when both operands are integers, you should cast one of the operands to be
real: int a = 100;
int b = 80;
double c = a / (double) b; // gives 1.25;
It is possible for the outcome of an arithmetic operation to be too large for storing in a
designated variable. This situation is called an overflow. The outcome of and overflow is
machine-dependant and therefore undefined. For example:
Unsigned char k = 10 * 92; // overflow: 920 > 255;

Last Update: Jun. 23, 12 45


Ethiopian Civil Service College Computer Programming & Numerical Methods

It is illegal to divide a number by zero. This results in a run-time division-by-zero failure


which typically causes the program to terminate.

3.2. Relational Operators


C++ provides six relational operators for comparing numeric quantities as shown in
Table 3-2. Relational operators evaluate to 1 (representing the true outcome) or 0
(representing the false outcome).

Table 3-2 Relational operators


Operator Name Example
== Equality 5==5 // gives 1
!= Inequality 5!=5 // gives 0
< Less Than 5 < 5.5 // gives 1
<= Less Than or Equal 5<=5 // gives 1
> Greater Than 5 > 5.5 // gives 0
>= Greater Than or Equal 6.3 > = 5 // gives 1
The operands of a relational operator must evaluate to a number. Characters are valid
operands since they are represented by numeric values. For example (Assuming ASCII
coding):
‘A’ < ‘F’ // gives 1 (is like 65 < 70);
The relational operators should not be used for comparing strings, because this will result
in the string addresses being compared, not the string content. For example, the
expression:
“HELLO” < “BYE”;
causes the address of “HELLO” to be compared to the address of “BYE”. As these
addresses are determined by the compiler ( in a machine-dependent manner), the outcome
may be 0 or may be 1, and is therefore undefined.

C++ provides library functions (e.g., strcmp) for the lexicographic comparison of strings.

3.3. Logical Operators


C++ provides three logical operators for combining logical expression. These are
summarized in Table 3-3. Like the relational operators, logical operators evaluate to 1
and 0.

Table 3-3 Logical operators


Operator Name Example
! Logical Negation ! (5 = = 5) // gives 0
&& Logical And 5 < 6 && 6 < 6 // gives 1
|| Logical Or 5 < 6 || 6 < 5 // gives 1
Logical negation is a unary operator, which negates the logical value of its single
operand. If its operand is nonzero it produces 0, and if it is 0 it produces 1.

Last Update: Jun. 23, 12 46


Ethiopian Civil Service College Computer Programming & Numerical Methods

Logical and produces 0 if one or both of its operands evaluate to 0. Otherwise, it


produces 1. Logical or produces 0 if both of its operands evaluate to 0. Otherwise, it
produces 1.

Note that here we talk of zero and nonzero operands (not zero and 1). In general, any
nonzero value can be used to represent the logical true, whereas only zero represents the
logical false. The following are, therefore, all valid logical expressions:
!20 // gives 0;
10 && 5 // gives 1;
10 || 5.5 // gives 1;
10 && 0 // gives 0;
C++ does not have a built-in Boolean type. It is customary to use the type int for this
purpose instead. For example:
int sorted = 0; // false;
int balanced = 1; // true;
3.4. Bitwise Operators
C++ provides six bitwise operators for manipulating the individual bits in an integer
quantity as listed in Table 3-4.
Table 3-4 Bitwise operators
Operator Name Example
~ Bitwise Negation ~’\011’ // gives ‘\366’
& Bitwise And ‘\011’ & ‘\027’ // gives ‘\001’
| Bitwise Or ‘\011’ | ‘\027’ // gives ‘\037’
^ Bitwise Exclusive Or ‘\011’ ^ ‘\027’ // gives ‘\036’
<< Bitwise Left Shift ‘\011’ << 2 // gives ‘\044’
>> Bitwise Right Shift ‘\011’ >> 2 // gives ‘\002’
Bitwise operators expect their operands to be integer quantities and treat them as bit
sequences. Bitwise negation is a unary operator which reverses the bits in its operands.
Bitwise and compares the corresponding bits of its operands and produces a 1 when both
its bits are 1, and 0 otherwise. Bitwise or compares the corresponding of its operands and
produces a 0 when both bits are 0, and 1 otherwise. Bitwise exclusive or compares the
corresponding bits of its operands and produces a 0 when both bits are 1 or both bits are
0, and 1 otherwise.
Bitwise left shift operator and bitwise right shift operator both take a bit sequence as their
left operand and a positive integer quantity n as their right operand. The former produces
a bit sequence equal to the left operand but which has been shifted n bit positions to the
left. The latter produces a bit sequence equal to the left operand but which has been
shifted n bit positions to the right. Vacant bits at either end are set to 0.
Table 3-5 illustrates bit sequences for the sample operands and results in Table 3-4. to
avoid worrying about the sign bit (which is machine dependent), it is common to declare
a bit sequence as an unsigned quantity:
Unsigned char x = ’\011’;
Unsigned char y = ‘\027’;

Last Update: Jun. 23, 12 47


Ethiopian Civil Service College Computer Programming & Numerical Methods

Table 3-5 How the bits are calculated in 8-bit machine


Example Octal value Bit Sequence Decimal value
x 011 0 0 0 0 1 0 0 1 9
y 027 0 0 0 1 0 1 1 1 23
~x 366 1 1 1 1 0 0 0 0 246
x&y 001 0 0 0 0 0 0 0 1 1
x |y 037 0 0 0 1 1 1 1 1 31
x^y 036 0 0 0 1 1 1 1 0 30
x << 2 044 0 0 1 0 0 1 0 0 36
x >> 2 002 0 0 0 0 0 0 1 0 2
3.5. Increment/Decrement Operators
The auto increment (++) and auto decrement (--) operators provide a convenient way of,
respectively, adding and subtracting 1 to/from a numeric variable. These are summarized
in Table 3-6. The example assumes the following variable definition:
int k = 5;
Table 3-6 Increment and decrement operators
Operator Name Example
++ Auto Increment (prefix) ++k + 10 // gives 16
++ Auto Increment (postfix) k++ + 10 // gives 15
-- Auto decrement (prefix) --k + 10 // gives 14
-- Auto decrement (postfix) k-- + 10 // gives 15
Both operators can be used in prefix and postfix form. The difference is significant.
When used in prefix form, the operator is first applied and the outcome is then used in the
expression. When used in the postfix form, the expression is evaluated first and then the
operator applied.
Both operators may be applied to integer as well as real variables, although in practice
real variables are rarely useful in this form.
How C++ got its name
Now that you understand the full meaning behind the ++ operator, you can probably
guess how C++ got its name. C++ is built upon the C language. C++ adds several
enhancements to C, most of which support Object-Oriented programming. Thus, C++
represents and incremental improvement to C, making the addition of the ++ (the
increment operator) to the name C a fitting name for C++.

Bjarne Stroutstrup initially named C++ “C with Classes.” However, at the suggestion of
Rick Mascitti, Stroutstrup later changed the name to C++. While the new language was
already destined for success, the adoption of the name C++ virtually guaranteed its place
in history because it was a name that every C programmer would instantly recognize.

3.6. Assignment Operator


Assignment operator (=) is used for storing a value at some memory location (typically
denoted by a variable). Its right operand can be a constant or value, which may be the
result of arbitrary expression, whereas the left operand should always be a variable.

Last Update: Jun. 23, 12 48


Ethiopian Civil Service College Computer Programming & Numerical Methods

Example: x = 20; //correct ;


sum = 4 + 2; //correct;
5 = 9; //wrong, 5 is a constant ;
X + 2 = y //wrong, x + 2 is and expression;
An assignment operation is itself an expression whose value is the value stored in its left
operand. An assignment operation can therefore be used as the right operand of another
assignment operation. Any number of assignments can be concatenated in this fashion to
form one expression. For example:
int m, n, p;
m = n = p = 100 //means: n = (m = (p = 100));
m = (n = p = 100) + 2 //means: m = (n = (p = 100)) + 2;
3.7. Compound Assignment operators
Compound assignment operators are variants of the assignment operator, obtained by
combining it with the arithmetic and bitwise operators as summarized in Table 3-7.
Table 3-7 Compound Assignment operators
Operator Example Equivalent to
+= n + = 25 n = n + 25
-= n - = 25 n = n - 25
*= n * = 25 n = n * 25
/= n / = 25 n = n / 25
%= n % = 25 n = n % 25
&= n & = oxF2F2 n = n & oxF2F2
|= n |= oxF2F2 n = n | oxF2F2
^= n ^ = oxF2F2 n = n ^ oxF2F2
<< = n << = 4 n = n << 4
>> = n >> = 4 n = n >> 4
This is equally applicable to other forms of assignments. For example:
M = 100;
m += n = p = 10 //means: m = m + (n = p = 10);

3.8. Conditional Operators


The conditional operator is terriary (takes three operands). It is an abbreviated form of if
… else statement. It uses ? and : symbols with a the general form:
Operand1 ? Operand2 : operand3;
First operand1 is evaluated, which is treated as a logical condition. If the result is
nonzero then operand2 is evaluated and it value is the final result. Otherwise, operand3 is
evaluated and its value is the final result.
int m = 1, n = 2;
int min = (m < n ? m : n); // min receives 1
Note that of the second and the third operands of the conditional operator only one is
evaluated. This may be significant when one or both contain side-effects (i.e., their
evaluation causes a change to the value of a variable). For example, in
int min = (m < n ? m++ : n++);

Last Update: Jun. 23, 12 49


Ethiopian Civil Service College Computer Programming & Numerical Methods

m is incremented because m++ is evaluated but n is not incremented because n++ is not
evaluated.
Because a conditional operation is itself and expression, it may be used as an operand of
another conditional operation, i.e., conditional expression may be nested. For example:
int m = 1, n = 2, p = 3;
int min = (m < n ? (m < p ? m : n): (m < p ? n : p);
// min receives m;
3.9. Comma Operator
Multiple expressions can be combined into one expression using the comma operator.
The comma operator takes two operands. It first evaluates the left operand and then the
right operand, and returns the value of the latter as the final outcome. For example:
int m, n, min;
int mcount = 0, ncount = 0; // …
min = (m < n ? mcount++, m : ncount++, n) ;
Here when m is less than n, mcount++ is evaluated and the value of m is stored in min.
Otherwise, ncount++ is evaluated and the value of n is stored in min.
3.10. sizeof Operator
Sizeof is a useful operator for calculating the size of any data item or type. It takes a
single operand which may be a type name (e.g., int) or an expression (e.g., 100) and
returns the size of the specified entity in bytes. The outcome is totally machine-
dependent.
#include <iostream.h>

int main (void)


{
Cout <<“char size = “ <<sizeof(char) << “bytes\n”;
Cout <<“char* size = “ <<sizeof(char*) << “bytes\n”;
Cout <<“short size = “ <<sizeof(short) << “bytes\n”;
Cout <<“int size = “ <<sizeof(int) << “bytes\n”;
Cout <<“long size = “ <<sizeof(long) << “bytes\n”;
Cout <<“float size = “ <<sizeof(float) << “bytes\n”;
Cout <<“double size = “ <<sizeof(double) << “bytes\n”;

Cout << “1.55 size = “ << sizeof(1.55) << “bytes\n”;


Cout << “1.55L size = “ << sizeof(1.55L) << “bytes\n”;
Cout <<“HELLO” size = “ << sizeof(“HELLO”) << “bytes\n”;
}
When run, the program will produce the following output (on my PC)
char size = 1 bytes
char* size = 2 bytes
short size = 2 bytes
int size = 2 bytes
long size = 4 bytes
float size = 4 bytes
double size = 8 bytes
1.55 size = 8 bytes
1.55L size = 10 bytes
HELLO size = 6 bytes

Last Update: Jun. 23, 12 50


Ethiopian Civil Service College Computer Programming & Numerical Methods

3.11. Operator Precedence


The order in which operators are evaluated in an expression is significant and is
determined by precedence rules. These rules divide the C++ operators into a number of
precedence levels (see Table 3-8). Operators in higher levels take precedence over
operators in lower levels.
Table 3-8 Operator precedence levels
Level Operator Kind Order
Highest :: Unary Both
() [] -> . Binary Left to Right
+ ++ ! * new sizeof( ) Unary Right to Left
- -- ~ & delete
->* .* Binary Left to Right
* / % Binary Left to Right
+ - Binary Left to Right
<< >> Binary Left to Right
< <= > >= Binary Left to Right
== != Binary Left to Right
& Binary Left to Right
^ Binary Left to Right
| Binary Left to Right
&& Binary Left to Right
|| Binary Left to Right
?: Terriary Left to Right
= += *= ^= &= << = Binary Right to Left
-= /= %= |= >> =
Lowest , Binary Left to Right
For example:
a==b+c*d
c*d is evaluated first because * has a higher precedence than + and ==. The result is
then added to b because + has a higher precedence than ==, and then == is evaluated.
Precedence rules can be overridden using brackets. For example, rewriting the above
expression as:
a = = (b + c) * d // causes + to be evaluated before *.
Operators with the same precedence level are evaluated in the order specified by the last
column of Table 3-8. For example, in
a = b + = c
the evaluation order is right to left, so first b += c is evaluated, followed by a=b.
3.12. Type Conversion
A value in any of the built-in types we have seen so far can be converted (type-cast) to
any of the other type. For example:
(char) 122 //converts 122 to a char whose code is 122
(int) 3.14 //converts 3.14 to an int to give 3
(long) 3.14 //converts 3.14 to a long to give 3L
(double) 2 //converts 2 to a double to give 2.0
(unsigned short) 2 //gives 3 as an unsigned short

Last Update: Jun. 23, 12 51


Ethiopian Civil Service College Computer Programming & Numerical Methods

As shown by these examples, the built-in type identifiers can be used as type operators.
Type operators are unary (i.e., take one operand) and appear inside brackets to the left of
their operand. This is called explicit type conversion. When the type name is just one
word, an alternate notation may be used in which the brackets appear around the operand:
int (3.14) //same as: (int) 3.14
In some cases, C++ also performs implicit type conversion. This happens when values of
different types are mixed in an expression. For example:
double d = 1; // d receives 1.0
int i = 10.5 // i receives 10
i = i + d; //means: i = int(double(i) + d)
In the last example, i+d involves mismatching types, so i is first converted to double
(promoted) and then added to d. The result is a double which does not match the type of
i on the left side of the assignment, so it is converted to int (demoted) before being
assigned to i.

3.13. Review Exercise and Assignment on C++ Expressions


Exercises:
1. Write a program and test all the previous examples
2. Add extra brackets to the following expressions to explicitly show the order in
which the operators are evaluated:
(n <= p + q && n >= p – q || n = = 0)
(++n * q-- / ++q – q)
(n | p & q ^ p << 2 + q)
(p < q ? n < p ? q * n – 2 : q / n + 1 : q – n)

3. What will be the value of each of the following variables after its initialization:
double d = 2 * int (3.14);
long k = 3.14 – 3;
char c = ‘a’ + 2;
char c = ‘p’ + ‘A’ – ‘a’;

4. Write a program which inputs a positive integer n and outputs 2 raised to the
power of n.

Assignment: Date Due: November 30, 2007


1. Write a program that will display the area and perimeter of the following
geometric shapes given the necessary input by the user.
a. Circle c. Rectangle e. A regular polygon of side x
b. Triangle d. Trapezium
2. Write a program which inputs three numbers and outputs the message Sorted if
the numbers are in ascending order, and outputs Not sorted otherwise.

Last Update: Jun. 23, 12 52


Ethiopian Civil Service College Computer Programming & Numerical Methods

4. Program Control Statements in C++


This chapter introduces the various forms of C++ statements for composing a program.
Statements represent the lowest-level building blocks of a program. Without loss of
generality, each statement represents a computational step which has a certain side-effect.
(A side-effect can be thought of as a change in the program state, such as the value of a
variable changing because of an assignment).

A running program spends all of its time executing statements. The order in which
statements are executed is called flow control (or control flow), reflecting the fact that the
currently executing statement has the control of the CPU which when completed will be
handed over (flow) to the next, but may be diverted to other paths by branch statements.
Flow control is an important consideration because it determines what is executed during
a run and what is not, therefore affecting the overall outcome of the program.

Like many other procedural languages, C++ provides different forms of statements for
different purposes. Declaration statements are used for defining variables. Assignment-
like statements are used for simple, algebraic computations. Branching statements are
used for specifying alternate paths of execution, depending on the outcome of a logical
condition. Loop statements are used for specifying computations which need to be
repeated until a certain logical condition is satisfied. Flow control statements are used to
divert the execution path to another part of the program. We will discuss these in turn.

4.1. Simple and Compound Statements


A simple statement is a computation terminated by a semicolon. Variable definitions and
semicolon-terminated expressions are examples:
int i; // declaration statement
++i; // this has a side-effect
double d = 10.5; // declaration statement
d + 5; // useless statement! – no side-effect
; // null statement – the simplest statement
Multiple statements can be combined into a compound statement by enclosing them
within braces. For example:
{ int min, i = 10, j = 20;
min = (i < j ? i : j);
Cout << min << ’\n’;
}
Compound statements are useful in two ways:
i. they allow us to put multiple statements in place where otherwise only single
statements are allowed
ii. they allow us to introduce a new scope in the program.
Because a compound statement may contain variable definitions and defines a scope for
them, it is also called a block. The scope of a c++ variable is limited to the block
immediately enclosing it.

Last Update: Jun. 23, 12 53


Ethiopian Civil Service College Computer Programming & Numerical Methods

4.2. Selection Statement


The programs we have seen so far each consists of a simple list of statements to be
executed in the order given. But sometimes it is required to have conditions to be tested
so that some of the statements may or may not be executed, depending on the result of the
test. A statement that contains condition for the execution of the statements is known as
conditional statement.

4.2.1. If statement
It is sometimes desirable to make the execution of a statement dependant upon a
condition being satisfied. If statement provides a way of expressing this, the general
syntax is:
if (Logical_Expression)
statement;
Where Logical_Expression is an integer expression and statement is any executable
statement. If the test of condition is evaluated to 1 (true), then the statement is executed.
When the expression is evaluated to 0, it is considered as false value; in which case the
statement is skipped. Any non-zero value is taken to be true. For example:
#include <iostream.h>
main ()
{
int num1, num2;
cout << “ Enter the two numbers: “;
cin >> num1 >> num2;
if (num1%num2 = = 0)
cout << num1 << “ is divisible by “ << num2 << endl;
return 0;
}
This program gets two numbers from the keyboard and checks if the condition,
num1 % num2 == 0, is true. If it is true then the output statement is executed. Otherwise,
it skips over the output statement.

If … else statement:
If … else is a variant form of the if statement to specify two alternative statements: one
which is executed if a condition is satisfied and one which is excited if the condition is
not satisfied. The syntax is:
if (Logical_Expresson)
Yes_Statement;
else
No_statement;
If the Logical_Expression evaluates to true then the statement after if, Yes_Statement, is
executed; otherwise the statement after else, No_Statement, is executed.

Last Update: Jun. 23, 12 54


Ethiopian Civil Service College Computer Programming & Numerical Methods

Using if… else the above program can be rewritten in more complete sense:
#include <iostream.h>
main ()
{
int num1, num2;
cout << “ Enter the two numbers: \n “;
cin >> num1 >> num2;
if (num1%num2 == 0)
cout << num1 << “ is divisible by “ << num2 << endl;
else
cout << num1 << “ is not divisible by “ << num2 << endl;
return 0;
}
Any expression that results in a zero or non-zero value can be used to control the if. For
example the following program reads two integers from the keyboard and displays the
quotient.
// divide the first number by the second
#include <iostream.h>
int main ()
{
int num1, num2;
cout << “ Enter the two numbers: “;
cin >> num1 >> num2;
if (num2) cout << num1/num2 << ‘\n’;
else cout << “ can not divide by zero. \n“;
return 0;
}
Note: it is not necessary (and would be considered as bad programming style) to write
this if as:

if (num2) cout << num1/num2 << ‘\n’;

It is possible to make test for multiple conditions using more than one if statements. In
which case it is called nested if statement. Two or more conditions are connected using
conditional connectors. && for and, || for or.

4.2.2. Conditional expression


The conditional operator is terriary (takes three operands). It is an abbreviated form of if
… else statement. It uses ? and : symbols with a the general form:
Operand1 ? Operand2 : operand3;
For example:
i. max = num1 > num2 ? num1 : num2;
ii. num1 % num2 = = 0 ? cout << num1 << “ is divisible by “
<< num2 : cout << num1 “ is not divisible by “ << num2;

Last Update: Jun. 23, 12 55


Ethiopian Civil Service College Computer Programming & Numerical Methods

4.2.3. Switch Statement


Switch statement is used to code mutually exclusive alternatives that are expressed by
multiple if…else statements. Switch statement is ideal for testing a single expression
against a series of possible values and executing the code associated with the matching
case constant. The general syntax is:
switch ( <expression> )
{
case const_1:
stmt_list_1;
case const_2:
stmt_list_2;
… .
case const_n:
stmt_list_n;
default:
stmt_list;
}
First expression (called the switch tag) is evaluated, and the outcome is compared to each
of the numeric constants (called case labels), in the order they appear, until a match is
found. The expression should evaluate to integer type or it should be character constant.
The value is checked for matching case constant. If the match is found then the statement
list of the matching constant is executed. The final default case is optional and is
exercised if none of the earlier cases provide a match. Note that switch statements can be
nested. For example:
main()
{
char ch;
cout << “Enter a letter: “;
cin >> ch;
switch (ch)
{ case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << "It's a vowel!";
break;
default:
cout << "It's a consonant!";
}
cout << “ Now out of the switch statement block”;
return 0;
}
The keyword break is used within a case statement to move the flow of control out of
switch block. In this example, break appears as the last statement in each case constant
except the default and will cause program execution to move to the line that prints "Now
out of the switch statement block!". The break statement was left out of the default part

Last Update: Jun. 23, 12 56


Ethiopian Civil Service College Computer Programming & Numerical Methods

because by that point in the code the switch block was ending, and there is no need of
using an explicit command to exit the switch.

It should be obvious that any switch statement can also be written as multiple if-else
statements. However, preference should be given to the switch version whenever
possible. The if-else approach should be reserved for situations where a switch cannot
do the job (e.g., when the conditions involved are not simple equality expressions, or
when the case labels are not numeric constants).

4.3. Iteration (or Looping Statements)


Iteration is repetition of an action or group of actions in a program. Without the ability to
loop or iterate through a set of values, our ability to solve real-world problems would be
severely limited. C++ has three flavors for repeating a statement or group of statements in
a program: while, do while and for.

4.3.1. While statement

The while statement (also called while loop) provides a way of repeating a statement
while a condition holds. The test condition for the loop is made at the top. Its syntax is:
while (<condition>)
<statement>;
First condition (also called the loop condition) is evaluated. If the outcome is nonzero the
statement (called the loop body) is executed and the whole process is repeated.
Otherwise, the loop is terminated. For example:
While (1)
; // Loops forever
*****************************************************************

i = n;
while ( n >= 1)
i *= --n; // Computes n factorial
It is not unusual for a while loop to have an empty body (i.e., a null statement). The
following loop, for example, sets n to its greatest odd factor.
While ((n % 2 = = 0) && (n /= 2))
;
Here the loop condition provides all the necessary computation, so there is no real need
for a body. The loop condition not only tests that n is even, it also divides n by 2 and
ensures that the loop will terminate should n be zero.

4.3.2. Do…While statement

The do…while statement (also called do loop) is similar to the while statement,
except that its body is executed first and then the loop condition is examined. The general
syntax is:

Last Update: Jun. 23, 12 57


Ethiopian Civil Service College Computer Programming & Numerical Methods

do
<statement>;
while (<condition>);
First statement is executed and then condition is evaluated. If the outcome of the latter is
nonzero then the whole process is repeated. Otherwise, the loop is terminated.
Example: i = n;
do
i *= --n;
while (n>1);
The do loop is less frequently used than the while loop. It is useful for situations where
we need the loop body to be executed at least once, regardless of the loop condition.

Unlike the while loop, the do loop is never used in situations where it would have a
null body. Although a do loop with a null body would be equivalent to a similar while
loop, the latter is always preferred for its superior readability.

4.3.3. For statement

The for statement (also called for loop) is similar to the while statement, but has two
additional components: an expression which is evaluated only once before everything
else, and an expression which is evaluated once at the end of each iteration. The general
form of the for statement is:
for (<initialize> ; <exit_ test> ; <update>)
<statement>;
Using braces we can have more than one statement to iterate. The for loop localizes
initialization, test for exit, and update - the variable to test, in one general syntactic
construct.

For loop is expressible as a while loop as shown below:


<initialize>;
while (<exit test>)
{
<loop body>;
<update>;
}
The most common use of for loops is for situations where a variable is incremented or
decremented with every iteration of the loops. For example:
sum = 0
for (i = 1, i <=1; ++i) /* calculates the sum of all
sum += i; integers from 1 to n */
It is also possible to declare the loop variables in the <initialize> section of the for
loop. For example:
for (int i = n; n>2; n--)
i *= (n-1); // Computes n factorial

Last Update: Jun. 23, 12 58


Ethiopian Civil Service College Computer Programming & Numerical Methods

Contrary to what may appear, the scope for i is not the body of the loop, but the loop
itself. Scope wise, the above is equivalent to:
int i
for (i = n; n>2; n--)
i *= (n-1);
However, the three loop headers are optional. For example, removing the initialize and
update expressions:
for (; i != 0;) // is equivalent to: while (i != 0)
something;
Similarly, removing all the expressions gives us an infinite loop. This loop’s condition is
assumed to be always true:
for (;;) // loops forever
Something;
Because loops are statements, they can appear inside other loops. In other words, loops
can be nested. For example:
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= 3; ++j)
cout << ‘(‘ << i << ‘,’ << j << “) , ”;
produces the products of the set {1, 2, 3} with itself, giving the output:
(1,1) , (1,2) , (1,3) , (2,1) , (2,2) , (2,3) , (3,1) , (3,2) , (3,3) ,

*Recursion: is when a function calls itself.


4.4. Jump statement

Of course, it is not always easy to write all of your for, while and do…while loops
so that they are easy to read and yet the loops terminate on exactly the right pass through
the loop. C++ makes it easier to jump out of loops and to control other areas of program
flow with its break and continue statements.

4.4.1. continue Statement

The continue statement terminates the current iteration of a loop and instead forces
program control to go to the next iteration. In while and do loops, the next iteration
commences from the loop condition. In a for loop, the next iteration commences from
the loop expression. It is an error to use the continue statement outside a loop. For
example, a loop which repeatedly reads in a number, processes it but ignores negative
numbers, and terminates when the number is zero, may be expressed as:
Is Equivalent to

do { do {
cin >> num; cin >> num;
if (num < 0) continue; if (num < 0) {
// process num here … // process num here…
} while (num != 0); } while (num != 0);

Last Update: Jun. 23, 12 59


Ethiopian Civil Service College Computer Programming & Numerical Methods

A variant of this loop which reads in a number exactly n times (rather than until the
number is zero) may be expressed as:
for (i = 0; i < n; ++i) {
cin >> num;
if (num < 0) { // causes a jump to: ++i
// process num here …
}
When the continue statement appears inside nested loops, it applies to the loop
immediately enclosing, and not to the outer loops. For example, in the following set of
nested loops, the continue applies to the for loop, and not the while loop:
While (more) {
for (i = 0; i < n; ++i) {
cin >> num continue;
if (num < 0) { // causes a jump to: ++i
// process num here …
}
//etc…
}
4.4.2. break Statement

Just as the continue statement, a break statement is used to move program control to
immediately after the end of a loop. A break statement may appear inside a loop
(while, do, or for) or a switch statement. It causes a jump out of these constructs,
and hence terminates them. Earlier in this chapter, you saw how the break statement is
used to exit a switch statement. A break statement only applies to the loop or switch
immediately enclosing it. It is an error to use the break statement outside a loop or a
switch.

For example, suppose we wish to read in a user password, but would like to allow the
user a limited number of attempts:
for (i = 0; i < attempts; ++i) {
cout << “please enter your password: “;
cin >> password;
if (Verify(password)) //check password for correctness
break; // drop out of the loop
cout << “incorrect!\n”;
}
Her we have assumed that there is a function called Verify which checks a password
and returns true if it is correct, and false otherwise. Rewriting the loop without a break
statement is always possible by using additional logical variable (verified) and adding
it to the loop condition:
verified = 0;
for (i = 0; i < attempts && !verified; ++i) {
cout << “please enter your password: “;
cin >> password;
verified = Verify(password);
if (!verified) //check password for correctness

Last Update: Jun. 23, 12 60


Ethiopian Civil Service College Computer Programming & Numerical Methods

break; // drop out of the loop


cout << “incorrect!\n”;
}
4.4.3. goto Statement

The goto statement provides the lowest-level of jumping. Using goto (for local
jumping) program control can be sent to anywhere inside the body of the program to
repeat execution of the statement(s).It has the general form:
goto label;
Where label is an identifier which marks the jump destination of goto. The label should
be followed by a colon and appear before a statement within the same function as the
goto statement itself. For example, the role of break statement in the for loop in the
previous section can be emulated by a goto:
for (i = 0; i < attempts; ++i) {
cout << “please enter your password: “;
cin >> password;
if (Verify(password)) //check password for correctness
goto out; // drop out of the loop
cout << “incorrect!\n”;
}
out:
//etc…
Because goto provides a free and unstructured form of jumping (unlike break and
continue), it can be easily misused.

4.4.4. return Statement

The return statement enables a function to return a value to its caller. It has the general
form: return expression;

where expression denotes the value returned by the function. The type of this value
should match the return type of the function. For a function whose return type is void,
expression should be empty: return;

The only function we have discussed so far is main, whose return type is always int. the
return value of main is what the program returns to the operating system when it
completes its execution.

When a function has a non-void return value (as in the following example), failing to
return a value will result in a compiler warning. The actual return value will be undefined
in this case (i.e., it will be whatever value which happens to be in its corresponding
memory location at the time.)
int main (void)
{
cout << Hello World\n”;
return 0;
}

Last Update: Jun. 23, 12 61


Ethiopian Civil Service College Computer Programming & Numerical Methods

4.5. Review Exercise and Assignment on C++ Statements


Exercises:
1. Write a program to solve a quadratic equation.
2. Write a program which inputs a person’s height (in cm) and weight (in kg) and
outputs one of the messages: underweight, normal, or overweight, using the
criteria:
Underweight: weight < height / 2.5
Normal: height / 2.4 <= weight <= height / 2.3
Overweight: height / 2.3 < weight

3. Assume that n is 20, what will the following code fragment output when
executed?
if (n >= 0)
if (n < 10)
cout << “n is small\n”;
else
cout << “n is negative\n”;

4. Write a program which inputs an integer value, checks that it is positive, and
outputs its factorial. Use the formula:
factorial (0) = 1
factorial (n) = n * factorial (n – 1)

5. Write a program for the Magic Number game. The program generates a random
number, prompts for your guess, and prints the message ** Right ** if you guess
the magic number. Include the header <cstdlib.h>.

Assignment: Date due December 10, 2007


1. Write a program which inputs a date in the format dd/mm/yy and outputs it in
the format month dd, year.
For example, 25/12/61 becomes: December 25, 1961.

2. Write a program which produces a simple multiplication table of the following


format for integers in the range 1 to 9:
1 x 1 = 1
1 x 2 = 2

9 x 9 = 81

3. Write a program for the following problems


a. To calculate the LCM and GCF of two numbers,
b. To check whether a number is prime or not.

4. Write a program that reverses the digits of a given positive integer.

Last Update: Jun. 23, 12 62


Ethiopian Civil Service College Computer Programming & Numerical Methods

5. Array
This section introduces the array and illustrates their use for defining their use for
defining variables.

An array consists of a set of objects (called its elements), all of which are of the same
type and are arranged contiguously in memory. It can also be considered as a variable
that can hold more than one value. Array is a series of elements of the same data type
(homogenous elements) placed consecutively in memory that can be individually
referenced by adding an index to a unique name. When you first studied variables, you
learned that a variable is like a box in memory that holds a single value. Now, if you take
a bunch of these boxes and put them together, you get an array.
Example: data Int Int Int Int contains 4 integer elements
In general, only the array itself has a symbolic name, not its elements. Each element is
identified by an index which denotes the position of the elements in the array. The
number of elements in an array is called its dimension. The dimension of an array is fixed
and predetermined; it cannot be changed during program execution.

Arrays are suitable for representing composite data which consist of many similar,
individual items. Examples include: a list of names, a table of world cities and their
current temperatures, or the monthly transaction for a bank account.

There are two methods of array implementation based on memory allocation:


1. Static Array implementation: Memory allocation for the variable is made at
compilation time. Will be dealt in this chapter.
2. Dynamic array implementation: Memory allocation for the variables is made at
run time. This will be treated in advanced courses of C++.
5.1. Static Array Implementation
5.1.1. One dimensional array
An array is defined by providing its type, name, and size; for example an integer array
with size 50 is declared as:
Data Type Identifier [Dimension]
int myarray[50];
char ch[10];
double a[5];

Fifty blocks are reserved in memory for this array. The size of each block is
determined by the data type; four bytes in this case.
Elements (values) of an array are accessed by using indexes with array name. If
the size of an array is n, then its n blocks are addressed using indexes from 0 to n-
1. For example, the first element of the above array is named by myarray[0], the
second one by myarray[1], ..., and the last element(block) by myarray[49].
The values to store in the above array should all be of data type integer.
If an array is defined as a global variable it will be automatically initialized to zero,
otherwise all the elements will be garbage values.

Last Update: Jun. 23, 12 63


Ethiopian Civil Service College Computer Programming & Numerical Methods

Arrays can be initialized during declaration with a single initializer list. In this case, its
size specifier may be omitted from its declaration. Consider the following declaration,
double a[5] = {15.5, 25.5, 35.5, 45.5, 55.5};
is equivalent to the declaration,
double a[] = {15.5, 25.5, 35.5, 45.5, 55.5};
In the later case the size is determined to be the number of values in the initializer list. If
the array has more elements than values listed in the initializer list, then the remaining
elements are initialized to zero. Un-initialized values in a list will be set to zero as:
double a[5] = {15.5, 25.5}; //means a[5] = {15.5, 25.5, 0, 0, 0}
double a[5] = {0}; //is equivalent to a[5] = {0, 0, 0, 0, 0}

Example 1.
/* The following program initializes an array and displays its elements in the reverse
order.*/
main ( )
{int b[5] = {10, 20, 30, 40, 50};
for (int i = 4; i >= 0; i--)
cout << " a[" << i << "] = " << a[i];}
Running this program, displays the numbers in reverse order.
N.B. This has not changed the physical allocation of the data in the memory.

Example 2.
/* The following program rearranges values in the array in increasing order. It uses
the bubble sort algorithm that proceeds through a sequence of iterations, each time
moving the next largest item into its correct position. On each iteration,
consecutive elements are compared, pushing the larger element forward.*/
main ( )
{
int a[20],tmp,k;
// The maximum number of values that you can store
//in the array a is 20
clrscr();
cout << "Enter the size and your data: \n";
cin >> k;
for (int i = 0;i < k;i++)
cin >> a[i];

for (int i = k;i > 0;i--)


for (int j = 0;j < i;j++)
if (a[j] > a[j+1])
{//The satements in this block are used to swap the values.
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
cout << "After sorting the data looks like: \n";
for(int i = 0;i < k;i++)
cout << a[i] << " ";
}

Last Update: Jun. 23, 12 64


Ethiopian Civil Service College Computer Programming & Numerical Methods

Example 3.
/* The following program searches for a value and tells the location of the value, if it is
found in the array. It uses the linear search algorithm that starts at the beginning of
an array and inspect each element, one after another, until the object is found or the list
is exhausted.*/
main ( )
{
int a[]={50,100,2,70,40,30,79,1,10,7,8,25},target, found, loc;
clrscr();
cout << "The value to search: ";
cin >> target;
found = loc = 0;
while(!found && loc < 8)
found = (a[loc++] == target);
if(found)
cout << target << " is at a[" << --loc << "]\n";
else
cout << target << " is not in the list. \n";
5.1.2. Multidimensional Arrays
The arrays we have seen so far are all one - dimensional. An array may have more than
one dimension (i.e., two, three, or higher). Multidimensional array is an array of arrays.
The organization of the array in memory is still the same (a contiguous sequence of
elements), but the programmer’s perceived organization of the elements is different.
Data Type Identifier [Dim_1][Dim_2]…[Dim_n]
For example,
int a[10][5]; //two dimensional (10 & 5) integer array
char x[3][5][4]; // three dimensional character array
In the first array each block of the first ten blocks points to an array of five blocks. To
access a certain element, indices corresponding to each dimension should be supplied.

Example:
/* The following program inputs names of twenty students in a two-dimensional array
and then displays their names. */

main ()
{
char name[20][25];
for(int i = 0;i < 20;i++)
cin.getline(ch[i],15);
for(int i = 0;i < 20;i++)
cout << ch[i] << endl;
}

Last Update: Jun. 23, 12 65


Ethiopian Civil Service College Computer Programming & Numerical Methods

5.2. Review Exercise and Assignment


Exercises:
1. Write a program that calculates the sum and average of maximum of 20 numbers
to be entered by the user.
2. Write a program that adds two matrices, entered by the user. What is the address
of the ith and jth element stored in column

Assignment:
1. Write a program that multiplies two matrices, entered by the user.
2. Write a program that calculates the determinant of n x n matrix.

Last Update: Jun. 23, 12 66


Ethiopian Civil Service College Computer Programming & Numerical Methods

6. Functions
This section describes functions as one of the main building blocks of C++ program.
Functions are sub programs in C++. It is a very good technique to approach big problems
by dividing them into sub-problems.

A function provides a convenient way of packaging a computational recipe, so that it can


be used as often as required. A function definition consists of two parts: interface and
body. The interface of a function (also called its prototype) specifies how it may be used.
The body of a function contains the computational steps (statements) that comprise the
function.

Using a function involves ‘calling’ it. A function call consists of the function name
followed by the call operator brackets ‘( )’, inside which zero or more comma-separated
arguments appear. The number of arguments should match the number of function
parameters. Each argument is an expression whose type should match the type of the
corresponding parameter in the function interface.

When a function is executed, the arguments are first evaluated and their resulting values
are assigned to the corresponding parameters. The function body is then executed.
Finally, the function return value (if any) is passed to the caller.

Since a call to a function whose return type is non-void yields a return value, the call is
an expression and may be used in other expressions. By contrast, a call to a function
whose return type is void is a statement.

6.1. Modularization
To see how modularization makes things easy, take a look at the following code:
Example 1.
#include <iostream.h>
#include <conio.h>
main()
{
int A[3],B[3],C[3],sum[3],diff[3];
clrscr();
cout<<”Enter the elements of the arrays:\n”;
cout<<”The first array(A)”;
for (int i=0; i<3; i++)
cin>>A[i];
cout<<”The second array(B)”;
for (i=0; i<3; i++)
cin>>B[i];
cout<<”The third array(C)”;
for (i=0; i<3; i++)
cin>>C[i];
for (i=0; i<3; i++)
{
sum[i] = A[i]+B[i]+C[i];
diff[i] = A[i]-B[i]-C[i];

Last Update: Jun. 23, 12 67


Ethiopian Civil Service College Computer Programming & Numerical Methods

}
cout<<”Here is the sum of the elements of the three
arrays\n”;
for (i=0; i<3; i++)
cout<<sum[i]<<endl;
cout<<”Here is the difference of the elements of the
three arrays\n”;
for (i=0; i<3; i++)
cout<<diff[i]<<endl;
getch();
return 0;
}
The above code (program) is designed to read elements of three given arrays from the
user and then display the sum and the difference of the elements of the arrays
respectively. The program is actually doing what it intends to do. But there are some
‘ugly’ points that are visible if you have the eyes to do so. Can you see any ugly point?

You might say there are no ‘ugly’ points seen. You may feel that this is a very organized
and structured program. But unfortunately, it is not! We can show this by explaining
about modularization.

Programming in its general view refers to writing programs (which are set of
instructions) for solving a certain problem. We have been discussing in the previous
sections that the problems should be well defined and understood before writing any
programs to solve them.

This all is right except missing one important explanation about modular (structured)
programming which is one of the techniques of programming. Structured programming
refers to the way of writing programs as a group of modules instead of a very long block.
A module is a sub program which will be probably used by other sub programs and
which will probably use other modules.

It is always advisable to divide a given big problem into sub problems. It is again wise to
approach each sub problems separately. In effect, small problems which could be easily
solved will be left in hand. Then write the sub programs for the sub problems and finally
integrate them to form a bigger software.

In C++, sub programs are called functions. You will find different names for sub
programs in different programming languages (like procedures in Pascal).

So far, we have seen (written) programs consisting of only one function (which is the
main function and is mandatory). But after all, what is the need for modularization? Some
of the quite obvious reasons are:
a) Programs will be easily manageable: Attacking a problem in this way makes things
easy. Once you divide the problem into sub problems, it is possible to think of
the smaller problems separately with full attention and potential. Complexity is
one of the main reasons why writing programs is so difficult and eventually
complexity will be much less when using modularization.

Last Update: Jun. 23, 12 68


Ethiopian Civil Service College Computer Programming & Numerical Methods

b) Decreasing Redundancy: If there are similar things to be done at different points of


the program and if you are not using functions, you will be forced to repeat the
instructions at each point at which the action is to be performed. Redundancy is
not a desired feature of computer programs as it creates complexity and result
in having programs which are very difficult to trace and maintain..
c) Reuse: If we were like the old programmers who always wrote programs from
scratch, we would not be able to write even a few programs by taking years.
Fortunately, we are reusing different modules to develop programs and we are
quite ‘faster’ than them. For instance, there is a predefined function named
sqrt(number) which returns the square-root of the specified number which is
defined in the <math.h> header file. Whenever you want to obtain the square
root of a number, you are going to use (invoke) this function as shown in the
following small program.

#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
float a = sqrt (4);
cout<<a;
getch();
}
Sit and try to write a module to calculate the square-root of a given number
from scratch and see how much time you will spend.
d) Easy testing and maintenance: Testing is included as one of the steps when
developing programs. People (especially beginners) may underestimate testing
but it is very basic. To test a certain program, different test values (inputs) and
the corresponding expected outputs would be prepared and then provided
(entered) for the program observing the result and determine whether it is the
expected one or not.
It would be good to have a program which is used to do one main well defined
task instead of multiple tasks for the testing process. The same holds true for
maintaining programs. Obviously, after testing if you realize that there is an
error, it will be simple to identify and correct problems when the code is
compact and stand for the same task.
In summary, to correct mistakes, problems should first be identified. Identification of
problems is easier if the program is to be developed as set of functions instead of being a
very long block.

In the previous example, we may criticize points like the following in relation to
modularization:
a) So many tasks but only one block: As can easily be seen, there exist several tasks
included in the code. Here is how the code is organized:
i) Read array A,
ii) Read array B,
iii) Read array C,
iv) Add the elements of the arrays,

Last Update: Jun. 23, 12 69


Ethiopian Civil Service College Computer Programming & Numerical Methods

v) Subtract the elements of the arrays,


vi) Display the sum (which is previously calculated), and
vii) Display the difference (which is previously calculated).
All these tasks are coded in only one function which is the only function in the
program. Consider the situation where you see a wrong sum. It will be a tiny
mistake somewhere in the program. Correcting the mistake is not difficult
rather finding out where the mistake exactly is not easy.
Where is the problem? When reading array A? or array B? or array C? Rather,
is the problem when calculating the sum or the difference? Or is the defect
when displaying?
It is really necessary to trace the program starting from the beginning till the
end to answer the questions. But if we modularize the program, things will be
easier because at a time, you will think about one of the tasks.
b) Redundancy and poor reuse: Observing the program given in the example, one may
come up with very similar tasks. Think about the first three tasks listed above. These
tasks are all about reading arrays. To read each array, a similar for loop is inserted
repeatedly, which is a very bad habit. It would have been better if a separate function
is developed for this task and the function is called at each point needed. Actually we
will soon discover and convince ourselves that it is still good to have functions even
if they will be called only once in the current program.
How should we modularize programs?
It is actually difficult to come up with a ‘formula’ to make programs modularized
(structured), but the following few points will help you to modularize your programs:
 A function should be used to perform one primary task: When thinking about
problems which are supposed to be solved by computer programs, try to identify
primary tasks and list down them. Then think about each task thoroughly and
determine whether they should be modularized further. In fact it is possible to
accommodate more than one task to be solved by one function, but this is not
really recommended.
 Make the main function only a coordinator: It is always better not to write code for
any of the tasks in the main function. The main function will call functions in
their right order. Note that this doesn’t mean that all functions are called by the
main function. Some might call others.
Recall that in the above example we have identified the tasks and listed them down.
Therefore, the skeleton of the program when it is modularized could possible be:
ReadArrayA() ReadArray()
{ {
… …
} }
ReadArrayB() AddArrays()
{ {
… …
} }
ReadArrayC() SubtractArrays()
{ {
… …
} }
AddArrays() Display()

Last Update: Jun. 23, 12 70


Ethiopian Civil Service College Computer Programming & Numerical Methods

{ {
… …
} }
SubtractArrays() main()
{ {

ReadArray();//for array a
}
ReadArray();//for array b
DisplaySum()
ReadArray();//for array c
{
… AddArrays();
} SubtractArrays();
DisplayDiif() Display();// for the sum
{ Display(); //for the difference
… }
}
main()
{
ReadArrayA();
ReadArrayB();
ReadArrayC();
AddArrays();
SubtractArrays();
DisplaySum();
DisplayDiff();
}
In the first skeleton, we mapped each task to one function. It is not always appropriate to
do so from efficiency point of view. Before mapping each task to one function, look for
similar tasks. For example ReadArrayA(), ReadArrayB(), ReadArrayC()
are very similar and can be made to be accommodated in one function. A function named
ReadArray() could be defined and the main() function could call it three times for
each array. The same is true for displaying the sum and difference. We could have one
generic function name Display() and call it two times. As a result, we will have a
compact skeleton like the second one.

Avoid any confusion which could be created as a result of calling the same function for a
little bit different tasks by believing that there is a way to make the ‘callings’ different. I
assure you there is!

There are two types of functions: Pre-defined and user-defined functions.

6.2. Pre-defined functions


These are functions which are already written and developed by the producers of
compilers. We have been using pre-defined functions when writing programs in the
previous sections.
Example 2:
In the following program, the pow function (which is a pre defined function) is used.
#include <iostream.h>
#include <conio.h>
#include <math.h>
main()
{

Last Update: Jun. 23, 12 71


Ethiopian Civil Service College Computer Programming & Numerical Methods

int a=2,b=3,c;
c = pow(a,b);
cout<<c;
getch();
return 0;
}
The pow(a,b) function is used to calculate ab (a the power of b). This function is not
part of the C++ language itself. Instead it is written and be ready for use by the producers
of the compilers. These kinds of functions which are readily available are called pre-
defined functions.

But where are these functions defined? The answer is straightforward. They are defined
in header files which are included at the beginning of the program. In actual terms,
header files contain built-in (pre-defined) function prototypes.

The direct implication of the above statement forces us to include the header file
<math.h> to use the pow function. Several built in functions exist and it is a good habit
to dig into header files to put the functions in mind so that you will be able to explore the
vast ocean of functions in C++.

6.3. User defined Functions


User defined functions are defined by programmers (you!). It is very possible to prepare
your own function, give it a valid (and of course relevant) name and use it in your
programs.

6.3.1. Defining a user-defined functions


At this moment, you must think a C++ program to be set of functions from which one is
necessarily the ‘main’ function. Much is said about modularization at the beginning of
this section. Once you are ready to write functions (meaning once you divide your
problem into sub problems), you need to know the following important points to begin
writing your functions.

A function definition consists of the interface (also called head) and the body of the
function. The interface of the function includes important information about the function
which are listed below:
i. The function’s name: Any function must have a name so that it can be referenced.
The rules which we rely on to name variables could directly be applied to function
names.
Example 3: Read_Input()

ii. The function’s return type: Even if there are functions which do not return any thing,
most functions do return a certain value to their calling functions. If a function
returns a value, then the value should be one of the data types in C++. This type is
needed to be stated explicitly in the head of the function.

Last Update: Jun. 23, 12 72


Ethiopian Civil Service College Computer Programming & Numerical Methods

iii. Parameters: Parameters are zero or more identifiers which are used to pass
information between functions in different ways.
The body of the function will contain all the C++ statements used for accomplishing the
task of the function. Pair of braces ‘( )’ is used to enclose the body of the function.
Example 4:
int add (int num1, int num2)
{
int s;
s = num1+num2;
return s;
}
Note: the above is not a complete C++ program.

Let’s discuss about the above function. The first line is the head of the function:
int add (int num1, int num2)
Return name parameters
type

What we understand from the head of the function is that,


 The name of the function is ‘add’.
 The function will return a value which is an int type.
 The function has got two parameters represented by two int variables called
num1 and num2.
The task that is accomplished by the function is to add the numbers which come through
the parameters num1 and num2 and finally return the sum. This task is carried out when
the three C++ statements within the block (which comprises of the body of the function)
are executed. The return statement is used to return any value which is supposed to be
returned by the function (if any). In this particular case, the sum of the numbers (num1
and num2) which is represented by the variable s is returned.

If you feel that there is nothing to be returned, the function’s return value could be called
‘void’. This implies void functions do not need a return statement.

If a function doesn’t have any parameters, put two brackets ‘(())’ at the end of the
function’s name. It is not correct to leave a function name without brackets. If the worst
comes, the brackets might be empty.

6.3.2. Using a user-defined functions


Technically when we use a function in our program, it will be stated that the function is
called. A function call is a way of telling the processor of the computer to jump to the
first statement in the specified function, and continued the execution till the end of the
function. Later when the execution of the function is completed, control will be returned
back to the calling function and execution will be continued starting from the very next
statement with in the calling function.

Last Update: Jun. 23, 12 73


Ethiopian Civil Service College Computer Programming & Numerical Methods

A function call simply consists of the function’s name followed by brackets in which zero
or more arguments are included. Arguments are values which will be sent to the function
and later copied to parameters. The number of parameters and arguments must be equal.
Note that arguments are directly mapped to parameters based on their order.

Moreover, void functions and functions having return values are called a little bit
differently. The value that is returned from a function should be (it is not actually must
be) used in an expression in the calling function’s side. But, void functions will be simply
called as a C++ statement.

The third point regarding function calling is that any function can call any other function
if the function to be called is declared or defined prior to its usage. Finally, in C++, it is
not possible to define a function within the body of another function.

Let’s see some examples:


Example 5: Example 6:
#include <iostream.h> #include <iostream.h>
#include <conio.h> #include <conio.h>

int add (int num1, int num2) int add (int num1, int num2)
{ {
int s; int s;
s = num1+num2; s = num1+num2;
return s; return s;
} }
//********************** //**********************
void main() void main()
{ {
int sum; int a,b,sum;
sum = add(2,5); cout<<”Enter two numbers:”;
cout<<sum; cin>>a>>b;
} sum = add(a,b);
cout<<”The sum of the
numbers is “<<sum;
}
In the above examples, observe the following points:
 The function add (its definition) comes before the main() function is defined. The
reason is that the main function will call the add function and at the time of calling,
the function has to be defined or declared. If you rearrange the functions (main first
and add second), without adding anything, then the compiler will report an error
message. Particularly Turbo C++ compiler displays the following error message
whenever such a problem exists:
Function ‘function name’ should have a prototype.
(Function add should have a prototype.)

This implies: because of the rearrangement, something else is needed which the
compiler called it ‘prototype’. We are going to discuss about prototypes later on. But
for the time being let’s adhere to the assumption that to be called functions should be
placed before the caller function.

Last Update: Jun. 23, 12 74


Ethiopian Civil Service College Computer Programming & Numerical Methods

 In Example 5, arguments are numbers which are sent to the function. Let’s study the
statement at which the function is called.
sum = add(2,5);

This is actually an assignment statement which assigns the value returned by the
expression add(2,3) to the variable sum. When this statement is processed, control
will automatically be taken to the function add. The arguments 2 and 3 are copied to
the parameters num1 and num2 respectively. Then the sum of the numbers is
calculated and is stored in the local variable s inside the body of the function.

Finally, when return s is executed, two things will happen:


I. The value of the variable s is returned to the calling function (as a return
value of the expression which consists of the function call).
II. Control is returned back to the calling function (e.g. main()) and execution
will be resumed from the very next statement after the function calling.

 The same happens in Example 6 except arguments are obtained from users and are
sent through variables.

sum = add(a,b);

The values of the variables a and b will be copied to the parameters num1 and num2
respectively and their sum will be returned.
Example 7:
#include <iostream.h>
#include <conio.h>

//-----Display_Menu----------------------------
void Display_Menu()
{
cout<<”*******MENU*******”);
cout<<”\n\t1. Add Numbers”;
cout<<”\n\t1. Subtract Numbers”;
cout<<”********************”);
}
//--------add----------------------------------
int add (int num1, int num2)
{
int s;
s = num1+num2;
return s;
}

//--------subtract---------------------------------
int subtract(int num1, int num2);
{
return num1-num2;
}

//--------main--------------------------------------
void main()
{

Last Update: Jun. 23, 12 75


Ethiopian Civil Service College Computer Programming & Numerical Methods

int n1,n2,choice;
Display_Menu();
cin>>choice;
cout<<”Enter two numbers:”;
cin>>n1>>n2;
if (choice==1)
cout<<”The sum of the numbers is “<<add(n1,n2);
else
cout<<”The difference between the numbers is
“<<subtract(n1,n2);
}
The function Display_Menu() is called as a statement because we do not expect any
return value from this function. Void functions may do an action like displaying
something, changing the values of variables, and etc. In the other side, the values returned
from the remaining two functions (add and subtract) are used in the cout statements. But
this doesn’t mean that the following call is invalid:
add(n1,n2);
This is a valid call causing the numbers to be added and their sum is returned. But the
value returned is simply lost and logically this is not all right.
Note: If a return value is not specified for a function, then it will be assumed that the
function has an int return type and eventually a return statement is expected.
Generally, we may define the following types of functions (commonly) when practicing
functions in writing programs:

a. Void functions: These functions will be included whenever some action has to be done.
The names of these functions are usually verb phrases like Display_Menu();

b. Input/Output functions: Functions used for reading input and producing output will
also be included most of the time.

c. Boolean functions: These kinds of functions are used to determine whether something
is true or false. They return a Boolean value (true(1) OR false(0)). Boolean
functions are usually used with selection statements like ‘if’. It is also a common
practice to give a name which starts with “Is...” for such types of functions. (like
IsMax(int m)…this can be a good name to refer to a function which
determines whether the argument is the largest number from a certain reference
list).

Last Update: Jun. 23, 12 76


Ethiopian Civil Service College Computer Programming & Numerical Methods

6.4. Function Prototypes


Previously, all the functions which are called from the main function are placed before
the main function. It seems that it is a must to place all the functions which are to be
called before they are called. However, there is a way to make functions global so that
they can be called anywhere regardless of their position. This is by defining the prototype
of the function at the beginning of the program.

The prototype of a function is simply a declaration of the function which consists of the
return type, the name of the function and the parameters (if there are any).

Look at the Example 8 which is given below. Lines 4-7 indicate how function prototypes
are specified. If all the functions are declared like this, the compiler will not bother about
the order of the functions. In other words, the functions’ declarations make them global.

Sometimes, function declarations (prototypes) could also be written by specifying the


return type, the name of the functions and only the data type of parameters (without
having any identifiers). Based on this, the following declaration is valid for a function
which returns int, whose name is ‘add’ and which has got two parameters both int type.
int add(int,int);
Note also that function prototypes are followed by a semicolon as shown here.
Example 8:
#include <iostream.h>
#include <conio.h>
//prototypes of the functions
int num1,num2;
int Display_Menu();
int add(int n1,int n2);
int subtract(int n1,int n2);
void Read_Numbers();
//----main-----------------------------------
void main()
{
int choice=Display_Menu();
Read_Numbers();
if (choice==1)
cout<<add(num1,num2);
else
cout<<subtract(num1,num2);
}
//---Display_Menu----------------------------
int Display_Menu()
{
int ch;
cout<<”*******MENU*******”);
cout<<”\n\t1. Add Numbers”;
cout<<”\n\t1. Subtract Numbers”;
cout<<”********************”);
cout<<”\nEnter your choice:”;
cin>>ch;

Last Update: Jun. 23, 12 77


Ethiopian Civil Service College Computer Programming & Numerical Methods

return ch;
}
//--------add----------------------------------
int add (int num1, int num2)
{
return num1+num2;
}
//--------subtract------------------------------
int subtract(int num1, int num2);
{
return num1-num2;
}
//-------Read_Numbers---------------------------
void Read_Numbers()
{
cout<<”Enter two numbers:”;
cin>>num1>>num2;
}

6.5. Writing Test Drivers


Whenever preparing your own functions, it is a good idea to test the functions separately
with a simple program. To test the functions, there should be a main function calling the
required function. Generally speaking, large modularized programs are built step by step
by working on the individual functions separately. For this purpose, follow the following
steps:
 Pick a function from the set of functions which are to be developed.
 Write the function.
 Write a test driver which is a main function calling the function you have written.
 Run the program and try to test the function in different ways by providing various
input values. Look at Example 9.

Example 9:
Write a program which will find the value of y for a given value of x using the following
mathematical function:
x3 5 x 5
y *
2 x 3
Solution:
Observing the given function, we can easily come up with a set of functions needed to
solve this problem. Study the mathematical function. The operations involved are:
Square root
Exponentiation Subtraction

x3 5 x 5
y *
2 x 3
Multiplication
Division Addition
Therefore we need to use five functions. We are going to write a function for each
operation and test it using a test driver. Let’s proceed:

Last Update: Jun. 23, 12 78


Ethiopian Civil Service College Computer Programming & Numerical Methods

a. Exponentiation:
The following is a function which returns xy by taking x and y as parameters:
int expo(int x, int y)
{
int result=1;
for (int i=1;i<=y;i++)
result=result * x;
return result;
}
The next step is to test the function by writing a test driver. But before that let’s talk
about one more point. In the mathematical function given in the problem, we need to
calculate only x3. But here we developed a generic function which could be used to
calculate xy for any given positive integers x and y. This is a good idea because we may
need a similar function in another time and can simply reuse this one if we make it to
work for general case. Therefore, it is always better to write generic functions except in
cases where you might face more problems when trying to do it. Anyways, it is still your
decision.

Let’s test the function:


#include <iostream.h>
#include <conio.h>
int expo(int x, int y);
void main()
{
cout<<expo(2,3);
getch();
return 0;
}
//--expo-----------------------------
int expo(int x, int y)
{
int result=1;
for (int i=1;i<=y;i++)
result=result * x;
return result;
}
Here we are ready to test the function because we have already included the main
function which calls our function expo. The sample test values taken for this particular
test are 2 and 3. We know what to expect. We must see 8 printed on the screen when this
program is run. If you see a different number than you expect, then you should be back to
your function code and try to find the problem. But fortunately, it works! (I have tried it).

But, is this the end of the test? The answer is no since we need to try more numbers like 4
and 5 (by expecting 1024) or 1 and 3 (by expecting 1). You will do this by inserting the
numbers in the function call found in the main function. Still, the function is working
well.

Last Update: Jun. 23, 12 79


Ethiopian Civil Service College Computer Programming & Numerical Methods

Are we satisfied? If you say yes, you are really wrong. Previously, we tried only positive
numbers. What about other cases where negative numbers involve? What about x 0? You
must also see these cases since we are making the function generic and we may reuse it in
the future without any hesitation.

Try 2 and 0. The expected value is 1. Luckily, this test is also successful. As we have
already discovered, we are left with one case which is when one of the parameters are
negative.

Try -2 and 3. The expected value is -8. It again works. The other case is when the
exponent is negative like 2 and –3. We expect to see 0.125 on the screen. Unfortunately
or fortunately, we will see 1 which is not the expected answer. This implies the function
has a defect. What is the cause of this defect?

You will automatically find out that the function is unable to handle negative exponents
since the for loop doesn’t have any chance of execution when the exponent (which is the
boundary value for the for loop) is negative. Even if you find a way to execute the for
loop, it is still needed to return the reciprocal of the number. The function can be
corrected as follows:
#include <iostream.h>
#include <conio.h>
//prototype………………………………
float expo(int x, int y);

//--main----------------------------------------------
void main()
{
cout<<expo(2,3);
getch();
return 0;
}

//--expo----------------------------------------------
float expo(int x, int y)
{
float result=1.0;
if (y>0) // if y is positive
for (int i=1;i<=y;i++)
result=result * x;
else //if y is negative
{
for (int i=1;i<=-y;i++)
result=result * x;
result = 1/result; //take the reciprocal
}
return result;
}
After modification, the function is tested and it returns 0.125.

Last Update: Jun. 23, 12 80


Ethiopian Civil Service College Computer Programming & Numerical Methods

In a similar way, develop the other functions one by one and build the program.
Therefore, the following could be the complete program:
#include <iostream.h>
#include <conio.h>
#include <math.h>
//prototypes………………………………
float expo(int x, int y);
float difference(float x, float y);
float sum(float x, float y);
float quotient(float x, float y);
float product(float x, float y);
float computeY (float x);

//------main------------------------------------
void main()
{
float x, y;
cout<<”\n\n\t This program will help you to evaluate the
function”;
cout<<”\n\ty =[(x3+5)/2]*[(sqrt(x)-5)/(x+3)] for a given
value of x.”;
cout<<”\n\n\t\t Please provide the value of x:”;
cin>>x;
y = computeY(x);
cout<<”For x=”<<x<<” y=”<<y;
getch();
return 0;
}
//------expo-----------------------------------
float expo(int x, int y)
{
float result=1.0;
if (y>0) // if y is positive
for (int i=1;i<=y;i++)
result=result * x;
else //if y is negative
{
for (int i = 1;I <= -y; i++)
result=result * x;
result = 1/result; //take the reciprocal
}
return result;
}
//--sum---------------------------------------
float sum(float x, float y)
{
return (x+y);
}
//--difference-------------------------------
float difference(float x, float y)
{
return (x-y);
}
//--quotient----------------------------------

Last Update: Jun. 23, 12 81


Ethiopian Civil Service College Computer Programming & Numerical Methods

float quotient(float x, float y)


{
return (x/y);
}
//------product------------------------------------
float product(float x, float y)
{
return (x*y);
}
//------computeY----------------------------------
float computeY(float x)
{
float value1, value2, finalvalue;
value1=quotient(sum(expo(x,3),5),2);
value2=quotient(difference(sqrt(x),5),sum(x,3));
finalvalue = product(value1,value2);
return finalvalue;
}

6.6. Visibility and Lifetime of Variables


When writing C++ programs, where can you possible declare variables? The complete
answer is variables can be declared within the block of any of the functions in the
program or outside the blocks of any of the functions (it seems strange!). The next point
that should be taken into consideration is that how the placement of the declaration of
variables affect the variables or the entire program. The placement of the declaration of
variables will determine the scope of the variables.

The scope of variables determines how long it is available to the program and where it
can be accessed. Generally to understand this in more detail, pay attention to the
following simple example:
Example 10:
#include <iostream.h>
#include <conio.h>
int DoubleValue(int a);
void main()
{
int x=5;
int y=DoubleValue(x);
cout<<”When the number “<<x<<” is doubled, “<<”it
will be “<<y;
getch();
return 0;
}
//-- DoubleValue -----------------------------
int DoubleValue (int a)
{
int s=2*a;
return s;
}

Last Update: Jun. 23, 12 82


Ethiopian Civil Service College Computer Programming & Numerical Methods

In the above program, different variables are declared and used in different places. The
variables x and y are declared in the main function. Since x is declared in the first line
within the block of the main function, it is visible and usable (accessible) through out the
main function (note that it is not stated as ‘through out the program’). Concerning the
second variable y, it is declared in the second line and accessible after that point. There is
no way to legally use the variable y before its declaration even in the main function. It is
quite obvious that these variables are not known in the function DoubleValue. Similarly,
variables like a and s are declared and used in the function DoubleValue which implies
they are local to this function.

Discussing about the scope of variables is very important here as we do have more than
one function in a program. The existence of several functions in a program makes the
scope of the variables different as to where they are declared.

Based on the scope of variables, it is likely to divide the type of variables into two
namely Local Variables and Global Variables. We will discuss about these two types of
variables in the next few topics:

6.6.1. Local Variables:


Local variables are variables that are declared with in the block of one of the functions in
the program and they exist only in the particular function at which it is declared. More
specifically, local variables will be accessible starting from the point of declaration until
the end of the block.

Parameters of functions are also considered as local variables which directly implies that
parameters can be used as any local variables as if they had been declared in the body of
the function.

6.6.2. Global Variables:


Global variables are variables which are declared outside the blocks of any of the
functions. These variables are visible from their point of declaration until the end of the
program. Look at the following examples on the next page.
Example 11:
Let’s consider once again the program which is given in Example 8. The variables which
are declared in line 4 of this program are global variables which are accessible by all the
functions (note the position). These two variables are used in every function wherever
they are needed. In the other side, there are also several local variables in some of the
functions in the program. To name the local variables, choice (local to main), ch (local to
Display_Menu), n1 & n2(local to add, subtract).
#include <iostream.h>
#include <conio.h>
//prototypes of the functions
int num1,num2;
int Display_Menu();
int add(int n1,int n2);

Last Update: Jun. 23, 12 83


Ethiopian Civil Service College Computer Programming & Numerical Methods

int subtract(int n1,int n2);


void Read_Numbers();

//----main----------------------------------------
void main()
{
int choice=Display_Menu();
Read_Numbers();
if (choice==1)
cout<<add(num1,num2);
else
cout<<subtract(num1,num2);
}

//---Display_Menu----------------------------------
int Display_Menu()
{
int ch;
cout<<”*******MENU*******”);
cout<<”\n\t1. Add Numbers”;
cout<<”\n\t1. Subtract Numbers”;
cout<<”********************”);
cout<<”\nEnter your choice:”;
cin>>ch;
return ch;
}

//--------add--------------------------------------
int add (int n1, int n)
{
return (n1+n2);
}

//--------subtract-----------------------------------
int subtract(int n1, int n2);
{
return (n1-n2);
}
//-------Read_Numbers--------------------------------
void Read_Numbers()
{
cout<<”Enter two numbers:”;
cin>>num1>>num2;
}

Last Update: Jun. 23, 12 84


Ethiopian Civil Service College Computer Programming & Numerical Methods

Example 12:
Consider the following program: a is a global variable
which is visible to all the
#include <iostream.h> functions in the program,
#include <conio.h>
int a=6; sum is a local
int add(int x, int y); variable to the
void main() main function.
{ ch is a global variable
int sum = add(a,a);
which is global to all
cout<<”Sum=”<<<<sum;
} the functions after its
char ch=’A’; declaration (add).
int add(int x, int y) x and y are local
{ variables to the
cout<<”ch=”<<ch; function add.
int s=x+y; s is a local
return s; variable to the
}
function add.
Example 13:
Predict the output of the following program:
1 #include <iostream.h> Observe that there exist three local variables.
2 #include <conio.h> All these variables are the same by name (num),
3 void fun1(int num); but they are completely separated and different
4 int func2(int num); variables. In other words, there is num of the
5 void main() main function, num of func1, and num of func2.
6 { Therefore, the four lines will be displayed.
7 int num=6;
8 cout<<”num=”<<num<<endl; In line 8, num=6 will be displayed. In line 9,
9 func1(num); func1 is called causing the body of the function
10 cout<<”num=”<<num<<endl; to be executed. The variable num is specified as
11 num = func2(num); an argument in this call. Recall that the value of
12 cout<<”num=”<<num<<endl; the variable (not the variable itself) is sent to the
13 } function. This value which is brought to the
14 void func1(int num) function will be copied to the parameter (local
15 { variable) num of func1. func1 increments the
16 num++; local parameter num and display it in line 16
17 cout<<”num=”<<num; and 17 respectively. The end of the function is
18 } marked by the closing brace in line 18 and
19 int func2(int num) execution will be continued back in the main
20 { function from line 10 printing line 3 of the
21 num=num+2; output. Here the same local num to the main
22 return num; function is referred (Hence num=6). Line 11
23 } consists of another function call. func2 is called
by specifying the value of num as an argument.
The output of the program shown will be func2 copies the argument to its local num. In
as indicated below: line 21, the local num is updated to 8 and this
num = 6 (in the main function) same value is returned. The returned value is
num = 7 (in the function func1) assigned to the local num of main because of the
num = 6 (in the main function) assignment operator in line 11. and finally,
num = 8 (in the main function) num=8 will be printed since the local num of
main is already modified in line 11.

Last Update: Jun. 23, 12 85


Ethiopian Civil Service College Computer Programming & Numerical Methods

6.6.3. Static Variables


Static variables are local variables that are made to preserve the last value of the variable
between successive calls of the function. From the previous discussion about local
variables, it is known that local variables vanish when their scope ends. But static
variables will save the previous value from the previous call. Static variables are declared
using the keyword static before the data type.
Example 14:
Predict the output of the following programs which are marked as 1 and 2:
#include <iostream.h> #include <iostream.h> Answer:
#include <conio.h> #include <conio.h> The output of program 1
void MyFunc(); void MyFunc(); will be:
void main() void main() n=3
{ {
for(int for(int
n=3
i=1;i<=3;i++) i=1;i<=3;i++) n=3
MyFunc(); MyFunc();
getch(); getch(); Program 2 will produce
} } the following:
void MyFunc() void MyFunc()
1 2
{ { n=3
int n=2; static int n=2; n=4
n++; n++; n=5
cout<<”n=”<<n<<endl; cout<<”n=”<<n<<endl;
} }
In both programs, MyFunc is called three times (the for loop will iterate three times). A
call to MyFunc will cause the variable n to be declared and assigned to 2. n is then
incremented and its value is printed on the screen. When the function is closed, the
variable n will vanish as it is a local variable to MyFunc.

In the second program, the variable n is declared as static. Because of this reason, the
value of n in the previous call is preserved. Therefore, after the call of MyFunc for the
first time, the value of n will be 3. This value will be saved. In the next call, the value of
n will again be incremented. This time the value of n becomes 4. When the third call is
executed, the value of n will be 5.

Summary on local, global and static variables


Global variables are very dangerous as they might be shared by more than one function
and may be modified anytime or anywhere. You should choose to make your variables
local to avoid such problems. As there is a means to pass any value to a function from the
calling function’s side, it is possible to make functions share values using local variables.

Of course by using global variables you will not worry about the visibility of the variable
to any given function. It might seem this way simplifies things. However, the truth is it
may create a very complex problem because one function may unconsciously modify the
variable and hence logical error. But if you have a good reason to make variables global
(if several functions share a variable), it might be acceptable. Still, the surprising thing is

Last Update: Jun. 23, 12 86


Ethiopian Civil Service College Computer Programming & Numerical Methods

that anything that can be done using global variables can also be performed using local
variables and that is why global variables are not encouraged.

Static variables are used when you want to preserve intermediate values. The following
might be a good example:

Example 15:
The following program will compute the value of y for a given value of x using the
following mathematical equation:
y = x3+x2+x+5

Solution:
#include <iostream.h>
#include <conio.h>
#include <math.h>
int sum(int currval);

void main()
{
int x,y;
cout<<”\nEnter the value of x”;
cin>>x;
y=sum(pow(x,3));//to add x3 to the initial value of static
variable s which is 0.
y=sum(pow(x,2));//to add x2 to x3
y=sum(x);//to add x to the sum of x3 and x2
y=sum(5);//to add 5 to the total sum of x3,x2 and x
cout<<”\nThe value of y is “<<y;
}
int sum(int currval)
{ Enter the value of x:
static int s=0; 2
s=s+currval;
The value of y is 15
return s;
}

6.7. Function Parameters


Now, let’s revisit parameters of functions and talk about them a little bit in detail. There
will be two points about which we should discuss.

6.7.1. Default Parameters


According to the explanation given before, the number of function parameters and
arguments should be equal when a function is called. This is a concrete rule that should
be respected. But there is one opening to violate this rule i.e. by using default parameters.

The possibility that we have is to assign a default value for parameters and it is allowed
to omit the parameters which have a default value when the function is called. The logic
is simple: if you don’t provide a value for the default parameter, then the default value
will be used within the function (if needed).

Last Update: Jun. 23, 12 87


Ethiopian Civil Service College Computer Programming & Numerical Methods

Default parameters are given default values when the function is declared (in the
prototype). In addition to this, default parameters must be placed to the right of the other
parameters. This implies it is not possible to mix up default parameters with the others.

Example 16:
#include <iostream.h>
#include <conio.h>
void f1(int x, int y=0);
void main()
{
x=2
f1(2,3);
y=3
f1(9);
getch(); x=9
} y=0
void f1(int x)
{
cout<<”x=”<<x<<endl;
cout<<”y=”<<y<<endl;
}
The function f1 is called two times. In the first call, two arguments are sent to the
function and x=2 and y=3 are printed. The second call to the same function has got only
one argument and it is valid as the second parameter is a default parameter. As a result of
this call, x=9 and y=0 (which is the default value) will be displayed.

Example 17:
The prototype int f1(int a, int b, int c=3, int d=2) is valid because all the default
parameters are accumulated to the right side. But the prototype void f2(int a, int b=4, int
c, int d=45); is invalid as the default parameters are mixed up with the others.

Example 18:
Write a C++ program which will be used to evaluate polynomial functions with various
degrees. Suppose it is required that the program should be able to work for up to degree 4
polynomials. The user will be prompted to enter the degree of the polynomial, the
polynomial itself and the value of x. In the end, the program is expected to display the
value of y.
Solution:
There could be two possibilities to write a program for the given problem.
Possibility 1:
Define separate function to evaluate polynomials of degree 0,1,2,3, and 4 and call them
based on the given degree (obtained from the user)
Possibility 2:
Define only a single function that could be called for all the stated degrees flexibly. The
function would have arguments for the coefficients of the polynomial and the value of x.
Since the value of the degree of the polynomial depends on the user’s input, there is no
way to know it in advance. Therefore, we could make the parameters for the coefficients
default parameters by assigning them a default value 0 like the following:
int computeY (int valueofx, int c0,int c1=0,int c2=0,int c3=0,int c4=0);
//c0 refers to the constant value, c1 refers to the coefficient of x1, c2 refers
//to the coefficient of x2, c3 refers to the coefficient of x3,c4 refers to the coefficient
of x4

Last Update: Jun. 23, 12 88


Ethiopian Civil Service College Computer Programming & Numerical Methods

For instance if the user wants the program to evaluate a polynomial of degree 2, then it is
expected that coefficients exist for x2, and probably for x1. In this particular case, the
function will be called by providing valueofx,c0,c1 and c2. The last two parameters will
be omitted and their default value (which is 0) will be taken in the calculation within the
body of the function. The following could be a call to this function to evaluate the
function y=2x2+3x+5 when the value of x is 3.
int y=computeY(3,5,3,2);

The following program is provided as to make you see how this feature is useful. The
program uses graphics mode and has a very good interface. I believe it will be a good
opportunity for you to explore these built in graphics functions as well. Try to understand
it by reading about each of the functions and learn more (optional).

#include <iostream.h>
#include <conio.h>
#include<math.h>
#include <graphics.h>
#include <stdlib.h>
#include <dos.h> //global variables
int c[5];
int ValueOfX,deg; //function prototypes
void DisplayHomePage(int color);
void CollectInformation(int color);
void AcceptEquation(int deg);
int computeY(int ValueOfX,int c0,int c1=0,int c2=0,int c3=0,int
c4=0);
//--------main function------------------------------------------
void main()
{
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;
/*initialize graphics and local variables */
/*C:\\tc\\bgi is the path of the graphics driver and
it is to be changed if a differnt driver path exists
in the pc that you are working*/
initgraph(&gdriver, &gmode, "C:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
cout<<"Graphics error: %s\n"<<grapherrormsg(errorcode);
cout<<"Press any key to halt:";
getch();
exit(1); /* terminate with an error code */
}
int ValueOfY;
DisplayHomePage(4);
sleep(1);
DisplayHomePage (0);
CollectInformation(5);
if (deg==0)
ValueOfY=computeY(ValueOfX,c[0]);
else if (deg==1)
ValueOfY=computeY(ValueOfX,c[0],c[1]);

Last Update: Jun. 23, 12 89


Ethiopian Civil Service College Computer Programming & Numerical Methods

else if (deg==2)
ValueOfY=computeY(ValueOfX,c[0],c[1],c[2]);
else if (deg==3)
ValueOfY=computeY(ValueOfX,c[0],c[1],c[2],c[3]);
else if (deg==4)
ValueOfY=computeY(ValueOfX,c[0],c[1],c[2],c[3],c[4]);
setcolor(4);
outtextxy(100,150+15*(deg+9),"The value of y is ");
gotoxy(28,11+(deg+9));
cout<<"y = "<<ValueOfY;
getch();
}
//---------DisplayHomePage -------------------------------------
void DisplayHomePage (int color)
{
setcolor(color);
setlinestyle(2,3,3);
/*draws an ellipse of center(290,220)
and horizontal radius of 220 and vertical
radius of 120*/
ellipse(290,220,0,360,220,120);
if (color==0)
setcolor(color);
else
setcolor(2);
settextstyle(5,0,3);
delay(500);
outtextxy(130,130,"Dear User,");
delay(500);
outtextxy(150,150,"It is a pleasure to help you!");
delay(500);
settextstyle(4,0,3);
if (color==0)
setcolor(color);
else
setcolor(15);
outtextxy(150,190," WELCOME");
settextstyle(3,0,2);
if (color==0)
setcolor(color);
else
setcolor(9);
delay(500);
outtextxy(150,220,"This program will help you to");
delay(500);
outtextxy(150,250,"evaluate a polynomial equation");
delay(500);
outtextxy(150,280," of degree 0,1,2,3,4");
}
//---------CollectInformation------------------------------------
void CollectInformation(int color)
{
setcolor(color);
rectangle(80,80,500,400);
setcolor(15);

Last Update: Jun. 23, 12 90


Ethiopian Civil Service College Computer Programming & Numerical Methods

settextstyle(11,0,3);
outtextxy(100,100,"Enter the degree of the polynomial:");
gotoxy(49,7);
cout<<"_\b";
cin>>deg;
outtextxy(100,130,"Now, enter the equation:");
AcceptEquation(deg);
}
//---------AcceptEquation--------------------------------
void AcceptEquation(int deg)
{
for(int i=deg;i>0;i--)
{
int j=deg-i;
outtextxy(120,150+15*j,"Coefficient of ");
gotoxy(31,10+j);
cout<<"x^"<<i<<": _\b";
cin>>c[i];
}
outtextxy(120,150+15*deg,"Constant value:");
gotoxy(31,10+deg);
cout<<"_\b";
cin>>c[0];
setcolor(4);
int r=deg+2;
outtextxy(100,150+(15*r),"Your equation is");
gotoxy(30,11+r);
cout<<"y = ";
for (i=deg;i>0;i--)
{
if (c[i]!=0)
if(i==1)
cout<<c[i]<<"x+";
else
cout<<c[i]<<"x^"<<i<<"+";
}
cout<<c[0];
r=r+3;
setcolor(15);
outtextxy(100,155+(15*r),"Enter the value of x:");
gotoxy(35,11+(r-1));
cout<<"_\b";
cin>>ValueOfX;
}
//---------computeY----------------------------------
int computeY(int ValueOfX,int c0,int c1,int c2,int c3,int c4)
{
int
y=c4*(pow(ValueOfX,4))+c3*(pow(ValueOfX,3))+c2*(pow(ValueOf
X,2))+c1*(pow(ValueOfX,1))+c0;
return y;
}

Last Update: Jun. 23, 12 91


Ethiopian Civil Service College Computer Programming & Numerical Methods

6.7.2. Passing Parameters


If we are developing programs having a number of independent functions, then there
should be a way to pass information between them as they exist in a single program. You
observed that the main method that is used to pass information is by passing parameters
(arguments). Arguments which are supposed to be sent to function (during calling them)
can be represented using variables as can be seen from the previous programs.

A function having the prototype:


void myfunction(int x);
could be called as:
myfunction(2); OR int a=3; OR int a;
myfunction(a); cin>>a;
myfunction(a);

The variables that are listed in the function’s parameter list are known as formal
parameters (arguments). Formal arguments are local to a function. They are also
called dummy arguments.
The expressions that are listed as parameter in the function call are called actual
parameters (arguments). During function call each of these expressions should be
evaluated to give a result which is compatible to the corresponding formal parameter.

When passing information through parameters, there are two different ways to do it:
i) Pass by value (by copy)
If parameters are said to be passed by value, then only their values (not themselves)
are passed to the function. As a result, any change which is done within the function
body on the formal parameters will not affect the actual parameters sent. To
understand this idea well, look at the examples given below:

Example 19:
Predict the output of the following program: Enter the length of the rectangle: 11.5
#include <iostream.h> Enter the width of the rectangle 12
#include <conio.h> The perimeter of the rectangle is 23.5
float perimeter(float length, float width);
void main()
{
float l,w;
cout<<”Enter the length of the rectangle:”;
cin>>l;
cout<<”\nEnter the width of the rectangle:”;
cin>>w;
cout<<”\nThe perimeter of the rectangle is
”<<perimeter(l,w);
getch();
}
float perimeter(float length, float width)
{
return (2*length+2*width);
}

Four local variables are defined in the above program namely l,w (actual arguments
and local to main) and length, width(dummy parameters and local to perimeter).
During the function call (line 6 of the main function), the variables l and w are

Last Update: Jun. 23, 12 92


Ethiopian Civil Service College Computer Programming & Numerical Methods

specified as arguments. In this call, the arguments are passed by value, which is
exactly to mean the values of l and w are brought to the function and copied to the
parameters length and width respectively.
Parameters passed by value are referred as read-only.

Example 20:
Consider the following program and predict the output:
#include <iostream.h>
#include <conio.h>
void fun1(int i, int j); a=2
void main() b=3
{ int a=2,b=3; a=2
cout<<”\na =”<<a; b=3
cout<<”\nb =”<<b;
fun1(a , b);
cout<<”\na =”<<a;
cout<<”\nb =”<<b;
getch();
}
void fun1(int i, int j)
{
i++;
j=j+2;
}

The output will be as shown in the box. When the function is called (in line 4 of the
main function), the arguments are passed by value. The values of a and b are sent and
copied to i and j. The two statements with in the body of the function will change the
value of i and j but this will not be propagated to the variables a and b because only
their values are brought there.

Example 21:
The program given in the previous example is a little bit changed at some points.
Changes are shown in bold font. Determine whether the same output will be
generated than the previous one.
#include <iostream.h>
#include <conio.h>
void fun1(int i, int j);
void main()
{
int i=2,j=3;
cout<<”\ni =”<<i;
cout<<”\nj =”<<j;
fun1(i , j);
cout<<”\ni =”<<i;
cout<<”\nj =”<<j;
getch();
}
void fun1(int i, int j)
{
i++;
j=j+2;
}

Last Update: Jun. 23, 12 93


Ethiopian Civil Service College Computer Programming & Numerical Methods

We are ready to bet that exactly the same output will be generated. We have still four
distinct local variables namely i, j (local to main) and other i, j (local to fun1).
Therefore, no change will be exhibited even if we replaced the variables a and b by i
and j in the main function. Still the variables are passed by value.

ii) Pass by reference (by address)


This way is exactly opposite to the passing parameters by value method. An actual
argument passed by reference will be affected by any change made to the dummy
(formal) parameters within the function block.

To make a parameter passed by reference, put an ampersand (&) symbol before the
identifier of the parameter or after the data type of the parameter. All the following
prototypes are valid considering compilation as well as execution:
void f1(int &a);
void f1(int& a);
void f1(int&a);

Example 22:
Trace the following simple program and predict the output:
#include <iostream.h>
#include <conio.h>
void fun2(int &x);
void main()
{
int myvar=3;
cout<<”\nBefore the call…….\n”;
cout<<”myvar = “<<myvar;
func2(myvar); Before the call……….
cout<<”\\nAfter the call…….\n”; myvar = 3;
cout<<”myvar = “<<myvar; After the call……….
getch(); myvar = 5;
}
void func2(int &x)
{
x = x+2;
}
The modification being made to x is propagated to myvar because the argument is now
passed by reference.

Parameters are passed by reference because of different reasons. Some of the frequently
happened cases which make the program to use pass by reference could be:

a. If you want more than one values to be returned from a function:


Multiple return statements, which are listed after one another are meaningless since the
execution of the function will be stopped as the first return statement, is encountered.
Then how could we return multiple values from functions?

Example 23:
Suppose we want to develop a function which accepts the value of the radius of a circle
and calculates both the area and circumference of the circle. The following
implementation is not the right solution:

Last Update: Jun. 23, 12 94


Ethiopian Civil Service College Computer Programming & Numerical Methods

float compute(float radius)


{
float a = 3.14*radius*radius;
float c=2*3.14*radius;
return a;
return c;……………………………unreachable code
}
Therefore, we need to look for another solution. The solution is to have two additional
parameters in the function which represent the area and circumference of the circle and
which are passed by reference (address). The complete solution is provided on the next
page and the description the program is given below:

The function call passes the variables area and circum to the function compute by
reference. In the parameter listing of the function compute, the variables A and C will
replace them. In other words, A and C are local names to represent Area and circum with
in the body of the compute function. The two assignment statements in the function
compute will assign the computed value to A and C and eventually to Area and circum in
the main function.

#include <iostream.h>
#include <conio.h>
void compute(float radius, float &A, float &C);
void main()
{
float area, circum,radius;
cout<<”\nEnter the radius of the circle”);
cin>>radius;
compute(radius, area, circum); Enter the radius of the circle: 3
cout<<”\n\tArea = “<<area; Area = 28.26
cout<<”\n\tcircumference = “<<circum; Circumference =
getch(); 18.84
}
void compute(float radius, float &A, float &C)
{
A=3.14*radius*radius;
C=2*3.14*radius;
}
b. When writing Input functions reading multiple values:
A function that is used to prompt the user for input data (multiple values) could
possibly have parameters passed by reference. These parameters will be used to pass
the values of the input parameters.
Example 24:
In the following program, three functions exist: Get_Values(), Calc_Avg() and
main(). Understand the program and see how parameters passed by reference are
useful in writing functions like Get_Values().
#include <iostream.h>
#include <conio.h>
void Get_Values(int &a, int &b, int &c);
float Calc_Avg(float a, float b, float c);
void main()
{
int a,b,c; Enter the values of the
three numbers:
Get_Values(a,b,c);
3, 4, 6
The average of
Last Update: Jun. 23, 12
the numbers is 4.33
95
Circumference = 18.84
Ethiopian Civil Service College Computer Programming & Numerical Methods

Float average = Calc_Avg(a,b,c);


cout<<”The average of the numbers
is “<<average;
getch();
}
void Get_Values(int &a, int &b, int &c)
{
cout<<”\nEnter the values of the three numbers:”;
cin>>a>>b>>c;
}
float Calc_Avg(float a, float b, float c)
{
return ((a+b+c)/3);
}
c. When passing large amount of data:
When parameters are passed by value, the copy of the argument will be used within
the function. If large amount of data should be passed between functions using
parameters, it is better to pass the parameters by reference, as this option will be very
efficient in terms of memory usage. When parameters are passed by reference, no
another copy will be stored in memory.

Note: It is possible to add the keyword const before parameters passed by reference to
make sure that no change will affect the actual argument.
There is one exceptional case:
When arrays are passed as arguments, they are passed by reference by default. The reason
is that the pointer to the initial element of the array is passed when arrays are passed as
arguments. Therefore, any modification is directly applied to the memory space reserved
for the array as the function statements are executed.

Example 25:
#include <iostream.h>
#include <conio.h>
void Change(int a[3]);
void main()
{
int b[3] = {2,3,5};
cout<<”Before the call…….”;
for (int i=0;i<3;i++)
cout<<”b[“<<i<<”]=”<<b[i]<<endl;
Change(b);
cout<<”After the call…….”;
for (int i=0;i<3;i++) Before the call…………..
b[0]=2
cout<<”b[“<<i<<”]=”<<b[i]<<endl;
b[1]=3
getch(); b[2]=5
} After the call…………..
void Change(int a[3]) b[0]=3
{ b[1]=4
for(int i=0;i<3;i++) b[2]=6
a[i]++;
}

Last Update: Jun. 23, 12 96


Ethiopian Civil Service College Computer Programming & Numerical Methods

6.8. Overloading Functions


Before describing what overloading is and what its advantages are, let’s see the following
example:

Example 26:
Write a program which will double a given number. If the user enters a character (instead
of a number), the program should display the character two times (funny doubling).

Solution:
‘Number’ may refer to integers or float type numbers. Considering both cases is essential.
Since we will give a chance for the user to enter integers, floats and characters, we need
three different functions to double them. Based on this conclusion, the program given on
the next page (page 29) could be a good candidate to achieve the goal stated in the
problem.

The three functions that are used to double the items are DoubleInt, DoubleFloat and
DoubleChar. Observing these functions lead us to the conclusion that they are used to
perform the same thing but on different types of data. For such kinds of situations,
overloading functions is very helpful and comfortable.

In C++, overloading functions is possible which means more than one functions can be
defined with the same name but with different parameters. Parameters may differ in their
number or their data type. It is possible to define the following functions in a single
program:

int func1(int a);


int func1(int a, int b);
int func1(int a, int b, int c);

#include <iostream.h> This program will double everything!


What kind of item do you have? Choose one:
#include <conio.h>
1. Integers
int DoubleInt(int a); 2. Real Numbers
float DoubleFloat( float a); 3. Characters
void DoubleChar(char ch); 2
void main() Now enter your item:
{ 3.5
clrscr(); When the value 3.5 is doubled, it will be 7
int choice, IntVal;
float FloatVal;
char CharVal;
cout<<"\nThis program will double everything!";
cout<<"\n\tWhat kind of item do you have? Choose
one:\n";
cout<<"\t\t1. Integer\n\t\t2. Real numbers\n\t\t3.
Character\n";
cin>>choice;
cout<<"Now enter your item\n";
if (choice==1)
{
cin>>IntVal;
cout<<"When the integer value "<<IntVal<<" is
doubled, it will be "
<< DoubleInt (IntVal);

Last Update: Jun. 23, 12 97


Ethiopian Civil Service College Computer Programming & Numerical Methods

}
else if (choice==2)
{
cin>>FloatVal;
cout<<"When the value "<< FloatVal <<" is doubled,
it will be "
<< DoubleFloat (FloatVal);
}
else
{
cin>>CharVal;
cout<<"When the character "<<CharVal<<" is double,
it will be ";
DoubleChar (CharVal);
cout<<". Funny! Isn't it?";
}
getch();
}

int DoubleInt (int a)


{
return (2*a);
}

float DoubleFloat (float a)


{
return (2*a);
}

void DoubleChar (char ch)


{
cout<<ch<<ch;
}

The three functions that are developed for similar actions to be applied on different items
(in the above program) can be declared and defined using the same name. Overloading
these functions is useful because you will not bother to create and use different functions
for each type of data. Therefore, these functions could be declared and defined using the
same name but with different parameters and return type. The programmer is not to
worry about which one to call. When passing arguments, the right function will be called
automatically. The above program could be rewritten like the following applying
overloading:

#include <iostream.h> This program will double everything!


#include <conio.h> What kind of item do you have? Choose one:
int DoubleItem(int a); 1. Integers
float DoubleItem( float a); 2. Real Numbers
void DoubleItem(char ch); 3. characters
2
void main()
Now enter your item:
{ 3.5
clrscr(); When the value 3.5 is doubled, it will be 7
int choice, IntVal;
float FloatVal;
char CharVal;

Last Update: Jun. 23, 12 98


Ethiopian Civil Service College Computer Programming & Numerical Methods

cout<<"\nThis program will double everything!";


cout<<"\n\tWhat kind of item do you have? Choose
one:\n";
cout<<"\t\t1. Integer\n\t\t2. Real numbers\n\t\t3.
Character\n";
cin>>choice;
cout<<"Now enter your item\n";
if (choice==1)
{
cin>>IntVal;
cout<<"When the integer value "<<IntVal<<" is
doubled, it will be "
<< DoubleItem (IntVal);
}
else if (choice==2)
{
cin>>FloatVal;
cout<<"When the value "<< FloatVal <<" is doubled,
it will be "
<< DoubleItem(FloatVal);
}
else
{
cin>>CharVal;
cout<<"When the character "<<CharVal<<" is double,
it will be ";
<<DoubleItem (CharVal);
cout<<". Funny! Isn't it?";
}
getch();
}

int DoubleItem (int a)


{
return (2*a);
}

float DoubleItem (float a)


{
return (2*a);
}

void DoubleItem (char ch)


{
cout<<ch<<ch;
}
As a result of function overloading (sometimes called function polymorphism), flexibility
increases. However, you need to consider the following points when overloading
functions in C++:
 The same name and different type of parameters (in number or type) could be given
to functions.
 The return type of overloaded functions could be identical or not.
 However functions with the same name and parameters but with different return type
are NOT allowed and putting these kind of declarations generate a compiler error.

Last Update: Jun. 23, 12 99


Ethiopian Civil Service College Computer Programming & Numerical Methods

6.9. Inline Functions


Any compiled program will be given memory space for its instructions to be stored.
Whenever the program is run, the individual instructions will be processed by fetching
them from memory. Consequently, functions (which are also programs particularly sub
programs) are given memory space so that the copy of the instructions will be stored
there. A function will get a chance to be executed when it is called. Therefore, whenever
there is a function call, execution will go to the instructions in memory. When the call is
completed, execution will go back to the calling function.

Going here and there might result in poor efficiency (speed). Specifically, if the functions
are very small (having two or three instructions), it will not be reasonable to jump into
these instructions as the functions are called. If these small functions are called twenty
times in your program, it is an obligation to move from the current execution point to the
copy of the instructions of the function and come back again for twenty times.

In C++, there is one solution for this problem which is making functions inline. If
functions are made to be inline, then the compiler will not create an independent copy of
the instructions but will directly insert the instructions in the place of the function call to
prevent jumping from one point to another one. No jump is made; it is just as if you had
written the statements of the function right into the calling function.

But sometimes making functions inline may not help or may result in another problem
which is also related to efficiency. The program becomes very long if (especially) large
functions are declared as inline functions. Therefore, the only candidate functions to be
inline are very small functions and are called many times in the program.

How can we make a function inline?


It is enough to declare the functions by putting the keyword inline before their return
type. Using the keyword “inline” in the head of the function is not necessary.

On page 32, a funny program is presented in which I believe using an inline function is
reasonable. An array of string(of size 14) is declared and initialized in the main function.
The elements of the array are sent to the function named PrintMessage so that each
message is displayed on the screen. Consequently, the program prints 14 messages turn
by turn by making a delay of two seconds between each message. The bottom line is the
small function PrintMessage is called 14 times.

Example 27:
#include <iostream.h>
#include <conio.h>
#include <dos.h>
inline void PrintMessage(char* msg);
void main()
{
char* greetings[14]={"Hello Dear User", "Welcome to this game",
"Here we have the funs", "You will be happy to be here",
"Sooner or later you will get the dearest thing in the world",
"That is OUR GREETING and WELCOME", "Are you angry?",
"We have another fun which makes you cheerful",
"Wait please………..",

Last Update: Jun. 23, 12 100


Ethiopian Civil Service College Computer Programming & Numerical Methods

"Fire!Fire!Fire",
"All your files are being deleted from your harddisk",
"..........................",
"You better shut down the system",
"No,no,no! Ha,Ha,Ha………… don't do that! we are kidding" };
clrscr();
for (int i=0;i<14;i++)
{
if (i==11)
for(int j=1;j<=5;j++)
{
PrintMessage(greetings[i]);
delay(400);
}
else
{
PrintMessage(greetings[i]);
sleep(2);
}
}
getch();
}
void PrintMessage(char* msg)
{
cout<<msg<<endl;
}

Hello Dear User


Welcome to this game
Here we have the funs
You will be happy to be here
Sooner or later you will get the dearest thing in the world
That is OUR GREETING and WELCOME
Are you angry?
We have another fun which makes you cheerful
Wait please……….
Fire!Fire!Fire
All your files are being deleted from your harddisk
….........................
….........................
….........................
….........................
….........................

You better shut down the system


No,no,no! Ha,Ha,Ha………… don't do that! we are kidding!"

Last Update: Jun. 23, 12 101


Ethiopian Civil Service College Computer Programming & Numerical Methods

Last Update: Jun. 23, 12 102


Ethiopian Civil Service College Computer Programming & Numerical Methods

PART TWO: NUMERICAL METHODS


1. Number System and Error Analysis in Numerical Methods
1.1. Introduction
The solution of most physical problems starts with the representation of the system under
consideration by a set of mathematical equations, a step which is known as modelling.
The remaining steps involve solving the equations and interpreting the results obtained.
The procedure might involve, for instance, solving a set of differential equations or a set
of simultaneous equations, finding the roots of equations, etc.
Through the ages, the solutions to the mathematical problems were sought analytically or
graphically. However, many mathematical problems can not be solved analytically and
the graphical approach is often not sufficiently accurate and gets too complicated to
implement as the number of parameters involved increases. An alternative to the above
two ways of solution is the numerical approach. Numerical methods are then the
techniques by the use of which mathematical problems are formulated so that they can be
solved with arithmetic operations. They often involve large number of repetitive
operations.
The wide-spread use of numerical methods today is due to the development of fast and
high capacity digital computers, which handle large number of arithmetic operations
within a short time. Hence the use of numerical methods is strongly connected with
computer programming (solving mathematical problems using computers).
In practice there are commercial packages with a suite of programs that are designed to
solve general-purpose problems, such as solving system of differential equations, or
simultaneous equations, curve fitting, optimization, etc. Such programs can be bought
(e.g. Numerical Recipes, by W. H. Press et al), or even downloaded free from internet
(CAUTION! check all programs for good performance, also danger of virus infected
programs), or found in some books. Frequently, however, one has to write programs to
solve specific problems.
In solving problems using the numerical approach, there are two fundamental concepts
which are part of almost all procedures; these are Recursion and Successive
approximation.
Recursion refers to the procedure by which a given functional value is computed in
terms of a sequence of values. The formula that relates the successive terms is known as
the recursion formula. For instance the factorial of an integer n can be computed as:
1 * 2 * 3 * … * (n-1) * n
Or if Fi represents the factorial of number i, then
Fi = F(i-1) * i //Where: F0 = F1 = 1
Hence Fi = F(i-1) * i, which is the recursive formula for the computation of the factorial.
Thus the factorial of any integer n can be computed as shown with the segment of a
program code shown below:
for (int i = n; n>2; n--)
i *= (n-1); // Computes n factorial.
F = i ; // the required result, F!.

Last Update: Jun. 23, 12 103


Ethiopian Civil Service College Computer Programming & Numerical Methods

Another example where recursion is used is in the computation of a sum of functional


values. For example the exponential function ex can be evaluated by the infinite series
ex = 1 + x + x2/2 + x3/3! + x4/4! + …
this can then be reformulated as
S0 = 1, S1 = 1 + x, S2 = 1 + x + x2/2,…
Hence
S0 = 1
S1 = S0 + x
S2 = S1 + x2/2

Sn = Sn - 1 + xn/n!
is the recursion formula for the computation of ex to any desired degree of accuracy,
which depends on value of n.
Successive approximation refers to the procedure by which more precise values of a
solution are computed by successively (step by step) improving trial values. A good
example could be the bisection method of finding the root of an equation in a given
interval.
1.2. The Error Problem
The computer performs its calculation of numerical problems using real numbers. These
numbers are called machine numbers. The number used depends up on the capacity of
the computer. If the capacity of the computer is exceeded they have to be rounded off.
This introduces the so-called round -off error. Thus we have the problem of round -off
error.
1.2.1. Elementary theory of errors
In the process of solving a problem one has to deal with numbers that may be exact or
approximate. An error is defined as the difference between the true value of a quantity
and its approximation.
In general there are two broad sources of errors:
i. Those because of the deficiency of the mathematical formulation in describing the
physical problem correctly. These are caused, for example, by simplifying assumptions
regarding the physical system, the inability of mathematics to correctly describe the
physical phenomena under consideration, etc. Errors of this kind are in general
negligible, as long as the physical problem is reasonably well described, even if it is not
fully described by the set of mathematical equations used. Moreover, the errors due to
inaccurate estimation of parameters in the mathematical equations fall in this category.
ii. Those incurred in finding the solution numerically, i.e. computational errors. This
consists of all the errors due to rounding (round off error) and truncation (approximation
of an infinite series by a finite one, for instance).

1.2.2. Absolute and relative errors


From the definition True value = approximate value + error
Error, Ea = True Value - Approximate Value = Absolute Error
And relative error is defined as:
Er = Error / True Value

Last Update: Jun. 23, 12 104


Ethiopian Civil Service College Computer Programming & Numerical Methods

The relative error is sometimes important to get a feeling on how 'significant' an error is.
Thus for a number A, if a is its approximation, then:
Ea = A - a
In reality, however, we seldom know the true value of a quantity. Hence to talk about the
correct value of the error might not make sense. Therefore, it is a usual practice to talk
about the upper bound of the error rather than the true magnitude of the error, it is
written:
Ea /A - a/
Or
A = a Ea
Thus the error is taken as the smallest number that is greater than or equal to the
difference between the true value and the approximate value.
In the same way the definition for the relative error is somewhat modified to reflect the
fact that the true value is not known,
A a
Er , a 0
a
and the true value can be written as
A = a (1 Er)
Still in some applications, the solution of a given problem is obtained through successive
approximation. In such cases the value at the end of any iteration apart from the last one
is an approximation of the final (approximate) solution. Hence in such cases an estimate
of the error, at the end of any iteration, is made based on approximate values of the
present and previous iteration. Hence,
Ea = present approximation - previous approximation,
and
Er = (present approximation - previous approximation)/ present approximation
Note: in all the above definitions of errors same symbols are used for simplicity.
Whichever of the definitions is used in a particular case should be clear from the context.
1.3. Number Representation in the Computer
Since there is a fixed space of memory in the computer, a given real number in a certain
base must be represented in a finite space in the memory of the machine. This means that
not all real numbers can be represented in the memory. The numbers that can be
represented in the memory of the computer are called machine numbers. Numbers
outside these cannot be represented. There are two conventional ways of representation.
1.3.1. Fixed Point Representation:
Suppose: the number to be represented has t digits. In the fixed point representation
system, the t digits are subdivided into t1 and t2 , where t1 is reserved for the integer part
and t2 is reserved for the fractional part of the number. Here, whatever the value of the
number is, the numbers t1 and t2 are fixed a priori, i.e. the decimal point is fixed.
Example
Suppose: a = (0111)2 is the number to be represented. Then:
if t1 = t2 = 2, then a = (01.11)2.

Last Update: Jun. 23, 12 105


Ethiopian Civil Service College Computer Programming & Numerical Methods

On the other hand,


if t1 = 0 and t2 = 4, then a = (0.0111)2
and a = (0111.)2 for t1 = 4 and t2 = 0
Note:- In modern computers, the fixed point representation is used for integers only.
Hence t1 = t and t2 = 0.
In fact, all integers less than or equal to t can be represented whereas those greater than t
cannot be represented in the memory of the computer.
1.3.2. Floating Point Representation:
Every real number “a” can be written as a = pNq, where p is any real number, N is the
chosen base of representation and q is an integer
The above representation of “a” is said to be normalized if N-1 p <1.
Example
If a = 36.72, then a = 0.3672 x 102
So that p = 0.3672, N = 10 and q = 2.
We call p and q, the mantissa and characteristic (exponent), respectively of the number
“a”. Thus, if the base is known or fixed, any real number “a” can be represented by the
ordered pair (p, q) and it will be sufficient to store this pair in the computer.
Every real number in a computer is represented as follows.
space reserved for the number a (n-bits)
S (+ or -) Characteristic/Exponent Mantissa
Sign Bit r bits t bits in base N
N.B.
A number cannot be represented exactly if it contains more than t bits in the mantissa. In
that case it has to be rounded off.
Note that in the normalized representation, we have:
a = 0. xxx.......Nq = pNq , where N-1 < mantissa < No = 1

x 0 (i.e. the number after the point must not be a zero)


For a 32-bit representation of the number, we have the following situation.
23456789 Mantissa = 7 + 16 = 23 bits
0/1 Exponent . . . . . . . . ………… ………………………………………
Sign Bit 8 bits 7 bits 16 bit
Note that the mantissa is all the digits after the point.
The characteristic or exponent must satisfy the inequality:
m < q < M ,
where m and M (m < 0 and M > 0) vary from computer to computer.
The floating point representation allows us to represent much more numbers than the
fixed point representation does.

Last Update: Jun. 23, 12 106


Ethiopian Civil Service College Computer Programming & Numerical Methods

1.4. Number Storage within the Memory of the Machine


Assume a computer with a 16-bit per word memory. In most computers integers are
represented in one memory word. The left-most bit is used as the sign bit and the rest are
used to represent the magnitude of the integer. The storage format of integers in a typical
16-bit per word computer is given below.

Bit number 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Sign Bit Bit pattern representing the integer

0 positive number
Note: Sign bit
1 negative number
Example
Suppose we want to represent the decimal number 21.
The binary equivalent of 21 is 10101, i.e., (21)10 = (10101)2.
We observe that the bit pattern for 21 has only 5 bits.
To store it in a 16-bit word, we have to expand it into an equivalent 16-bit pattern. This
can be done by adding as many zeros as are necessary to the left of the bit pattern 10101.
i.e., (10101)2 = (0000000000010101)2
Thus the representation of 21, in a 16 bit word computer will be:

0 000000000010101

On the other hand, the representation of -21 would be:


1 000000000010101

1.5. The Rounding off Problem


In a floating point representation system with t digits for the mantissa, a number with
mantissa greater than t digits cannot be represented exactly. Thus, such a number must
some how be rounded off to t digits. There are essentially two methods that are used in
rounding off.
1.5.1. Chopping:
Here all the real numbers with mantissa greater than t are dropped off after the tth digit.
Example:
Let a = 0.556712 with N = 10 and t = 3. a has 6 digits of mantissa.
Thus all the numbers after t = 3 must be chopped or truncated resulting in a = 0.556.
1.5.2. Symmetrical Rounding off :
Here we consider the (t + 1)th digit and add the number ½ N-t to the mantissa to obtain
p + ½ N-t. The result of this sum is then chopped at the tth digit.

Last Update: Jun. 23, 12 107


Ethiopian Civil Service College Computer Programming & Numerical Methods

Example
Consider a = 0.556712 with N = 10 and t = 3
Here the mantissa is 0.556712
Now, 1/2 N-t = 1/2 x 10-3 = 0.5 x 10-3 = 0.0005
Adding this to the mantissa, we have:
p + 1/2 N-t = 0.556712 + 0.0005 = 0.557 212
chopped here
a = 0.557
Note:
1. The difference between x and fl(x) is called the round off error.
2. IBM computers use the “chopping” type of rounding off while the PDP11 uses the
symmetrical rounding off.
3. In a floating-point representation, if the exponent q > M, we have what we call
overflow and if q m, we have underflow.

1.6. Numerical Errors


Every floating point operation in a computational process may give rise to an error
which, once generated, may then be amplified or reduced in subsequent operations.
Example
Suppose p1 is the mantissa of the machine, and
p2 is the successive mantissa of the machine.
Let N = 10 and t = 3.
If p1 = 0.556, then p2 = 0.557
N-t
p1 p2
The distance between two successive mantissas is:
p2 - p1 = N-t = 10-3 = 0.001

1.6.1. Error Committed by Chopping


Let p be the mantissa of a number a = pNq, which is not a machine number and let p lie
between p1& p2 (i.e. between the two mantissas of the machine). Let a = pNq be the
representation of a in the machine obtained after rounding off.

p1 p p2

Now, p (p1, p2) with the extremes excluded. Infact: p - p N-t


Example
If p = 0.5563, then p = 0.556 = p1 (after chopping)
Thus, p - p = 0.0003 < 10-3 which verifies the assumption p - p N-t,
Therefore, the error committed by chopping is N-t.

Last Update: Jun. 23, 12 108


Ethiopian Civil Service College Computer Programming & Numerical Methods

1.6.2. Error Committed by Symmetrical Rounding off


Let p lie between p1 and p2.
N-t

p1 p p2
-t
Locate the point: p1 + 1/2 N between p1 and p2 as shown below.

p1 p1 + 1/2N-t p2

If p (p1, p1 + 1/2 N-t ), then p = p1.


On the other hand, if p (p1 + 1/2 N-t, p2), then p = p2.
Thus, for the symmetrical rounding off: p - p < 1/2 N-t.

Example
Let p = 0.5561.
We observe that p lies in the left interval (p1, p1+1/2 N-t)
i.e. 0.5561 + 1/2 N-t = 0.5561 + 0.0005
= 0.556 6
p
p = p = p1
0.556 + 1/2 x10-3
p1 = 0.556 p = 0.5565 p2= 0.557
-3
Thus, p - p = 0.5561 - 0.556 = 0.0001 1/2.10 = 0.0005
-t
If instead p = 0.5567, then: 0.5567 + 1/2N = 0.5567 + 0.0005 = 0.557 2
p= 0.557 = p2
-3
Again, p - p = 0.557 - 0.5567 = 0.0003 1/2 x10 = 0.0005
Conclusion:
t
N forchopping
p-p t
1
2 N forsymmetrical roundingoff
In general, if a = pNq and a = pNq, then:
N qN t
Nq t
forchopping
a - a = pNq - pNq = Nq p - p
N q 12 N t 1
2 Nq t
for symmetrical roundingoff
Definition: Let be the exact value and be the approximation.
Then we say: ( - ) is the absolute error and ( - )/ is the relative error.

Last Update: Jun. 23, 12 109


Ethiopian Civil Service College Computer Programming & Numerical Methods

1.6.3. Relative Error:


Let a = pNq , and a = pNq.
a a a a q 1
Then, q 1
because a N
a N
q t
N N N
1 t
(chopping )
q 1
q
a a N
Moreover, N ( p p) q 1 t
q 1 q 1
N N N .2 N 1 1 t
( symmetrical rounding off )
q 1 2 N
N
We define the number N 1 t , as the machine accuracy or precision number.
Note : is the smallest number which can be added to 1 so that the result is > 1.
i.e. 1 + > 1. However, if we have a number ‘, such that ‘ < , then 1 + ‘ = 1
In other words, is smallest number that the computer can use. It is an indication of the
accuracy of the machine. “ “ is also termed as the Epsilon of the machine.

1.7. Computational Problems and Algorithms


A Problem is an unambiguous and precise relation between a set of input & a set of
output.

x1
i.e. in the relation : y = f(x) , x x2 input and y output
.
.
xn

x f y

An Algorithm is an unambiguous and precise description of operations executed on input


data in a finite number of steps, transforming it to the desired output.
First of all, we have to study the condition of the problem. That is, is the problem
sensitive to the perturbation (agitation) of the input data?
A problem is said to be well-conditioned if small perturbation in input data causes only
little perturbation in the output. Otherwise, it is said to be ill-conditioned.
Note:
Numerical results furnished by algorithms implemented on a computer are influenced by
different types of errors.

Last Update: Jun. 23, 12 110


Ethiopian Civil Service College Computer Programming & Numerical Methods

1. Errors on the input data:-


- can arise from measurement errors
- can come from results of other algorithms
- the input data can be exactly known but may have to be rounded off to the machine
number
- round off error in irrational numbers (such as and non-terminating fractions etc.
2. Round off errors:
- These errors result after the computation is performed by the computer.
3. Truncation errors:-
- Occurs when a process is truncated (broken off) before its end.
e.g. - truncation of a non-converging series
- approximation of mathematical models
- derivatives substituted by an incremental ratio ( i.e. dy/dx X Y)
Consider a numerical problem defined by the function:
y = f(x1, x2,...........,xn) = f(x), (1.9)
where, x = (x1,x2,.............,xn) is the input data and y is the output.
We begin with data : x = ( x1, x2,................, xn), which are approximations (rounding
off) of the exact data : x = x + x .
We try to evaluate the effect of the perturbated data on the result y. In other words, we
evaluate the effect of xi = xi - xi . Note that we are assuming the operation performed
is exact and, therefore, no other errors are introduced in the evaluation of f( x ) except
the error in the input data.
If f (x) is differentiable at least twice, and if its second derivative is continuous, we can
obtain a simplified approximate response using Taylor's series as follows:

f(x) = f( x + x) = f( x ) + f/ x1 x . x1 + f/ x2 x . x2 + .......... + f/ xn x .
xn +
+ 2nd order terms (1.10)

y = f(x) - f( x ) = f( x + x) - f( x )
= f/ x1 x . x1 + f/ x2 x . x2 +...........+ f/ xn x . xn +
+ 2nd order terms (1.11)
Considering the relative error, we have:

y/y = f/ x1 x= x . (x1/y) . ( x1/x1) + ...........+ f/ xn x= x . (xn/y) . ( xn/xn) +


+ 2nd order terms (1.12)

yy f/ x1 x . x1 y . x1 x1 + ...........+ f/ xn x . xn y . xn xn +
+ 2nd order terms (1.13)

[Eqn. (1.13) results from Eqn. (1.12) because for any real number a, if a = a1 + a2 +
......+an ,
then : a a1 + a2 + .... + an ]
Note that
f/ xi x . xi y represents the amplification coefficient of the relative error xi / xi .

Last Update: Jun. 23, 12 111


Ethiopian Civil Service College Computer Programming & Numerical Methods

If little perturbation xi xi causes perturbation of same order of magnitude in yy


, then the problem is said to be well-conditioned.
Note:
When we talk of conditioning, it only refers to the problem and it has no relation to the
rounding off that occurs in the operation of the machine (computer)
Stability of an Algorithm:- An algorithm is said to be stable if it is insensitive to the
perturbation of the input data; otherwise it is unstable. Obviously, this test applies only
to well conditioned problems because it is meaningless to talk about stability or
un-stability of an algorithm if the problem is ill-conditioned
Conclusion:
1. 1 = f(x) - f( x ) :- This is an error introduced as a result of the rounding off of
the input data. This error is associated with the stability of the problem
2. 2 = f(x) - f1( x ) :- This error is introduced as a result of approximating f(x)
by f1(x). It is associated with the descretization of the problem or the algorithm
itself.
3. 3 = f1( x ) - f2( x ) :- This error is a result of the rounding off of the results
(output) and it is introduced by the computer itself. Here, the stability of the
algorithm comes into consideration.
Total error = f - f2( x ) = 1 + 2 + 3
Note : -
- Sometimes, these errors compensate and at other times they add up.
- If the problem is well-conditioned, then 1 is small. On the other hand, if the algorithm
is stable then 3 is small.

Last Update: Jun. 23, 12 112


Ethiopian Civil Service College Computer Programming & Numerical Methods

2. Solutions of Non-Linear Equations


2.1. Introduction
Consider the non-linear equation:
f (x) = 0 (2.1)
We want to find the roots (zeros) of this function. The zeros can be real or complex. The
function f(x) may be given explicitly as polynomial in x or as transcendental function. In
most cases f(x) may be known only implicitly; i.e., a rule for evaluating f(x) may be
known, but its explicit form is unknown. Thus, f(x) may represent the solution of a
differential equation at a specified point, while x may represent an initial condition. In
most cases, one obtains only approximate solutions to f(x) = 0 and to achieve this, one
should rely on some computational techniques. In rare cases, though, one may find exact
solutions (e.g. if f(x) is a factorable polynomial).
Now, let Я be the root of f(x).
i.e., f(Я ) = 0. (2.2)
If Я is the root searched for, then the iterative method of solution results in a sequence:
x0, x1, x2, x3, ..............., xn, such that:

猠捵⁨桴瑡›††††††††–⁨⁨⁄
EMBED Equation
(2.3)
Hence, the convergence of this sequence is essential. Therefore, the problems to be
considered are:
1. Convergence problem and
2. Stopping criteria. i.e. when should we stop the sequence and give an
approximation to the procedure.
2.2. Methods Used in Root Finding
The idea of successive approximation (Iterative method) is widely used. It starts with one
or more initial approximation to the root and produces a sequence: x0, x1, x2,..............xn
which presumably converges to the root.
To obtain the initial approximation, some methods need a pre-assigned interval [a,b];
others need a close approximation of the root itself. The later methods converge more
rapidly. The approximations can be obtained:
a) by graphical method or
b) by analytical method
Illustrations: y
a) The graphical method:
›††††††††††††††††††††††††††⁨愉
 桔⁨牧灡楨慣敭桴摯›††††††††††††††††††††
†††††††ഠ ഈ††††不 桔

N.B. The solution lies between a & b. a b x


Fig. 2.2

Last Update: Jun. 23, 12 113


Ethiopian Civil Service College Computer Programming & Numerical Methods

b) By analytical method:

x x1 x2 x3 x4
††††††††††††††††砉⁨ㄉ砲⁨㌉破††††ഈ ††††††††††††††⁨
f(x) - - + +

Table 2.1
Here, the solution lies between x2 and x3.
f(x) = x3 - x2 - 1 = 0 (2.4)
We find out that f(1) = -1 and f(2) = 3. This means that since f(x) is a continuous
function, it must vanish somewhere in the interval [1,2]. This is by the Intermediate-value
Theorem.
If f(x) were to vanish at two or more points in the interval [1,2], i.e. has more than one
root in the interval, then f((x) must vanish somewhere in [1,2]. This is by Rolles’
Theorem.

Order of Convergence:- Suppose x0, x1, x2,........xn is a sequence which converges to a


value (, and let (n = ( - xn . If there exists a number p ( 1, and a real constant c > 0, such
that:
EMBED Equation
then we say that the sequence has an order of convergence of p.

If p = 1, then the convergence is said to be linear.


If p = 2, then the convergence is said to be quadratic.
If p = 3, then the convergence is said to be cubic.
In the case of linear convergence, it is necessary that c ( 1 (generally, c < 1)

2.3. The Bisection Method

This is an example of bracketing methods which requires an interval within which the
root lies. The procedure in this method consists of continuously halving the interval that
contains the root. The convergence can be checked by determining the error as defined
below.
Let (2 = tolerance of f(x) i.e f(x) < (2
(1 = tolerance between two successive approximations.
i.e. (x k+1 - xk( ( (1 /(xk+1(
where, x0 : initial approximation , and x1 : next approximation
Algorithm
1. Input { x0, x1, (1, (2}
2. fo ( f(x0) ; f1 ( f(x1)
3. While ( f0 (> (2 and ( f1 ( > (2
3.1 If ( x1 - x0( < (1 ( x1( :
Then
3.1.1 output { x0, x1}, stop
3.2 x2 (
EMBED Equation
3.3 f2 ( f(x2)

Last Update: Jun. 23, 12 114


Ethiopian Civil Service College Computer Programming & Numerical Methods

3.4 If fo. f2 < 0


Then
3.4.1 x1 ( x2
3.4.2 f1 ( f2
Else
3.4.3 x0 ( x2
3.4.4 f0 ( f2
(The Program comes out of the loop, when (2 become greater than f0 or f1).
4. If (f0 ( ( (2
Then
4.1 output {x0}, stop
Else
4.2 output {x1}, stop
Note:- One can obtain the root faster by making full use of the information about f(x) at
each point.

Last Update: Jun. 23, 12 115


Ethiopian Civil Service College Computer Programming & Numerical Methods

3. Interpolation and Approximation


3.1. Introduction
We sometimes know the values corresponding to a set of data points, and we seek to find
a value at an arbitrary point. In the absence of analytic expression for the values of the
data points, the method of estimating intermediate values between precise data points is
called interpolation. When the point for which the value is to be estimated is outside the
range, the method is called extrapolation.
In the following sections, Polynomial Function Interpolation, Rational Function
Interpolation, Cubic Spline Interpolation and Multidimensional Interpolation methods are
discussed along with a computer program incorporating subroutines for the various
interpolation/extrapolation methods. The results of the computer program are analyzed
with a sample problem and presented in this report for each method. The computer
program subroutines developed for interpolation can also be used for extrapolation.
3.2. Polynomial Interpolation
Polynomial interpolation is the most common method of interpolation. For n data points,
there is one and only one polynomial of order n-1 or less that passes through all the
points. Polynomial interpolation consists of determining the unique (n-1)th order
polynomial that fits n data points. Although there are a Varity of mathematical formats in
which this unique polynomial can be expressed, the 0subroutine developed for Neville's
algorithm is used and the same is discussed in the report.
The general formula for an n-1th order polynomial passing through n points:
a0 + a1x + a2x2 + a3x3 + ....+an-1xn-1 Eq 2.1
Let P1 be the value at x of the unique polynomial of degree zero (i.e. a constant) passing
through the point (x1,y1); so P1 =y1. Likewise define P2, P3 ,..., PN. Now let P12 be the value
at x of the unique polynomial of degree one passing through both (x1,y1) and (x2,y2).
Likewise P23, P24,... P(N-1)N. Similarly, for higher order polynomials upto P123...N, which is
the value of the unique interpolating polynomial through all N points, i.e the desired
answer. The various P's form a "tableau" with "ancestors" on the left leading to a single
"descendant" at the extreme right. For example, with N=4,

x1: y1=P1

P12

x2 : y2=P2 P123

P23 P1234 Eq. 2.2

x3 : y3=P3 P234

P34

x4: y4=P4
Neville's algorithm is recursive way of filling in the numbers in the tableau a column at a
time, from left to right. It is based on the relationship between a " daughter" P and its
two "parents,"

Last Update: Jun. 23, 12 116


Ethiopian Civil Service College Computer Programming & Numerical Methods

Pi(i+1)...(i+m) = (x-xi+m)Pi(i+1)...(i+m-1)+(xi-x)P(i+1)(i+2)...(i+m) Eq 2.3


xi-xi+m
This recurrence holds because the two parents already agree at points xi+1...xi+m=1.
An improvement on the recurrence (Eq2.3) is to keep track of the small differences
between parents and daughters, namely to define (for m=1,2... N-1),

Cm,i=Pi...(i+m)-Pi...(i+m-1)
Dm,i=Pi...(i+m)-P(i+1)...(i+m) Eq 2.4

( xi x)(Cm,i 1 Dm,i )
Cm 1,i
xi xi m 1
Eq 2.5
( xi m 1 x)(Cm,i 1 Dm,i )
Dm 1,i
xi xi m 1

This recurrence is implemented in the subroutine of the computer program.


Illustrative example
Data points were generated from the following polynomial function of degree three,
table 2.1.
F(x) = x3 - x2 +3x + 1
The desired point of interpolation is x=2
The analytic function value, f(2), is 11
Neville’s algorithm will be employed to fit the unique polynomial of degree three
from the four data points as illustrated below.

1. The data value of the closest pointy to the desired point is selected by successive
comparison of their distance from the point of interest and the value will be assigned
ass an initial guess to the point desired.

Data points and their respective distance from x=2


x -1.0 0.5 3.0 4.0
f(x) -4.0 2.275 28.0 61.0
distance 3 1.5 1 2

Distance = / x – xi /
Hence initial guess f(2) = f(3) = 28

2. The difference between each successive data values will be used to obtain the small
incremental modifications ( C ij, D ij ), needed to arrive at the desired point.

From Eq 2.5, the initial values ( C0i, D0i ) are the differences between each
successive data value obtained by going backward and forward, i.e.,

C0i = F(xi )- f(x i-1)


D0i = f( xi ) - f(x i+1)
C01 = F(x1 )

Last Update: Jun. 23, 12 117


Ethiopian Civil Service College Computer Programming & Numerical Methods

D01 = f( x1 ) - f(x2)
= -4-2.375
= -6.375
Similarly,
C02 = 6.375
D02 = -25.625
C03 = 25.625
D03 = -33.0
C04 = 33.0
D04 = 61.0
Applying equations 2.5
( x1
x )(C02 D01 )
C11
x1 x2
3(6.375 ( 6.375)
C11
1.5
= 12.75
= 6.375
C12 = 15.375
D12 = -10.25
C13 = -33.0
D13 = -66.0
By the same talking the next small increments will be
( x3 x)(C12 D11 )
D21
x1 x3
= -2.25
( x2 x )(C13 D12 )
C22
x2 x4
= -9.75
D22 = 13
Following the family tree, one end up at D31, hence the final correction, D31, is
( x4 x )(C22 D21 )
D31
x2 x4
=3
Finally the incremental values will systematically be added /the computer adds at each
step/ and the final value is the interpolation result
x1 f(x1) = P1
P12
x2 f(x2) = P2 P123
P23 P1234
X3 f(x3) = P3 P234
P34
X4 f(x4) = p4

Last Update: Jun. 23, 12 118


Ethiopian Civil Service College Computer Programming & Numerical Methods

Path followed for interpolation


F(2) = f(x3) + C11 + D22 + D3
= 28 + (-33) +13 +3
= 11.0
Hence the error is zero, i.e., the interpolation function is polynomial of same degree and
coefficient as that of the analytic.
3.3. Rational Function Interpolation
Some functions are not well approximated by polynomials, but are well approximated by
rational functions, that is quotients of polynomials. We denote a rational function
passing through the m+1 points (xi,yi)...(xi+m,yi+m)by Ri(i+1)...(i+m). More explicitly,
suppose.
Pμ (x) po p1x ... pμx ν
Ri(i 1)...(i m) Eq 3.1
Qν (x) qo ... qμ x ν
Since there are + + 1 unknown p's and q's (qo being arbitrary), we must have
m + 1= + + 1 Eq 3.2
In specifying a rational function interpolating function, we must give the desired order of
both the numerator and the denominator.
Rational functions are superior to polynomials, roughly speaking, because of their ability
to model functions with poles, that is, zeros of the denominator of equation 3.1. These
poles might occur for real values of x, if the function to be interpolated itself has poles.
More often, the function f(x) is finite for all finite real x, but has an analytic continuation
with poles in the complex x-plane. Such poles can themselves ruin a polynomial
approximation, even one restricted to real values of x. If wee draw a circle in the complex
plane around m tabulated points, then we should not expect polynomial interpolation to
be good unless the nearest pole is rather far outside the circle. A rational function
approximation, by contrast, will stay "good" as long as it has enough powers of x in its
denominator to account for (cancel) any near by poles.
For the interpolation problem, a rational function is contracted so as to go through chosen
set of tabulated functional values. One sometimes constructs a rational function
approximation by the criterion that the rational function of equation (3.1) itself have a
power series expansion that agrees with the first m+1 terms of the power series expansion
of the desired function f(x). This is called Pade’ approximation.
Bulirsch and Stoer found an algorithm of the Neville type, which performs rational
function extrapolation on tabulated data. The Bulirsh-stoer algoriithm produces the so-
called diagonal rational function, with the degrees of numerator and denominator equal
(if m is even) or with the degree of the denominator larger by one (if m is odd, see
equation 3.2 above). The algorithm is summarized by a recurrence relation exactly
analogous to the recurrence equation (2.3) for polynomial approximation:
R(i 1)...(i m)- Ri...(i m - 1)
Ri(i 1)...(i m) R(i 1)...(i m)
……Eq 3.3
x - x1 R(i 1)...(i m) - Ri...(i m - 1)
1 1
x - xi m R(i 1)...(i m) - R(i 1)...(i m - 1)

Last Update: Jun. 23, 12 119


Ethiopian Civil Service College Computer Programming & Numerical Methods

This recurrence generates the rational function through m+1 points form the ones through
m and (second term in the denominator of equation 3.3) m-1 points. It started with
Ri=yi Eq 3.4
and with

[
R= Ri(i+1)...(i+m) with ]
m=-1 = 0 Eq 3.5
Now, exactly as in the polynomial case equations (2.4) and (2.5) above, we can convert
the recurrence (3.3) to one involving only the small differences
Cm,i = Ri...(i+m)-(i+m-1)
Dm,i = Ri...(i+m)-R(i+1)...(i+)...(i+m) Eq. 3.6
These satisfies the relation
Cm+1,I - Dm+1,I = Cm,i+1 - Dm,i Eq. 3.7
which is useful in proving the recurrences
Cm, i 1(Cm, i 1 - Dm, i)
Dm 1, i
x - xi
Dm, i (Cm, i 1 - Dm, i)
x - xi m 1
x - xi
Dm, i (C m, i 1 - Dm, i)
x - xi m 1
Cm 1, i Eq. 3.8
x - xi
Dm, i - Cm, i 1
x - xi m 1
This recurrence is implemented in the subroutine of the computer program, whose use is
analogous in every way to the subroutine of the polynomial interpolation.
Illustrative example
The rational function interpolation also follows same procedure as polynomial function
Interpolation. The basic difference is the way one obtained the incremental correction of
each step.
Sample data were generated from the explicit analytic function
x
.
f ( x) 2
x 1
The degree of numerator and the denominator is added to yield three; hence the four data
points will be necessary for the interpolation.
Data points and their respective distance from x=4.5
X 2.0 3.0 5.0 6.0
f(x) 0.667 0.375 0.208 0.171
Distance 2.500 1.500 0.500 1.500
The initial guess is f(4.5) = 0.208
Successive correction to this guess are obtained using equation 3.8
Similar procedure as the case of polynomial will end up with the following values of (C,
D)
D11 = 0.219
C12 = -0.521

Last Update: Jun. 23, 12 120


Ethiopian Civil Service College Computer Programming & Numerical Methods

D12 = -0.167
C13 = -0.019
D21 = 0.181
C22 = 0.133
D22 = 0.030
D31 = 0.015
Adding corrections by walking through the family tree
F(2) = f(x3) + C13 + D22 + D3
= 0.208 + (-0.019) + (0.030) + (0.015) = 11.0
= 0.234.
Hence the error is zero, i.e., the interpolation function is polynomial of same degree and
coefficient as that of the analytic.

3.4. Splines and Cubic Spline

polynomials used. Unfortunately, this is not generally true. Indeed, for various functions.
and more
between nodes as n increases. Hence we need to see a method of spline.
Spline interpolation is a piecewise polynomial interpolation. This means that a function
f(x) is given on an interval a< x <b, and we want approximate f(x) on this interval by a
function g(x) that is obtained as follows. We partition a< x <b; that is, we subdivide it
into subintervals with common endpoints
a =x0 <x1 < . . .< xn = b.
We now require that g(x) = f(x), . . . ,g(x) = f (x), and that in these subinterval g(x) be
given by polynomials, one polynomial per subinterval, such that at those endpoints, g(x)
is several times differentiable . Hence, instead of approximating f(x) by a single
polynomial on the entire interval a < x < b, we now approximate f(x) by n polynomials.
In this way we may obtain approximating functions g(x) that are more suitable on many
problems of interpolation and approximation. In particular, this method will in general
be numerically stable; these g(x) will not be as oscillatory between nodes as a single
polynomial on a < x < b often is. Function thus obtained are called Spline.
The simplest continuous piecewise polynomial approximation would be by piecewise
linear function. But the graph of such a function has corners and would be of little
practical interest. Hence it is preferable to use functions that have a certain number of
derivatives everywhere on the interval a < x < b.
We shall consider cubic splines, which are perhaps the most important ones from a
practical point of view. By definition, a cubic spline g(x) on a < x < b is a continuous
function g(x) that has continuous first and second derivatives everywhere in that interval
and , in each subinterval of that partition, is represented by a polynomial of degree not
exceeding three. Hence g(x) consists of cubic polynomials, one in each subinterval.

Last Update: Jun. 23, 12 121


Ethiopian Civil Service College Computer Programming & Numerical Methods

Illustrative example
Data from cubic polynomial of Y= -3/4(x-1) + 3/2(x-1) 2 – 3/4(x-1) 3

Data Number X (n) Y (n)


1 -2.00 0.00
2 -1.00 0.00
3 0.00 1.00
4 2.00 0.00
5 1.00 0.00
Take values of YP1=YPN=0.00
YP1 and YPN are first derivatives of the approximating cubic spline at the initial and
final data point.
Required: find the corresponding value of Y at X equals to 1.50.
First derivative of the approximating cubic spline is given by
dy/dx = (Yj+1 - Yj)/(Xj+1 – Xj) – ((3*A-1)/6)(Xj+1 – Xj)*Y”j +
((3*B-1)/6)(Xj+1 – Xj)*Y”j+1 .

Where, A =( Xj+1 – X)/(Xj+1 – Xj) and B = (X – Xj)/(Xj+1 – Xj)


From the above expressions we get
(2/6)*Y”(1) + (1/6)*Y”(2) = 0
(1/6)*Y”(4) + (2/6)*Y”(5) = 0 Eq 4.1
To ensure continuity of the curve at the data points the first derivatives from the right and
the left must be equal. Therefore, we get the following expression
((Xj – Xj-1)/6)*Y”j-1 + ((Xj+1 – Xj-1)/3)*Y”j + ((Xj+1 – Xj)/6)*Y”j+1

= (Yj+1 – Yj)/(Xj+1 – Xj) – (Yj– Yj-1)/( Xj – Xj-1). Eq 4.2

From the above expression we get the following relations:


Y”(1) + 4*Y”(2) + Y”(3) = 6
Y”(2) + 4*Y”(3) + Y”(4) =-12
Y”(3) + 4*Y”(4) + Y”(5) = 6 Eq 4.3
Combining equation 1.1 and 1.2 we get:
Y”(1) = -1.50 Y”(2) = 3.00 Y”(3) = -4.50 Y”(4) = 3.00 Y”(5) = -1.50
Using the cubic spline approximating equation
Y = A*yj + B*Yj+1 + C*Y”j + D*Y”j+1 Eq 4.4

Where, C = ((A3 – A)/6)*(Xj+1 – Xj) 2 and D = ((B3 –B/6)*(Xj+1– Xj)2

A = 0.5 B = 0.5 C = -0.0625 and D = -0.0625


Which results in Y = -0.09375 (interpolation value).

Last Update: Jun. 23, 12 122


Ethiopian Civil Service College Computer Programming & Numerical Methods

3.5. Interpolation in Two or More Dimensions


In multidimensional interpolation, we seek an estimate of y(x1,x2,x3,…xn) from an n
dimensional grid of tabulated values y and n one dimensional vectors giving the tabulated
values of the independent variables x1, x2,x3,….x for clarity, we will consider explicitly
only the case of two dimensions being analogous in every way.
The simple interpolation in two dimensions is bilinear interpolation on the grid square. It
is formulas are:

4 3
X2 (K+1)

(X1, x2)
D2

1 2
X2 (K)

D1
X1 (J) X1 (J+1)

t ( x1 X 1A( J ) /[ X 1A( J 1) X 1A( J )]


Eq 5.1
u ( x2 X 2 A( K ) /[ X 2 A( K 1) X 2 A( K )]

So that t and v each lie between 0and 1 and


y( x1 , x2 ) (1 t )(1 u) y1 t (1 u) y2 tuy3 (1 t )uy4 Eq 5.2
There are two distinctly different directions that one can take in going beyond bilinear
interpolation to higher order methods.
1. One can use higher order to obtained increased accuracy for the interpolation
function, with out necessary trying to fix up the continuity of the gradient and higher
derivatives.
2. Or, one can make use of higher order to enforce smoothness of some of these
derivatives as the interpolating function point crosses grid square boundaries.

3.5.1. Using higher order Interpolation for accuracy


The basic idea is to break up the problem in to a succession of one-dimensional
interpolation. If we want to do M-1 order interpolations in the x1 direction, and N-1 order
in the x2 direction, we first locate an MxN sub-block of the tabulated function array that
contains our desired point (x1, x2). We then do M one dimensional interpolations in the x2
directions, to get the functional values at the points (x1A (j), x2), j=1,2…M. finally we do
a last interpolation in the x1 direction to get the answer.

3.5.2. Higher order for smoothness


Bicubic interpolation: requires the user to specify at each grid point not just the function
y (x1, x2) but also the gradients y/ x1=y1, y/ x2=y2 and cross derivative 2y/ x1 x2=y12.
Then an interpolating function that is cubic in the scaled coordinates t and v can be
found, with the following properties:

Last Update: Jun. 23, 12 123


Ethiopian Civil Service College Computer Programming & Numerical Methods

a) Te values of the function and the specified derivatives are reproduced


exactly on the grid points and

b) The values of the function and the specified derivatives change


continuously as the interpolating point crosses from one Grid Square to
another.
It is important that nothing in the equation of bicubic interpolation requires you to specify
the extra derivatives correctly. But it is a separate problem for someone to obtain the
values that are specified. Best of all is to know the derivatives analytically. Next best is to
know the derivatives analytical differencing from the functional values already tabulated
on the grid.
Y 1A( J , K ) (YA( J 1, K ) YA( J 1, K )) / X 1A( J 1) X 1A( J 1))
Y 2 A( J , K ) (YA( J , K 1) YA( J , K 1)) / X 2 A( K 1) X 2 A( K 1))
Y 12 A( J , K ) (YA( J 1, K 1) YA( J 1, K 1) YA( J 1, K 1) YA( J 1, K 1))
/(( X 1A( J 1) X 1A( J 1)) * ( X 2 A( K 1) X 2 A( K 1)))

Bicubic Spline
The other common technique for obtaining smoothness in two-dimensional interpolations
is the bicubic interpolation. To interpolate one functional value, one performs one-
dimensional splines across the rows of the table, followed by one additional one-
dimensional spline evaluations for the M rows; one must still do a constriction and an
evaluation for the final column spline.

Illustrative example
Consider the function, f (x1, x2) were,
f ( X 1, X 2) ( X 1X 2) eX1X2
Consider the grid lines
X1=0, X1=0.2, X1=0.4, & X1=0.6
X2=0, X2=0.2 X2=0.4 & X2=0.4
X2

0.8
0.1064 0.1888 0.2512
0.6
0.0738 0.1363 0.1888
0.4
0.0384 0.0738 0.1064
0.2

X1
0 0.2 0.4 0.6 0.8

Using Bicubic Interpolation

Last Update: Jun. 23, 12 124


Ethiopian Civil Service College Computer Programming & Numerical Methods

Consider the grid line X2 = 0.2 with X1 = 0.2, X1 = 0.2, X1 = 0.4, X = 0.6 and X1 = 0
As can be seen this a one-dimensional problem. Hence using Newton’s divide difference
and third degree polynomials.
f3 (X1) = b0 + b1 (X-0) + b2 (X-0)(X-0.2) + b3(X-0)(X-0.2)(X-0.4)

b0 = f (X10) = 0
b1 = f [X1, X10] = 0.1920
b2 = f [X2, X1, X0] = -0.0375
b3 = f [X3, X2, X1, X0] = 0.0042
Hence,
f3(X)=0.1920X-0.0375X(X-0.2)+0.0042X(X-0.2)(X-0.4)
Similarly interpolation functions are constructed along X2=0 and X2=0.4
The next step is evaluation step, in our case the functional values for f(0.3,0), f(0.3,0.2)
and f(0.3,0.4) ,from the already constructed functions.
The last step is construction of one interpolating function along the newly created
column.
Here in our case, the desired interpolation point is f(o.3,0.2)
f (0.3,0.2) = 0.1920(0.3) - 0.0375(0.3)(0.3-0.2) + 0.0042(0.3)(0.3-0.2)(0.3-0.4)

f (0.3,0.2)= 0.0565
Using Bicubic Spline
X2 = 0.2
X1 = 0.2, X1 = 0.4 and X1 = 0.6
For one-dimensional interpolation
f '' ( x i 1 ) f '' ( x)
f i ( xi ) ( xi x) 3 ( xi x) 3
6( x i x i 1 ) 6( x i x i 1 )
f ( xi 1 ) f '' ( xi 1 ) f ( xi ) f '' ( xi )( xi xi 1 )
( xi xi 1 ) ( xi x) (x xi 1 )
( xi xi 1 ) 6 ( xi xi 1 ) 6

…Eq5.3

( xi xi 1 ) f '' ( xi 1 ) 2( xi 1 x i 1 ) f '' ( x i ) ( x i 1 x i ) f '' ( x i 1 )


6 6 …Eq5.4
f ( xi 1 ) f ( xi ) f ( xi 1 ) f ( xi )
( xi xi 1 ) ( xi 1 xi )

In our case for X2 =0.2 constant the problem reduces to a one dimensional problem with
the pointes X1=0,X2=0.2, X1=0.4 and X1=0.6
Using Eq. 5.4 for each interval.
0.2f”(0)+0.8f”(0.2)+0.2f’’(0.4)=-0.09 …(a)
’’
0.2f”(0.2)+0.8f”(0.4)+0.2f (0.6)=-0.09 …(b)
Assuming natural spline

Last Update: Jun. 23, 12 125


Ethiopian Civil Service College Computer Programming & Numerical Methods

f”(0)=f”(0.6)=0
Solving (a) and (b)
f’(0.2)=-0.092
f”(0.4)=-0.082
Using Eq. 5.4 the interpolating functions are determined for each interval.
For the interval (0,0.2)
f (X)=0.0767X3+0.1951X
For the interval (0.2,0.4)
f (X)=-0.0767(0.4-X)3 –0.0683(X-0.2)3+0.1951(0.4-X)+0.3717(X-0.2)
For the interval (0.4,0.6)
f (X)=-0.0683(0.6-X)3 +0.3717(0.6-X)+0.5320(X-0.4)
From the above functions f(0.3,0.2) can be computed as
f (X)=-0.0767(0.1)3 –o.0683(0.1)3+0.1951(0.1)+0.3717(0.1)
=0.0565

Last Update: Jun. 23, 12 126


Ethiopian Civil Service College Computer Programming & Numerical Methods

3.6. General Flowchart

START

INTERPOLATION OPTION
1 POLYNOMIAL/RATIONAL
2. BICUBIC, CUBIC, BICYBUC SPLINE

OPTION = 1/2

READ - INPUT OPTIONS KEYBOARD/FILE


- # OF DATA AND EACH DATA POINT
- # AND POINT OF DESIRED INTERPOLATION POINT
- OUTPUT OPTIONSCREEN/FILE

SPECIFIC INTERPOLATION
FROM GROUP.

CALL SPECIFIC SUBROUTINE FOR THE TYPE OF


INTERPOLATION INTENDED.

OUTPUT – INPUT DATA


- DESIRED INTERPOLATION POINT
- VALUE AND LAST ADJUSTMENT IF
CALCULATED

END

Last Update: Jun. 23, 12 127


Ethiopian Civil Service College Computer Programming & Numerical Methods

References
8. G

Last Update: Jun. 23, 12 128


Ethiopian Civil Service College Computer Programming & Numerical Methods

Annex
I. Solution to Review Exercises & Assignments
I.1. Introduction to Programming
Exercise:
1. Write an algorithm in step form for making a cup of tea.
a. Prepare a kettle and a tea cup
b. If the kettle doesn’t have water, then fill it
c. Plug the kettle into the power point
d. If the tea cup is not empty, then empty the cup
e. Place tea leaves in the tea cup
f. Wait the water in the kettle till it boils
g. If the water in the kettle is not boiling, then go to step 6
h. Switch the power off
i. Pour the water from the kettle into the tea cup.

2. Develop an algorithm that counts all vowels on a given page of text. (assume n is
the number of letters on that page)
Step form Pseudo-code form
1. Set current to 1 Begin
2. Set last to n Current = 1
3. Set count to 0 Last = n
4. Read letter at current Count = 0
5. If letter is vowel, then increment Repeat
count by 1 Read letter
6. Increment current If letter is vowel, then
7. If current <= last, then go to step 4 Count = Count + 1
Current = Current + 1
End if
Until Current > Last
End

3. Write an algorithm in step form that sums all even numbers between 1 & 20
inclusive and displays the sum.
Begin
Sum = 0
Count = 2
Repeat
Sum = Sum + Count
Count = Count + 2
Until Count > 20
Print Sum
End

4. Develop an algorithm in pseudo-code & flow chart format to find the roots of a
quadratic equation.

Last Update: Jun. 23, 12 129


Ethiopian Civil Service College Computer Programming & Numerical Methods

Assignment:
Develop an algorithm (both pseudo-code and flow chart) for the following problems:
1. Calculate the average of the first n whole numbers and display the average
Begin
Read n
Sum = 0
Current = 1
Repeat
Sum = Sum + Current
Current = Current + 2
Until current > n
Average = Sum / n
Display Average
End

2. Calculate and display the factorial of an integer

3. Generate the Fibonacci sequence, where the user enters the value of n.
F1 = F2 = 1
Fn = Fn-1 + Fn-2

Last Update: Jun. 23, 12 130


Ethiopian Civil Service College Computer Programming & Numerical Methods

I.2. Introduction to C++


Exercises
1. Which of the following represent a valid identifier?
identifier // valid
seven_11 // valid
_unique_ // valid
full-name // invalid: ‘-‘ not allowed in id
full#name // invalid: ‘#’ not allowed in id
4by4 // invalid: can’t start with digit
default // invalid: ‘default’ is a keyword
avg_wt_of_stdnt // valid
variable // valid
object.oreiented // invalid: ‘.’ Not allowed in id

2. Which of the following represent a valid variable definition?


int n = -100; // valid
unsigned int i = -100; // valid
signed int = 2.9; // invalid: no variable name
long m = 2, p = 4; // valid
int = 2k; // invalid: 2k not an id.
double x = w * m; // valid
float y = y * 2; // valid (but dangerous!, Why?)
unsigned double z = 0.0; // invalid: can’t be unsigned
double d = 0.67F; // valid
float f = 0.52L; // valid
signed char = -1786; // invalid: no variable name
char c = ‘#’ + 2; // valid
sign char h = ‘\111’; // invalid: ‘sign’ not recognized
char *name = “Shimelis B”; // valid
unsigned char *name = “411357”; // valid

3. Write a program that converts a temperature reading in degrees Fahrenheit to its


equivalent degree Celsius value.

#include <iostream.h>
int main (void)
{
Double fahrenhait, celsius;
cout << “Temperature in Fahrenhait: “;
cin >> fahrenhait;
Celsius = 5 * (fahrenhait – 32) / 9;
cout << fahrenhait << “ degrees Fahrenheit = “ <<
celsius << “ degree Celsius\n”;
return 0;
}

Last Update: Jun. 23, 12 131


Ethiopian Civil Service College Computer Programming & Numerical Methods

Assignment
1. Write a simple calculator program which displays the sum, difference, product
and quotient of two integers entered by the user.

#include <iostream.h>
void main ()
{
int x, y;
int sum, dif, prd, qtn;
char a ;
clrscr () ;
cout << "Enter the Operator \n";
cin >> a
cout << " + for If statement \n";
cout << " - for Subtraction \n";
cout << " * for multiplication \n";
cout << " / for division \n";
cout << "Enter the two numbers:\n " ;
cin >> x >> y;
switch (a)
{
case ‘+’:
sum = x + y;
cout << x << " + " << y << " = " << sum << endl;
break;
case '-' :
dif = x - y;
cout << x << " - " << y << " = " << dif << endl;
break;
case '*' :
prd = x - y;
cout << x << " * " << y << " = " << prd << endl;
break;
case '/' :
if (y !=0)
qtn = x / y;
cout << x << " / " << y << " = " << qtn << endl;
else
cout << “ Stop! Division by zero \n” ;
break;
}
getch ();
}

Last Update: Jun. 23, 12 132


Ethiopian Civil Service College Computer Programming & Numerical Methods

2. Develop algorithm in both pseudo-code and flow chart format:


a. To calculate the LCM and GCF of two numbers,
Begin
Read x, y
P=x*y
If ( x > y), then
r = remainder of ( x / y )
Repeat while ( r > 0 )
x=y
y=r
r = remainder of ( x / y )
end loop
GCF = y
LCM = P / y
display GCF
display LCM
Else if ( x < y), then
r = remainder of ( y / x )
Repeat while ( r > 0 )
y=x
x=r
r = remainder of ( y / x )
end loop
GCF = x
LCM = P / x
display GCF
display LCM
else
Display “ Check your Input!”
Endif
End

b. To check whether a number is prime or not.


Begin
Read x
If (x 2 and remainder of (x / 2 = 0 ), then
Display ‘x is not prime number’ Eliminates even numbers
Exit
else
Repeat Loop through ODD numbers starting with
y=3 3
z=x
Do While z > y
If (remainder (x / y) = 0) then Here the number is not prime!
display "Divisible by " , y
Exit
End If
z=x/y
y=y+2
end loop If the number passes through the loop, it is
Display “the number is Prime” prime
End

Last Update: Jun. 23, 12 133


Ethiopian Civil Service College Computer Programming & Numerical Methods

I.3. Expressions in C++


Exercises:
5. Write a program and test all the previous examples
6. Add extra brackets to the following expressions to explicitly show the order in
which the operators are evaluated:
(n <= p + q && n >= p – q || n = = 0)
(++n * q-- / ++q – q)
(n | p & q ^ p << 2 + q)
(p < q ? n < p ? q * n – 2 : q / n + 1 : q – n)

7. What will be the value of each of the following variables after its initialization:
double d = 2 * int (3.14);
long k = 3.14 – 3;
char c = ‘a’ + 2;
char c = ‘p’ + ‘A’ – ‘a’;

8. Write a program which inputs a positive integer n and outputs 2 raised to the
power of n.

Assignment: Date Due: November 30, 2007


5. Write a program that will display the area and perimeter of the following
geometric shapes given the necessary input by the user.
a. Circle c. Rectangle e. A regular polygon of side x
b. Triangle d. Trapezium
6. Write a program which inputs three numbers and outputs the message Sorted if
the numbers are in ascending order, and outputs Not sorted otherwise.

Last Update: Jun. 23, 12 134


Ethiopian Civil Service College Computer Programming & Numerical Methods

I.4. Statements in C++


Program code for illustration of Statements in C++
#include <iostream.h>
#include <conio.h>
void main ()
{
int num1,num2, max;
long int n,b;
double i;
char a ;
clrscr () ;
cout<<"Enter case"<<endl;
cout<<"Case 1 - If statement \n";
cout<<"Case 2 - If else statement "<<endl;
cout<<"Case 3 - Conditional statement "<<endl;
cout<<"Case 4 - while statement "<<endl;
cout<<"Case 5 - do while statement \n";
cout<<"Case 6 - for statement "<<endl;
cout<<"Case 7 - jump statement "<<endl;
cin>> a;
switch (a)
{
case '1':
cout<<"Compare two numbers using If statement \n";
cout<<"Enter the two numbers: " ;
cin>>num1>>num2;
if (num1%num2 == 0)
cout <<num1<<" is divisible by "<<num2<<endl;
break;

case '2':
cout<<"Compare two numbers using If...else\n ";
cout<<"Enter the two numbers: " ;
cin>>num1>>num2;
if (num1%num2 == 0)
cout <<num1<<" is divisible by "<<num2<<endl ;
else
cout <<num1<<" is not divisible by "<<num2<<endl;
break;

case '3':
cout<<"Compare two numbers using the Conditional
statement "<<endl;
cout<<"Enter the two numbers: " ;
cin>>num1>>num2;
max = num1 > num2 ? num1 : num2;
cout<<"Maximum of "<<num1<<" & "<<num2<<" is
"<<max<<endl;

num1 % num2 == 0 ? cout <<num1<<" is divisible by


"<<num2<<endl :
cout <<num1<<" is not divisible by "<<num2<<endl;
break ;

Last Update: Jun. 23, 12 135


Ethiopian Civil Service College Computer Programming & Numerical Methods

case '4':
cout<<"n! using while statement. "<<endl;
cout <<"Enter the number n"<<endl;
cin>>b ;
cout<<" using i *= --n"<<endl ;
n = b;
i = n ;
while (n>=1)
{
cout<<"n = "<< n<<" f = "<<i<<endl;
i *= --n;
}
break;

case '5':
cout<<"n! using do while statement. "<<endl;
cout <<"Enter the number n"<<endl;
cin>>b ;
cout<<" using i *= --n"<<endl ;
n = b ;
i = b ;
do
{
i *= --n;
cout<<"n = "<< n<<" f = "<<i<<endl;
}
while (n>1);
break;

case '6':
cout<<"n! using for statement. "<<endl;
cout <<"Enter the number n"<<endl;
cin>>b ;

cout<<" using i *= n"<<endl ;


n = b ;
for (i = 1 ; n > 1 ; --n)
{
i *= n;
cout<<"n = "<< n<<" f = "<<i<<endl;
}

cout<<" using i *= (n-1) "<<endl ;


n = b ;
for (i=n;n>2;n--)
{
i *= (n-1);
cout<<"n = "<< n<<" f = "<<i<<endl;
}

default:
break ;
}
getch () ;
}

Last Update: Jun. 23, 12 136


Ethiopian Civil Service College Computer Programming & Numerical Methods

Exercises:
1. Write a program to solve a quadratic equation.
2. Write a program which inputs a person’s height (in cm) and weight (in kg) and
outputs one of the messages: underweight, normal, or overweight, using the
criteria:
Underweight: weight < height / 2.5
Normal: height / 2.4 <= weight <= height / 2.3
Overweight: height / 2.3 < weight

3. Assume that n is 20, what will the following code fragment output when
executed?
if (n >= 0)
if (n < 10)
cout << “n is small\n”;
else
cout << “n is negative\n”;

4. Write a program which inputs an integer value, checks that it is positive, and
outputs its factorial. Use the formula:
factorial (0) = 1
factorial (n) = n * factorial (n – 1)

5. Write a program for the Magic Number game. The program generates a random
number, prompts for your guess, and prints the message ** Right ** if you guess
the magic number. Include the header <cstdlib.h>.

Assignment: Date due December 10, 2007


1. Write a program which inputs a date in the format dd/mm/yy and outputs it in
the format month dd, year.
For example, 25/12/61 becomes: December 25, 1961.

2. Write a program which produces a simple multiplication table of the following


format for integers in the range 1 to 9:
1 x 1 = 1
1 x 2 = 2

9 x 9 = 81

3. Write a program for the following problems


a. To calculate the LCM and GCF of two numbers,

#include <iostream.h>
#include <conio.h>
void main ()
{
Long x, y, r, p, LCM, GCF ;
cout << “Enter the two Positive Integers \n” ;
cin >> x >> y ;
P = x * y ;
If ( x > y)
{

Last Update: Jun. 23, 12 137


Ethiopian Civil Service College Computer Programming & Numerical Methods

r = x % y ;
while ( r > 0 )
{ x = y ;
y = r ;
r = x % y ;
}
GCF = y ;
LCM = P / y ;
cout <<” GCF = “ << GCF << endl;
cout << “ LCM = “ << LCM << endl ;

}
Else if ( x < y)
{
r = y % x ;
while ( r > 0 )
{ y = x ;
x = r ;
r = y % x ;
}
GCF = x
LCM = P / x
cout <<” GCF = “ << GCF << endl;
cout << “ LCM = “ << LCM << endl ;
}
Else
cout <<” Check input number! “ << endl;
}

b. To check whether a number is prime or not.

#include <iostream.h>
#include <conio.h>
void main ()
{
Long x, y, z;
cout << “Enter the two Positive Integers \n” ;
cin >> x ;
If ((x != 2) && a (x % 2 = 0 )
cout << x << “ is not prime number \n”;
else
{ y = 3 ;
z = x ;
While (z > y)
{ If (x % y = 0)
cout << "Divisible by " << y
z = x / y ;
y = y + 2 ;
}
}
cout << x << “is a Prime number \n” ;
}

Last Update: Jun. 23, 12 138


Ethiopian Civil Service College Computer Programming & Numerical Methods

4. Write a program that reverses the digits of a given positive integer.

#include <iostream.h>
#include <conio.h>
void main ()
{
long m, d, n = 0;
cout << “Enter a Positive Integer \n” ;
cin >> m ;
while (m > 0)
{
d = m % 10 ;
m /= 10 ;
n = 10 * n + d ;
}
cout << “ The reverse is “ << n << endl;
}

Last Update: Jun. 23, 12 139


Ethiopian Civil Service College Computer Programming & Numerical Methods

I.5. Arrays
Exercises:
1. Write a program that calculates the sum and average of maximum of 20 numbers
to be entered by the user.
2. Write a program that adds two matrices, entered by the user. What is the address
of the ith and jth element stored in column

Assignment:
1. Write a program that multiplies two matrices, entered by the user.
2. Write a program that calculates the determinant of n x n matrix.

Last Update: Jun. 23, 12 140


Ethiopian Civil Service College Computer Programming & Numerical Methods

II. Review Questions


II.1. Introduction to Programming
II.1.1. Algorithm Design and Representation
1. Design an algorithm for solving the following problems. Represent the algorithm in
pseudo-code.
a. Convert a value in Km to a value in m.

Begin
Read ValueInkm
ValueInm ValueInkm*1000
Display ValueInm
End
b. Find the largest of three given numbers. Take the assumption that the numbers entered
will never be equal.
A number of algorithms can be designed for this problem. Please try to design your own
algorithm before going through the next one.

Begin
Read a,b,c
max a
if max<b then
max b
end if
if max<c then
max c
end if
Display max
End
c. Design and represent an algorithm to obtain the sum of the first 50 counting numbers.
Looping structure is needed and the algorithm is presented below.
Begin
number 1
while number<=50
sum sum+number
number number+1
end while
Display sum
End
Hand trace (process the instructions yourself) the above algorithm. It seems to be perfect.
But it is not. The problem is that sum is not initialized. When we reserve space for ‘sum’
in memory, we don’t know the value stored there. Therefore we have to initialize ‘sum’
to be zero. The edited algorithm will be:
Begin
sum 0

Last Update: Jun. 23, 12 141


Ethiopian Civil Service College Computer Programming & Numerical Methods

number 1
while number<=50
sum sum+number
number number+1
end while
Display sum
End
d. Find the maximum (largest) from a given list of numbers.
In this case, the number of data in the list should be known in advance. Therefore you
need to ask for this number first.
Begin
Read n
Read number //the first number
max number
counter 1
while counter<=n
read number
if max < number then
max number
end if
counter counter+1
end while
display max
End

e. Exchange the values of two given variables.


The following algorithm works for this problem.
Begin
Read a,b
temp a
a b
b temp
display a,b
End

f. Count the occurrence of negative numbers from 10 given numbers.

Begin
cnt 0 //cnt is to say counter. Even you can use a different name for the
variable.
index 1
while index<=10
read number
if number<0 then
cnt cnt+1
end if
index index+1

Last Update: Jun. 23, 12 142


Ethiopian Civil Service College Computer Programming & Numerical Methods

end while
Display cnt
End
g. Find the largest of three given numbers. Assume there is a probability that three equal
numbers be entered.

Begin
Read a,b,c
If a=b and b=c then
Display message as to tell the user the numbers are all equal
else
max a
if max<b then
max b
end if
if max<c then
max c
end if
Display max
End if
End
h. Compute ab without using the exponential operator.

Begin
Read a,b
result 1
i 1
while i<=b
result result*a
i i+1
end while
Display result
End

Last Update: Jun. 23, 12 143


Ethiopian Civil Service College Computer Programming & Numerical Methods

2. Design an algorithm for solving the following problems. Represent the algorithm in
pseudo-code and flowchart.

a. Find the largest from two given numbers. Take the assumption that the numbers
entered will never be equal.
Start
Begin
Read a,b
Read a,b
if a>b then
max a
else
Is Yes
max b max a
a>b
end if
Display max No
End
max b

display max

Stop

b. Obtain average marks for five students for given four subject’s marks.

Start
Begin
counter 1
while counter<=5 counter 1
Read mark1,mark2,mark3,mark4 1
sum mark1+mark2+mark3+mark4
Average sum/4 No counter<=5
Display Average
counter counter+1
Stop Yes
end while
End
Read mark1, mark2,
mark3, mark4

Avg (mark1+mark2+mark3+mark4)/4

Display avg

counter counter+1 1

Last Update: Jun. 23, 12 144


Ethiopian Civil Service College Computer Programming & Numerical Methods

c. Find the maximum (largest) from a given list of numbers.

Begin Start
Read n
Read number
Read n
max number Read
counter 1 number
while counter<=n max
read number number
if max < number then cnt 1 1
max number
end if No cnt<=n
counter counter+1
end while Display Yes
display max max
End Read
Stop number

No max<nu
mber
Yes

max number

cnt cnt+1 1

d. A university employs students on a part time basis. It pays a first year student 2 Birr
per hour, a second year 3 Birr per hour, a third year 4 Birr per hour and 5 Birr per
hour to the 4th year students. Draw the flow chart of the algorithm that will accept the
name of the student, year, and the number of hours and calculate his/her total
payment. {From ‘Introduction to computer Science’ by Dr. Dida Midekso}
The pseudocode for this problem can be written like the following:

Begin
Read name,year,hour
If year=1 then
Totalpayment=hour*2
else of year=2 then
Totalpayment=hour*3
else if year=3 then
Totalpayment=hour*4
else if year=4 then
Totalpayment=hour*5
else
Display message as “Invalid year value”
Endif
Display totalpayment
end

Last Update: Jun. 23, 12 145


Ethiopian Civil Service College Computer Programming & Numerical Methods

Start

Read
name
Read year
Read hour

yes Year=1

Totalpayment hour*2 No

1
yes
Year=2
Display
Totalpayment
2
No Totalpayment hour*3 1
Stop
yes
Year=3

No Totalpayment hour*4 1

yes
Year=4

No Totalpayment hour*5 1

2 Display message
INVALID DATA

Last Update: Jun. 23, 12 146


Ethiopian Civil Service College Computer Programming & Numerical Methods

III. Sample Exam Questions


III.1. Civil Service college
UE 261 Computer Programming & Numerical Methods
Semester I 2000 E.C. (2007/2008 G.C.) Date: 13 January 2008
Mid-Semester Examination Duration: 9:00 - 10:30 am
Note: The examination is OPEN book and All questions have equal marks
1. Develop an algorithm to compute the sum of factorials.
Pseudo-code algorithm Sample Code
begin #include <iostream.h>
read n void main ()
factorial = 1 {
sum = 0 long sum = 0,factorial = 1, i = 1;
long n;
while (count < = n)
factorial = factorial * n cout << "Enter the number \n";
sum = sum + factorial cin >> n ;
count = count + 1
while ( i <= n)
display sum {
end factorial *= i ;
sum += factorial;
i +=1;
}
cout <<"Sum of "<<n << "!"<<" =
"<<sum<<endl;
}

2. What will be the value of each of the following variables after its initialization:
Declaration Output
Unsigned int x = - 3.14; x = ***** //error! X should be non-negative
double d = 2*int (3.14); d = 6 // d = 2 * 3 = 6
long k = 3.14 – 3; k = 0 // k = (0.14)L = 0
char c = ‘a’ - 32; c = A // c = 97 – 32 = 65 = ‘A’
char* b = “Love”; b = Love // b is string variable

3. Write a program which inputs a positive integer n and outputs 2n.


// PROGRAM TO CALCULATE XY.
#include <iostream.h>
void main ()
{
long base,exponent;
long pwr = 1;
cout << "Enter the base \n";
cin >> base ;
cout << "Enter the Exponent \n";
cin >> exponent ;
for (long i = 0; i < exponent; i++)
pwr *= base;
cout <<base <<"raised to "<<exponent << " = " <<pwr <<endl;
}

Last Update: Jun. 23, 12 147


Ethiopian Civil Service College Computer Programming & Numerical Methods

4. Write the output of the following program for input numbers -1, 0, 2, 8 and 13.
// PROGRAM TO CHECK WHETHER A POSITIVE INTEGER IS PRIME OR NOT.
#include <iostream.h>
void main ()
{
long i,x, y, z;
cout << "Enter the number \n";
cin >> x ;
if ((x ==2) || (x == 3))
cout << x << " is a Prime number \n";
else if ((x <= 0) || (x % 2 == 0 ))
cout << x << " is not a Prime number \n";
else
{
y = 3 ;
z = x ;
while (z > y)
{
if (x % y != 0)
{
y += 2;
if(z <= y)cout << x << " is a Prime number \n";
}
else
{cout <<x << " is not a prime number" <<endl;
break;
}
}
}
}

//output for each run


Enter the number -1
-1 is not a Prime number
**************************************************
Enter the number 0
0 is not a Prime number
**************************************************
Enter the number 2
2 is a Prime number
**************************************************
Enter the number 8
8 is not a Prime number
**************************************************
Enter the number 13
13 is a Prime number
**************************************************

Last Update: Jun. 23, 12 148


Ethiopian Civil Service College Computer Programming & Numerical Methods

III.2. Related to Object-Oriented Software Engineering


Source: http://www.csse.monash.edu.au/~jonmc/CSE2305/index.html
Copyright © Jon McCormack. All rights reserved.
Instructions
1. For each question the number of marks is given.
2. Spend approximately 1 minute per mark.
3. This list of sample questions is not exhaustive. Make sure you revise all sections
of the course.
Terms and Meanings
1. In point form summarize the significant differences between the object-oriented
and procedural approaches to software development. [5 marks]
2. Explain what is meant by object-oriented concept of "abstraction". [2 marks]
3. Explain what is meant by object-oriented concept of "encapsulation". [2 marks]
4. Define "polymorphism". [2 marks]
5. Explain what is meant by the term "genericity". [2 marks]
6. Explain the similarities and differences between the two forms of persistence:
spatial and temporal. [5 marks]
7. Explain what is meant by the term "software development model". [5 marks]
Abstraction
1. Explain the difference between abstraction and encapsulation. [5 marks]
2. What role(s) does abstraction play in object-oriented programming. [5 marks]
3. What is the principal mechanism for abstraction in C++? [3 marks]
Encapsulation
1. In point form, explain what benefits encapsulation brings to a language. [8 marks]
2. What is the principal mechanism for encapsulation in C++? [3 marks]
3. It is said that C++ does not strictly enforce encapsulation. In what way(s) is it
possible to circumvent encapsulation in C++? [8 marks]
Inheritance
1. List three reasons why it is useful to be able to inherit characteristics from parent
classes. [6 marks]
2. An inheritance hierarchy is sometimes described as a "directed acyclic graph".
Why is this description appropriate? [8 marks]
3. List the three different types of inheritance available in C++. [3 marks]

Last Update: Jun. 23, 12 149


Ethiopian Civil Service College Computer Programming & Numerical Methods

4. It is sometimes more appropriate to aggregate (i.e. define) members rather than


inherit them. Explain why. [Hint: consider a class implementing a car description
and which members will be more appropriately defined, rather than inherited from
the Vehicle base class]. [5 marks]
5. Explain how "multiple" inheritance differs from "single" inheritance. [4 marks]
6. Give the declaration of a class D that publicly inherits from class B, and privately
inherits from class C. (You may assume that class D has no additional members
of its own). [3 marks]
Genericity
1. List three possible advantages of using generic functions or classes. [6 marks]
2. List three possible drawbacks of using generic functions or classes. [6 marks]
3. If a C++ program defines both a non-generic and a generic version of a given
function, which (if either) actually gets called? [4 marks]
4. Suggest why it is appropriate that the C++ mechanism for genericity is called a
"template". [3 marks]
5. Explain how the concepts of genericity and function overloading are related. [5
marks]
Polymorphism
1. List three advantages of using polymorphism in an object-oriented program. [6
marks]
2. Explain briefly how a C++ program decides which virtual function to call during
execution. [8 marks]
3. The C programming language provide a construct which enables a primitive form
of polymorphism. What is that construct? [2 marks]
Function and operator overloading
1. Describe what is meant by the "signature" of an overloaded function, and explain
why a functions signature must be unique, even if it's name is not. [5 marks]
2. Explain how C++ manages to determine which version of an overloaded function
to call. [8 marks]
3. Explain the error in the following code: [3 marks]
double max(double a, double b)
{
return a>b ? a : b;
}

double max(int a,int b)


{
return a>b ? a : b;
}

int main(void)

Last Update: Jun. 23, 12 150


Ethiopian Civil Service College Computer Programming & Numerical Methods

{
cout << max(1,2.2);
}
4. List the advantages and drawbacks of the use of function overloading. [5 marks]
5. Suggest two or more guidelines for when overloaded functions should and
shouldn't be used. [6 marks]
6. Explain why operator overloading is often not a good idea. [5 marks]
7. List the three C++ operators which are most usually overloaded and explain why
your answer to the previous question does not apply to these three operators. [5
marks]
8. Write a overloaded operator function that allows the code:
MyClass mc;
cout << mc;
to behave identically to the code: [8 marks]
MyClass mc;
mc.PrintMeTo(cout);
9. Modify the following class declaration:
class String
{
public:
String(void)
: myStr(0) {}
String(char* str)
{ myStr = new char[strlen(str)];
strcpy(myStr,str);
}
private:
char* myStr;
};
so that the code:
String s ("a string");
s[1] = 'z';
cout << s[1] << endl;
correctly allows access to individual characters in the
string. [8 marks]
Dymanically-allocated memory
1. Explain the diference between the new and new[] operators. [4 marks]
2. Explain the diference between the delete and delete[] operators. [4 marks]
3. Write a single C++ statement that dynamically allocates a single int and initializes
it to 42. [3 marks]
4. Write one or more C++ statements that dynamically allocates an array of 42 ints
and initializes each one to zero. [3 marks]
Constructors and destructors
1. Write a declaration for a constructor for the class Annotation. Your constructor
should take two arguments: number (of type int), and ref (of type TextRef). [2
marks]

Last Update: Jun. 23, 12 151


Ethiopian Civil Service College Computer Programming & Numerical Methods

2. Modify the following class declaration:


class String
{
public:
String(void)
: myStr(0) {}
String(char* str)
{ myStr = new char[strlen(str)];
strcpy(myStr,str);
}
private:
char* myStr;
};
by adding a suitable destructor. [5 marks]
3. Modify the String class from the previous question by adding a copy constructor.
[5 marks]
Miscellaneous C++ syntax
1. Write a declaration for an enumerated type called Hombre, with values good,
bad, and ugly. [2 marks]
Class members
1. Explain why it is almost always a bad idea to declare the data members of a class
to be public. [5 marks]
2. List the semanticdifferences between this version of the Value::Negate() member
function:
void Value::Negate(void)
{
this->myVal *= -1;
}
and this one:
void Value::Negate()
{
myVal = -myVal;
}
[3 marks]
3. What is the minimum number of data members a class may have? [2 marks]
4. What is the minimum number of function members a class may have? [2 marks]
Exceptions
1. Briefly explain how exceptions work in C++. [8 marks]
2. Under what circumstances is it useful to catch exceptions by value? [4 marks]
3. Under what circumstances is it useful to catch exceptions by reference or pointer?
[4 marks]
4. Rewrite the following function so that any exceptions which are thrown by the
functions set(), game(), and match() are caught within the function play(). Your
code should print a message ("exception caught") and then call exit() if any
exception is thrown. [8 marks]

Last Update: Jun. 23, 12 152


Ethiopian Civil Service College Computer Programming & Numerical Methods

Score play(void)
{
Score s;
while (!match())
{
while (!game())
{
s = set();
}
}
return s;
}

The software development process


1. Briefly outline the various stages of the Waterfall model. [3 marks]
2. Explain why the Waterfall model is not a satisfactory model of real-world
software development. [5 marks]
3. In point form, describe the Fountain model of software development. [3 marks]
4. Draw a labeled diagram explaining Boehm's spiral model of software
development. Why is the spiral model still considered one of the better models of
software development? [6 marks]
5. List three or more properties which increase as the radius of the spiral model
increases, and explain why they increase. [4 marks]
6. List two or more properties which decrease as the radius of the spiral model
increases, and explain why they decrease. [4 marks]
7. The Spiral model covers a greater range of activities than the Fountain model.
Explain which sector(s) of the Spiral model correspond to stages in the Fountain
model. [5 marks]
8. Define "rapid prototyping" and explain how it fits into the Exploratory
Programming model of software development. [6 marks]
9. Explain what is meant by saying that "software development models are just
theories". [6 marks]
Metrics
1. Suggest why persuasiveness is a characteristic of a good metric. [5 marks]
2. Suggest why it is desireable that software metrics be programming language
independent. [5 marks]
3. Give two examples of non-computable metrics, and explain why such metrics are
of limited value. [5 marks]
4. List four quantities related to software development which can be easily
measured. [4 marks]
5. List four quantities related to software development which cannot be easily
measured. [4 marks]

Last Update: Jun. 23, 12 153


Ethiopian Civil Service College Computer Programming & Numerical Methods

6. Explain why the "lines-of-code" metric is a poor one. [5 marks]


7. Compute the cyclomatic complexity of the following program:
int globalVal = 0;

int main(void)
{
while (globalval< 2)
{
globalval *= 2;
}
else if (globalval > 8)
{
globalval -= 2;
}
else
{
globalval = 10-globalval;
}
}
for (int i=0; i<globalval; i++)
{
cout << i << endl;
}
}

Show all your working. [8 marks]


8. Compute the cyclomatic complexity of the following code fragment:
int x = 10;
do
{
double y;
if (cin >> y)
{
if (y!=0)
{
for (i=1; i<y; i++)
{
cout << i*x/y << endl;
}
}
}
}
while (x-->0);

Show all your working. [8 marks]


9. Compute the cyclomatic complexity of the following function:
template <class TYPE>
TYPE max(TYPE a, TYPE b)
{
return (a>b) ? a : b;
}

Show all your working. [8 marks]

Last Update: Jun. 23, 12 154


Ethiopian Civil Service College Computer Programming & Numerical Methods

Testing and Error Checking


1. Write a program that performs execution path testing for the code listed under the
cyclomatic complexity questions. [5 marks]
2. List five different error handling schemes and explain how they work. [10 marks]
3. List four different types of white-box testing and discuss their usefulness in
relation to object-oriented development. [8 marks]
4. Explain the value of boundry value analysis in finding errors in a program. [6
marks]
5. How does backtracking help in program debugging?[6 marks]
Classification
1. In point form, explain the difference between classical (Platonic) classification
and a priori clustering. [5 marks]
2. Why can prototype-based classification be described as "instance-outward"? [4
marks]
3. Prototype-based classification uses "paradigms". In this context what is a
"paradigm". [4 marks]
4. Give an overview of how you might use an analogicial approach to clustering to
create a software system which controls Internet traffic. [hint: think of how road
traffic is controlled] [8 marks]
Object-oriented design
1. List the five main steps in an object-oriented design process. [5 marks]
2. Describe how the "informal description" technique may be used to identify the
objects, methods, and classes in a problem. [5 marks]
3. Explain the role of experts in Domain Analysis. [4 marks]
4. When performing Domain Analysis, why is it important not to automatically
accept participants' explanations of their behaviour. [5 marks]
5. At the highest level, objects may fulfil one of three roles. List those roles and
describe the characteristics of objects in each of those roles. [10 marks]
6. Explain the difference between the datum role and the data type role. [6 marks]
7. List the four commonest relationships which may exist between two classes. [4
marks]
8. Explain how a C++ program can specify that two classes are in an "is
implemented as a" relationship. [3 marks]
9. Explain what is meant when we say that "the interesting behaviour of an object-
oriented system is (often) emergent"? [8 marks]

Last Update: Jun. 23, 12 155


Ethiopian Civil Service College Computer Programming & Numerical Methods

10. What is an "abstract base class"? Why is "programming to an interface" an


important principle of good object-oriented programming? [8 marks]
11. Draw a UML object and sequence diagram corresponding to the execution of the
following program: [8 marks]
class Base
{
public:
virtual void PrintMe(void)
{
cout << "Base!" << endl;
}
};

class Derived
{
public:
virtual void PrintMe(void)
{
Base::PrintMe();
cout << "Derived!" << endl;
}
};

int main(void)
{
Base b;
Derived d;

// object diagram should be drawn at this point


b.PrintMe();
d.PrintMe();
}

12. Draw a class diagram for a system to solve the following problem. Include key
attributes and behaviours.
The El Cheapo video store needs a computerized database system to record
borrowed videos, CDs, and video games. Videos are categorized as new release,
weekly, or five-for-five-dollars. video games may be either PlayStation,
MegaDrive, or Saturn.

El Cheapo need to be able to check whether an individual item is currently on


loan, when it is due back, how much is owing on it (if it is overdue), as well as
details of who borrowed it (i.e. name, address, phone number, licence
number).

They also need to be able to generate lists of items selected by various


criteria: item type, items currently in, items currently out, most popular, least
recently borrowed. This lists must be able to be sorted alphabetically and by
video ID number.

They also want to generate lists of customers secting by various criteria: most
prolific borrowers, inactive (no borrowing for a month or more), who owes the
most on overdue books.

[20 marks]

Last Update: Jun. 23, 12 156


Ethiopian Civil Service College Computer Programming & Numerical Methods

13. Draw a class diagram for a system to solve the following problem. Include key
attributes and behaviours.
We are creating a symbolic mathematics package. We want to be able to
specify functions of X, Y, and Z. Each function will consist of a left- and right-
hand expression, and each expression will consist of one or more terms, with
addition or subtraction operators between them.

Each term may be either a constant (a numerical value or the special constant
C) or a product (a numerical coefficient followed by one ore more of the
variables X, Y, and Z, each raised to some power).

The mathematics package will be able to perform the following operations on a


function:
Re-express it as a function of X, Y, or Z
Factorize either side
Expand all terms
Solve for X, Y, or Z
Evaluate it, given values for X, Y, and Z
Differentiate it with respect to X, Y, or Z
[20 marks]

Last Update: Jun. 23, 12 157


Ethiopian Civil Service College Computer Programming & Numerical Methods

III.3. C++ and OOPS Sample Question


Source: http://www.bestsamplequestions.com/technical-questions/cpp-sample-questions/cpp-
sample-questions.html

1. Predict the output or error(s) for the following:


class Sample
{
public:
int *ptr;
Sample(int i)
{
ptr = new int(i);
}
~Sample()
{
delete ptr;
}
void PrintVal()
{
cout « "The value is " « *ptr;
}
};
void SomeFunc(Sample x)
{
cout « "Say i am in someFunc " « endl;
}
int main()
{
Sample s1= 10;
SomeFunc(s1);
s1.PrintVal();
}
Answer:
Say i am in someFunc
Null pointer assignment(Run-time error)
Explanation:
As the object is passed by value to SomeFunc the destructor of the object is called when the
control returns from the function. So when PrintVal is called it meets up with ptr that has been
freed.The solution is to pass the Sample object by reference to SomeFunc:
void SomeFunc(Sample &x)
{
cout « "Say i am in someFunc " « endl;
}
because when we pass objects by reference that object is not destroyed. while returning from
the function.
2. Which is the parameter that is added to every non-static member function when it is
called?
Answer: 'this' pointer
class base
{

Last Update: Jun. 23, 12 158


Ethiopian Civil Service College Computer Programming & Numerical Methods

public:
int bval;
base(){ bval=0;}
};

class deri:public base


{
public:
int dval;
deri(){ dval=1;}
};
void SomeFunc(base *arr,int size)
{
for(int i=0; i‹size; i++,arr++)
cout«arr-›bval;
cout«endl;
}

int main()
{
base BaseArr[5];
SomeFunc(BaseArr,5);
deri DeriArr[5];
SomeFunc(DeriArr,5);
}
Answer:
00000
01010
Explanation:
The function SomeFunc expects two arguments. The first one is a pointer to an array of
base class objects and the second one is the size of the array. The first call of someFunc
calls it with an array of bae objects, so it works correctly and prints the bval of all the
objects. When Somefunc is called the second time the argument passed is the pointeer to
an array of derived class objects and not the array of base class objects. But that is what the
function expects to be sent. So the derived class pointer is promoted to base class pointer
and the address is sent to the function. SomeFunc() knows nothing about this and just
treats the pointer as an array of base class objects. So when arr++ is met, the size of base
class object is taken into consideration and is incremented by sizeof(int) bytes for bval (the
deri class objects have bval and dval as members and so is of size ›=
sizeof(int)+sizeof(int) ).

Last Update: Jun. 23, 12 159


Ethiopian Civil Service College Computer Programming & Numerical Methods

IV. Selected Group Activity Reports


IV.1. Independent Projects
1: Piedmont Train System [50 marks]

Question
For this assignment you are required to design and code a simple Train management
system. The Piedmont Railway Company (P.R.C.) maintains a simple railway transport
system. There are several different classes of rolling stock used by P.R.C.: Engines,
carriages, wagons and cabooses. Carriages are normally fitted with seating and come in a
number of different varieties: 1st class, 2nd class and diner cars. Wagons are used for the
transport of general materials (e.g. timbre, livestock, etc.) P.R.C. uses two main types of
engine: diesel and electric. Electric engines require overhead power on the train lines they
travel on, where as Diesel engines do not (although they can run on lines with overhead
power). Engines have a total load carrying capacity (measured in metric tonnes) which is
the total weight the engine can move (including its own weight). The weight of carriages
depends on the base carriage weight (different for each type of carriage - see table below)
plus a "PAX load" equal to 100kg x the number of people on board. For dining cars the
PAX load is 20% of the total passenger PAX transported up to a maximum of 5,000kg.
Carriages also have a maximum carrying capacity. A Wagon's weight depends on the
stock transported.
Base Weight Max carrying
Stock Max Load Journey Cost
(kg) capacity
Diesel Engine 16,000kg N/A 500 Metric Tonnes $100/km
Electric Engine 12,000kg N/A 300 Metric Tonnes $150/km
1st Class $20/km +
4,000kg 40 PAX N/A
Carriage $180/passenger
2nd Class $10/km +
3,000kg 60 PAX N/A
Carriage $100/passenger
Dinner Carriage 5,000kg 50 PAX N/A $8/km
Caboose 1,500kg N/A N/A $4/km
30 Metric
Wagon 2,200kg N/A $18/km + $300/tonne
Tonnes

P.R.C. maintains a fleet of rolling stock. Items of rolling stock are assembled into a train
which makes a journey. Each train requires at least one engine (at the front) and a
caboose (at the back). Any number of carriages, wagons and engines may be added, up to
the total carrying capacity of the engines. It is anticipated that new stock may have
different parameters than those shown in the table, so the figures should not be hard
coded into any system.
Given a particular fleet configuration we are interested in a system that allows us to build
trains suitable for a specified journey. The journey is specified with the following
parameters:
Distance
Number of passengers
Amount of freight

Last Update: Jun. 23, 12 160


Ethiopian Civil Service College Computer Programming & Numerical Methods

Start date (train components taken from available fleet)


End date (train components returned to available fleet)
Track type (powered or non-powered)
The system is to have the following features:
The ability to add and delete items from the fleet of rolling stock;
The ability to print information about each item in the fleet;
The ability to specify a journey and then have the system select rolling stock from
the available fleet to meet the requirements of the journey at the lowest cost ;
The ability to print out details of all trains and their currently assigned journeys -
this should include the total cost of the journey.
Stock that is currently assigned to a particular journey cannot be used in another journey
until after the journey is complete.
Any interaction with the system should be via a simple text interface. Make sure your
system is able to handle errors correctly (e.g. insufficient fleet available to meet requested
journey requirements).
Develop an object-oriented design for this system and implement it in C++.

What to submit for assessment


You should submit the following:
A UML class diagram of your solution (in pdf format).
Written description of key classes and their functions.
A table showing all your classes and their current state (designed, coded, tested,
fully working), and the results of applying your testing plan to your code.
All your source code files, test and data files, plus a working Makefile, with 2
targets: the default target builds the program(s), the clean target removes the
executable and any intermediate (e.g. .o files created during compilation).
Written components of the assignment should be done in plain text (i.e. readable
using a standard text editor such as vi).

Last Update: Jun. 23, 12 161


Ethiopian Civil Service College Computer Programming & Numerical Methods

IV.2. Group Projects


Group Project
1. Solutions of non-linear equations
1-1 Polynomial equations
2-1 Exponential and Logarithmic equations
2. Matrix operation and system of linear equations
1-2 Determinant & Inverse of matrix
2-2 Solution of system of Linear equations
3. Interpolation and Approximation
1-3 Using Lagrange’s and Newton’s method
2-3 Using Least square and Uniform approximation
4. Numerical Differentiation and Integration
1-4 Differentiation
2-4 Integration
5. Statistics
1-5 Measures of central tendency
2-5 Sorting, validation and data in-filing

Group Activity I:
s

Last Update: Jun. 23, 12 162

You might also like