Computer Programming - Chapter 2
Computer Programming - Chapter 2
HARAMAYA UNIVERSITY
HARAMAYA INSTITUTE OF TECHNOLOGY
SCHOOL OF ELECTRICAL & COMPUTER ENGINEERING
COMPUTER PROGRAMMING
2
2. Fundamentals of C++ Programming
Introduction
Algorithm and Flow Chart
Basics of C++ Programming
Variables And Assignment
Declaration and Initialization
Input /Output
3
2.1 Algorithm and Flow Chart
Algorithm is part of the blueprint or plan for the computer program.
It is an effective procedure for solving a problem in a finite number of steps.
There are three different ways of stating algorithms:
Step-Form – step-by-step procedure
Pseudocode – code like algorithm
Flowchart – Graphical design
Algorithms show these three features:
1. Sequence (also known as Process): means that each step or process in the
algorithm is executed in the specified order.
4
… Cont’d
2. Decision (also known as Selection):
In algorithms the outcome of a decision is either true or false, there is no in between.
The outcome of the decision is based on some condition that can only result in a true
or false value.
3. Repetition (also known as Iteration or Looping)
Repetition takes two forms, the Repeat loop and the While loop.
The repeat loop is used to iterate or repeat a process or sequence of processes
until some condition becomes true.
5
… Cont’d
• Algorithm could be designed using Basic flowcharting symbols
many techniques and tools.
• One tool of designing algorithm is
by using flowcharts.
• Flowchart is a graphical way of
expressing the steps needed to
solve a problem.
• A flow chart is a schematic
(diagrammatic description)
representation of a process.
Examples
6
1. Obtain two numbers from the keyboard, and determine and display
which (if either) is the larger of the two numbers
2. Find the average, maximum, minimum, and sum of three numbers given
by the user.
3. Take an integer from the user and display the factorial of that number.
4. Find the area of a circle where the radius is provided by the user.
5. Receive 3 numbers and display them in ascending order from smallest
to largest
2. Preprocess,
3. Compile,
4. Link,
5. Load
17
1. Edit
• This is accomplished with an editor program.
• The programmer types C++ statements with the editor and makes corrections if
necessary.
• The programs source file is then stored on secondary storage device such as a disk with
a “.cpp” file name.
• After the program is edited, C++ is principally compiled in three phases:
1. Preprocessing,
2. Translation to object code, and
3. Linking
18
2. Preprocess
In a C++ system, a preprocessor program executes automatically before
the compiler’s translation phase begins.
The C++ preprocessor obeys command called preprocessor directives,
Which indicate that certain manipulations are to be performed on the program
before compilation.
The preprocessor is invoked by the compiler before the program is
converted to machine language.
The C++ preprocessor goes over the program text and carries out the
instructions specified by the preprocessor directives (e.g., #include).
The result is a modified program text which no longer contains any
directives.
19
3. Compile
The C++ compiler translates the program code.
The compiler translates machine independent high level source code
into machine dependent object code.
The compiler may be a true C++ compiler which generates native
(assembly or machine) code.
The outcome may be incomplete due to the program referring to
library routines
Which are not defined as a part of the program.
For example, the << operator which is actually defined in a separate
IO library.
20
4. Linking
C++ programs typically contain references to functions and data
defined else where, such as in the standard libraries.
The object code produced by the C++ compiler typically contains
“holes” due to these missing parts.
A linker links the object code with the code for the missing function
to produce an executable image (with no missing pieces).
Generally, the linker completes the object code by linking it with
the object code of any library modules that the program may
have referred to.
The final result is an executable file (.exe)
21
5. Loading
The loader takes the executable file from disk and transfers it to
memory.
Additional components from shared libraries that support the
program are also loaded.
Finally, the computer, under the control of its CPU, executes the
program.
In practice all these steps are usually invoked by a single command
and the user will not even see the intermediate files generated.
22
Summary of C++ program phases to be executed
23
Structure of C++program
• The structure of a simple C++ program will have the following format:
Source code Output
24
… Cont’d
// my first program in C++
This is a comment line.
They can be used by the programmer to include short explanations or observations within the source
itself.
Comments are pieces of source code discarded from the code by the compiler.
They do nothing other than giving information for the reader.
Their purpose is only to allow the programmer to insert notes or descriptions embedded within
the source code.
C++ supports two ways to insert comments:
1) // line comment
2) /* block comment */
Line comment: discards everything from where the pair of slash signs (//) is found up to the
end of that same line.
Block comment: discards everything between the /* characters and the next appearance of
the */ characters, with the possibility to include several lines.
25
… Cont’d
#include<iostream>
Sentences that begin with a pound/hash sign (#) are directives for the preprocessor.
It tells the preprocessor to include the iostream standard file.
This specific file (iostream) includes the declarations of the basic standard input-
output library in C++
Using namespace std;
❑ All the elements of the standard C++ library are declared within what is called a
namespace, the namespace with the name std.
Classes and functions of the standard libraries are located within the std namespace
we must use the C++ using directive
The header: • would be equivalent to the old expression
#include <iostream.h>
26
… Cont’d
void/int main ()
This line corresponds to the beginning of the main function declaration.
The main function is the point by where all C++ programs begin their execution.
It does not matter whether there are other functions with other names
defined before or after it.
The instructions contained within this function's definition will always be the
first ones to be executed in any C++ program.
Stream Description
cin standard input
cout standard output
cerr standard error –output
clog standard logging- output
67
Cout
cout is used with insertation operator, <<, which is used for
data insertion.
Each cout line produces a line of words on a separate line only
if the end line \n or endl is used.
Otherwise, the lines will be written one after the other on the
same line.
Example:
cout << “Sentence 1”; // prints Sentence 1
cout << 100; // prints 100
cout << x; // prints the value of x
68
… Cont’d
Text is enclosed in double quotes as shown in previous example.
Multiple insertion operations (<<) may be chained in a single
statement.
cout << “This is” << “a” << “a valid statement”;
What cout will not do automatically is add line breaks at the end,
unless instructed to do so.
So if one writes the following lines of code:
cout << “This is line 1.”;
cout << “This is line 2.”;
Then the output will be in a single line without any line breaks in
between as shown below:
This is line 1. This is line 2.
69
… Cont’d
To insert a line break, a new line character should be
included as follows:
cout << “This is line 1.\n”;
cout << “This is line 2.\n”;
Now the output will be:
This is line1.
This is line 2.
76
77
Exercise 3
3. Write the output of the following program.
1. Read an integer number of a fixed length (3
#include<iostream.h>
digits) from the standard input then print its void main()
digits separated by spaces. For example if {
int x=10,y=20,z=30;
364 is the input then the output should be 3 6 4
--x;
or 4 6 3. y=++y;
z=x--;
2. Write a program in C++, which takes radius
cout<<”x= ”<<x<<endl;
from the user and calculate the area and cout<<”y= ”<<y<<endl;
circumference of a circle. cout<<”z= ”<<z<<’\n’;
cout<<x+y;
}
78
… Cont’d
5. What is the output of the following program
4. You can convert temperature from degrees #include<iostream.h>
int main(){
Celsius to degrees Fahrenheit by multiplying int x,y, c=10;
cout<<c++;
by 9/5 and adding 32. Write a program that x=2*c+1;
cout<<'\n'<<x;
allows the user to enter a floating-point y=--x+1;
cout<<'\n'<<y;
number representing degrees Celsius, and
x=c++;
cout<<'\n'<<x;
then displays the corresponding degrees
cout<<'\n'<<c++;
Fahrenheit. x=2*x++;
cout<<'\n'<<x;
cout<<'\n'<<4+x;
return 0;
}