Professional Documents
Culture Documents
Chapter 1 C++
Chapter 1 C++
Chapter One
1.1 Introduction to programming
Computer is an electronic device that:
accepts data
performs computations
And makes logical decisions according to instructions that have been given to
it.
Then produces meaningful information in a form that is useful to the user.
They have been deployed to solve different real life problems, from the simplest game
playing up to the complex nuclear energy production.
In order to solve a given problem, computers must be given the correct instruction about
how they can solve it. The terms computer programs, software programs, or just
programs are the instructions that tells the computer what to do.
Computer requires programs to function, and a computer programs does nothing unless its
instructions are executed by a CPU.
What is programming?
Programming is a skill that can be acquired by a computer professional that gives
him/her the knowledge of making the computer perform the required operation or
task.
A computer program (also known as source code) is often written by professionals
known as Computer Programmers (simply programmers). Source code is written in
one of programming languages.
What is a programming language?
Programming language is an artificial language that can be used to control the
behaviour of a machine; particularly a computer. It’s a set of different category of
written symbols that instruct computer hardware to perform specified operations
required by the designer (or programmer).
Programming languages, like natural language (such as English, Afaan Oromo,
Amharic), are defined by syntactic and semantic rules which describe their structure
and meaning respectively.
The syntax of a language (Programming language) describes the possible
combinations of symbols that form a syntactically correct program. It’s all about the
grammar of the language.
The meaning given to a combination of symbols is handled by semantics. In general,
programming languages allow humans to communicate instructions to machines.
The main purpose of programming languages is to provide instructions to a computer.
As such, programming languages differ from most other forms of human expression in
that they require a greater degree of precision and completeness.
When using a natural language to communicate with other people, human authors and
speakers can be ambiguous and make small errors, and still expect their intent to be
COMPUTER PROGRAMMING
understood. However, computers do exactly what they are told to do, and cannot understand
the code the programmer "intended" to write.
So computers need to be instructed to perform all the tasks. The combination of the
language definition, the program, and the program's inputs must fully specify the
external behaviour that occurs when the program is executed. Computer languages
have relatively few, exactly defined, rules for composition of programs, and strictly
controlled vocabularies in which unknown words must be defined before they can be
used
b. High-level languages
Although programming in assembly language is not as difficult and error prone as stringing
together ones and zeros, it is slow and cumbersome. In addition it is hardware specific. The
lack of portability between different computers led to the development of high-level
languages so called because they permitted a programmer to ignore many low-level details
of the computer's hardware. Further, it was recognized that the closer the syntax, rules, and
mnemonics of the programming language could be to "natural language" the less likely it
became that the programmer would inadvertently introduce errors (called "bugs") into the
program.
High-level languages are more English-like and, therefore, make it easier for
programmers to "think" in the programming language.
High-level languages also require translation to machine language before execution. This
translation is accomplished by either a compiler or an interpreter. Compilers translate the
entire source code program before execution. Interpreters translate source code programs one
line at a time. Interpreters are more interactive than compilers.
FORTRAN (FORmula TRANslator), BASIC (Bingers All Purpose Symbolic
Instruction Code), PASCAL, C, C++, Java are some examples of high-level
languages.
Exercise: Compare and contrast low level languages with high level languages?
The question of which language is best is one that consumes a lot of time and energy
among computer professionals. Every language has its strengths and weaknesses. For
example, FORTRAN is a particularly good language for processing numerical data,
but it does not lend itself very well to organizing large programs. Pascal is very good
for writing well-structured and readable programs, but it is not as flexible as the C
programming language. C++ embodies powerful object-oriented features. As might
be expected in a dynamic and evolving field, there is no single standard for
COMPUTER PROGRAMMING
Bottom up design: is the reverse process where the lowest level component are built
first and the system builds up from the bottom until the whole process is finally
completed.
Basic Program development tips
Reliable: the program should always do what it is expected to do and handle all types
of exception.
Maintainable: the program should be in a way that it could be modified and
upgraded when the need arises.
Portable: It needs to be possible to adapt the software written for one type of
computer to another with minimum modification (run on different hardware’s).
Efficient: the program should be designed to make optimal use of time, space and
other resources of the computer.
Before a program is written, the programmer must clearly understand what data are to be
used to get the desired result, and the procedure to be used to produce the result. The
procedure, or solution selected is referred to as an algorithm.
What is an algorithm?
Algorithm can be defined as: “A sequence of activities to be processed for getting
desired output from a given input.”
Webopedia defines an algorithm as:
“A formula or set of steps for solving a particular problem. To be an algorithm, a set
of rules must be unambiguous and have a clear stopping point”.
There may be more than one way to solve a problem, so there may be more than one
algorithm for a problem.
Now, if we take definition of algorithm as: “A sequence of activities to be processed for
getting desired output from a given input.” Then we can say that:
1. Getting specified output is essential after algorithm is executed.
2. One will get output only if algorithm stops after finite time.
3. Activities in an algorithm to be clearly defined in other words for it to be
unambiguous.
Before writing an algorithm for a problem, one should find out
what is/are the inputs to the algorithm and
what is/are expected output after running the algorithm.
There are two commonly used techniques (tools) to implement an algorithm. These are :
flowcharts and
Pseudo-code.
Modeling a programs logic using pseudo-code
Pseudo-code is writing an algorithm as close as possible to computing languages.
COMPUTER PROGRAMMING
If the condition is true, one of the two branches is explored; if the condition is false, the
other alternative is taken. This is usually represented by the ‘if-then’ construct in pseudo-
codes and programs. In flowcharts, this is represented by the diamond-shaped decision
box. This structure is also known as the selection structure.
Problem1: write algorithm to find the greater number between two numbers
Step1: Start
Step2: Read/input A and B
Step3: If A greater than B then C=A
Step4: if B greater than A then C=B
Step5: Print C
Step6: End
Problem3: A algorithm to find the largest value of any three numbers.
Step1: Start
Step2: Read/input A, B and C
Step3: If (A>=B) and (A>=C) then Max=A
Step4: If (B>=A) and (B>=C) then Max=B
Step5: If (C>=A) and (C>=B) then Max=C
Step6: Print Max
Step7: End
COMPUTER PROGRAMMING
Problem3: Design an algorithm which generates even numbers between 1000 and 2000
and then prints them in the standard output. It should also print total sum:
1. Start
2. I ← 1000 and S ← 0
3. Write I
4. S ← S + I
5. I ← I + 2
6. If (I <= 2000) then go to line 3
else go to line 7
7. Write S
8. End
Problem4: Design an algorithm with a natural number, n, as its input which calculates
the following formula and writes the result in the standard output:
S = ½ + ¼ + … +1/n
1. Start
2. Read n
3. I ← 2 and S ← 0
4. S= S + 1/I
5. I ← I + 2
6. If (I <= n) then go to line 4 else write S in standard output
7. End
1.3.2 Properties of algorithm
Donald Ervin Knuth has given a list of five properties for a, algorithm, these properties are:
1) Finiteness: An algorithm must always terminate after a finite number of steps. It means
after every step one reach closer to solution of the problem and after a finite number of steps
algorithm reaches to an end point.
2) Definiteness: Each step of an algorithm must be precisely defined. It is done by well
thought actions to be performed at each step of the algorithm. Also the actions are defined
unambiguously for each activity in the algorithm.
3) Input: Any operation you perform need some beginning value/quantities associated with
different activities in the operation. So the value/quantities are given to the algorithm before
it begins.
COMPUTER PROGRAMMING
Flow chart
The flowchart is a diagram which visually presents the flow of data through
processing systems.
This means by seeing a flow chart one can know the operations performed and the
sequence of these operations in a system.
Algorithms are nothing but sequence of steps for solving problems.
So a flow chart can be used for representing an algorithm. A flowchart, will describe
the operations (and in what sequence) are required to solve a given problem.
For example suppose you are going for a picnic with your friends then you plan for the
activities you will do there. If you have a plan of activities then you know clearly when you
will do what activity.
Similarly when you have a problem to solve using computer or in other word you need to
write a computer program for a problem then it will be good to draw a flowchart prior to
writing a computer program. Flowchart is drawn according to defined rules.
COMPUTER PROGRAMMING
Flowchart Symbols
Problem 3: - Draw flow chart of an algorithm that add two numbers and display their result.
Problem4: Algorithm for find the greater number between two numbers.
COMPUTER PROGRAMMING