Computer Programming C++ Chapter Three
Computer Programming C++ Chapter Three
Chapter 2
Basics of programming
Algorithm and flow chart
Basics of C++
Variables and assignment
Declaration and initialization
Input /output using console
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
2.1 Programming Basics
3
Objectives
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
…Programming Basics
4
A program
is a list of instructions, together with any fixed information required to
carry out those instructions
The process of programming consists of defining those instructions
and that data
In order to design a program for a computer, you must determine three
basic elements:
The instructions that must be performed.
The order in which those instructions are to be performed.
The fixed data required to perform the instructions.
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Programming Languages
5
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
…Programming Languages
6
It is machine/platform dependent.
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
…Programming Languages
7
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
…Terminologies
9
Linker Error: The most common linker error is an unresolved external reference.
This occurs when a function is called in one module but coded in a different module.
The linker tries to find the function's implementation code, but it isn't there.
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
…Terminologies
10
Sequence
each step or process in the algorithm is executed in the specified order
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
…Features of Algorithms
12
End
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Algorithm Stating Techniques
15
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Flow Chart
16
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Begin
Exercise Input a, b, c
17
Write an algorithm and draw a flow chart to find the solution of Quadratic Equation
Begin
Input a, b, c (coefficients) D = b*b-4*a*c
Determine the Discriminant D = b2-4ac
If D > 0 Yes
D>0
Find the two real roots as
R1 = (-b-sqrt(D))/(2*a) No
R2 = (-b+sqrt(D))/(2*a) R1 = (-b-sqrt(D))/(2*a) Yes
Display both roots (R1 and R2) R1 = (-b+sqrt(D))/(2*a) D=0 R = (-b/(2*a)
Else if D = 0
Display R1 No
Find the root as Display R
Display R2 Print no real
R = -b/(2*a)
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
2.2 Introduction to C++ Programming
19
Objectives
Identify and differentiate the various parts of C++ program
Declare and manipulate variables
Communicate (I/O) with the console using appropriate syntax and
manipulators
State the various operator types
Use operator precedence rules to evaluate expressions
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
2.2.1 Introduction
20
Section Objectives
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Program Development
21
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 1
22
Problem
Printing Hello World! on the screen
output
Hello World!
// My first program in C++
#include<iostream>
Editor using namespace std;
int main()
{
cout<<"Hello World! "; Screen
return 0;
}
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 1 (cont’d)
23
All lines beginning with two slash sign (//) are considered comments
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 1 (cont’d)
24
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 1 (cont’d)
25
The parenthesis following the word main are the distinguishing feature of a
function
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 1 (cont’d)
26
}
The body of the function is surrounded by braces
Every function must have a pair of braces ({ }) around the body of the function
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 1 (cont’d)
27
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 1 (cont’d)
28
Line 7: return 0;
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 2
29
#include<iostream>
using namespace std
int main()
{
cout<< " Hello World! ";
cout<< " I’m a C++ program";
return 0; Notice that the second
} output is immediately
after the first one
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Comments
30
The line comment discards everything from where the pair of slash sign (//) is found
up to the end of the same line
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 3
31
#include<iostream>>
using namespace std
int main()
{
cout<< " Hello World! "; // displays Hello World!
cout<< " I’m a C++ program"; // displays I’m a C++ program
return 0;
}
output
Hello World! I’m a C++ program
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Software Development Environments
32
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
2.2.2 Variables, Data Types, Constants
33
Section Objectives
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Introduction
34
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Introduction (cont’d)
35
This same process can be expressed in C++ with the following set of
instruction:
a = 5;
b = 2;
a = a + 1;
result = a - b;
a , b and result are known as variables
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
What is a variable ?
36
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Identifiers
37
Example
a, b, result, root, no_of_students, _grade
Are all valid C++ identifiers
2students, cout, for
are not valid C++ identifiers
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Identifiers (cont’d)
38
Note
1. Identifiers can not match any language keywords nor compiler specific
ones.
eg. switch, break, for, bool, if, false, class, char, etc…
2. No blank spaces are allowed in identifies
3. The C++ compiler (the language in general) is case sensitive
eg. RESULT, result, Result
are three different identifiers
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Data Types
39
This is because the size of different data types are not the same
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Built in Data Types
40
Character
They can represent a single character such as ‘A’ or ‘$’.
The most basic type is char, which is a one-byte character
Numerical Integer
They exist in a variety of sizes, and can be either signed or unsigned, depending on whether
they support negative values or not
Floating-point
They can represent real values, such as 3.14 or 0.01, with different levels of precision
depending on which of the three floating-point types is used (float, double)
Boolean
The Boolean type, known in C++ as bool, can only represent one of two states, true or false
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Declaration of Variables
42
eg.
int a; // declares a variable a of type int
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Declaration of Variables (cont’d)
43
is the same as
int a;
int b;
int c;
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Declaration of Variables (cont’d)
44
Integer data types (char, short, long and int) can be signed or
unsigned
This is according to the range of numbers that we need to represent.
To specify an integer data type we do it by putting the keyword
signed or unsigned before the data type itself.
Example:
unsigned short numberOfSons;
signed int myAccountBalance;
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Declaration of Variables (cont’d)
45
int myAccountBalance;
// equivalent as: signed int myAccountBalance;
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 4
46
#include<iostream> Output
using namespace std; 6 2 4
int main()
{
int a,b,result; // declaring variables
a = 5;
b = 2; process
a = a+1;
result = a - b;
cout<<a<<" "<<b<<" "<<result<<" "; // print out the result
return 0; // terminate the program
}
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Initialization of Variables
47
For example
int a = 0; or
int a (0);
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Scope of Variables
48
#include<iostream>
using namespace std;
int main()
{
unsigned short age; // local variables
float aNumber, anotherNumber;
return 0;
} Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Scope of Variables (cont’d)
49
Global Variable
Can be referred anywhere in the code, within any function, wherever it is
after its declaration
The global variables will hold their value throughout the lifetime of your
program
Local Variable
The scope of local variables is limited to the code level in which they are
declared
Variables declared inside a function or block are local variables
Only statements that are inside that function or block of code can use them
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Constants
50
Can be
Integers
Floating-Point Numbers
Characters
Strings
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Integers
51
Octal (base 8)
eg. 032, 067, -042
#include<iostream> Integers
using namespace std;
int main()
75
{
// integers
75
cout<<"Integers\n\n"; 75
//Decimal
cout<<75<<"\n";
// octal
cout<<0113<<"\n";
// hexadecimal
cout<<0x4b<<"\n”
return 0;
}
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Floating Point Numbers
53
eg.
3.14159 // 3.14159 (PI)
3.0 // 3.0
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Characters and strings
54
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Escape Codes
55
\n newline
\r carriage return
\t tabualtion
\v vertical tabualtion
\b back space
\f page feed
\a alert (beep)
\’ single Quote
\” double Quote
\\ inverted slash
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Escape Codes (cont’d)
56
For example:
2. "string expressed in \
two lines"
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Defined constants (using #define)
57
You can define your own names by using the #define preprocessor directive.
The syntax is
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Declared Constants (const)
58
With the const prefix you can declare constants with a specific type
exactly as you would do with a variable:
In case that the type was not specified (as in the last example) the
compiler assumes that it is type int.
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
2.2.3 Communication through console.
59
Section objectives
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Introduction
60
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Output (cout)
61
For Example
cout << "Hello"; // prints Hello on screen
cout << Hello; // prints the content of
// Hello variable on screen
The insertion operator (<<) may be used more than once in a same
sentence:
For Example
cout << "Hello, " << "I am " << "a C++ sentence";
cout << "Hello, I am " << age << " years old and my\ zipcode is " <<
zipcode;
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Input (cin)
63
For example:
int age;
cin >> age;
Declares the variable age as an int
Then waits for an input from cin (keyborad) in order to store it in this
integer variable.
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Input (cin)…
64
You can also use cin to request more than one datum input from the
user:
cin >> a >> b;
is equivalent to:
cin >> a;
cin >> b;
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 6
65
// input/output example
#include<iostream>
using namespace std;
int main()
{
int i;
cout<<" Enter an integer value ";
cin>>i;
cout<<" The Value you entered is " <<i;
cout<<" and its double is "<<i*2<<endl;
return 0;
}
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
I/O Manipulators
66
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Some I/O Manipulaltors
67
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 7
68
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
const float tenth = 0.1;
const float one = 1.0;
const float big = 1234567890.0;
cout << “\n\n\tA. "<< tenth << ", " << one << ", " << big << endl;
cout << “\tB. " <<fixed<<setprecision(3)<<tenth<<", “<<one<<",“
<< big << endl;
cout << “\tC. " << setprecision(20) << tenth << endl;
cout << “\tD. " << setw(8) << setfill('*') << 34 << 45 << endl;
cout << “\tE. " << setw(8) << 34 << setw(8) << 45 << endl;
return 0;
}
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Example 8: cin, cout, getline
69
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string x;
cout << “your name please ”;
getline (cin, x);
cout << “Hello” << x << “\n”;
return 0;
}
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
2.3.4 Operators
70
Section objective
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Introduction
71
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Assignment operator (‘=’)
72
It is the equal sign (assignment operator) that makes the value on the
right be assigned to the variable on the left
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Assignment operator (‘=’)…
73
Note :
l-value must always be a variable
5 = a; is an error
r-value can be either
Constant
Variable
The result of operation or
Combination of them
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Assignment operator (‘=’)…
74
Example
a = 5;
b = a;
c = a + b;
d = a + b * c +5;
Assignment only takes place from right to left never reverse
Example
What will be the values of a, b , and c after execution of the following
statements?
int a = 5, b = 3, c = 4; All the values of
a = b = c; a , b and c will
be 4
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Arithmetic Operators
75
Example
mean = (num1 + num2) / 2; // arithmetic mean of two numbers
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Module Operator (%)
76
Remainder operator
Works only on integers
It finds the remainder when a number is divided by another
Example
11 % 3 = 2 i.e 11/3 =3 and remainder is 2
int a = 13, b = 4;
int c = a % b; 1
cout<<c;
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Bitwise Operators
77
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Compound Assignment Operators
79
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Compound Assignment Operators (cont’d)
81
Example
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Increment and Decrement Operator
82
Example
int count = 3;
count = count + 1; //same as count += 1;
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Increment and Decrement Operator (cont’d)
84
Example
int a, b = 3;
a = ++b; //assign after incrementing
cout<<a<<“ ”<<b<<“ ”;
output 4 4
int a, b = 3;
a = b++; // increment after assignment
cout<<a<<“ ”<<b<<“ ”;
output 3 4
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Increment and Decrement Operator (cont’d)
85
Note
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Relational Operators (==, !=, >, <, >=, <=)
86
Example
int a = 2, b = 3, c = 6;
The result of ((b+4) > a*c) is false (0)
The result of ((b=2) ==a) is true(1)
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Logical Operators (!, &&, ||)
87
C++ has two binary logical operators (“or”, “and”) and one unary
logical operator (“not”)
Logical and (&&)
condition1 && condition2 is true if both the conditions are true, false
otherwise
Example
int a = 2, b = 3, c = 6;
The result of (a*b == c && 4 < 6) is true (1)
The result of (b+4 < c && b == 3) is false (0)
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Logical or (||)
88
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Logical not (!)
89
Example
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Conditional Operator (?:)
90
Syntax
Condition?result1:result2
If condition is true result1 will be executed result2 otherwise
Example
c = 5>3?a:b; // a will be returned i.e c = a
c = 7==5?4:3;// 3 will be returned i.e c = 3
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Explicit Type Casting operators
91
int i;
float f = 3.14;
i = (int) f; // or i = int (f)
i will be 3
sizeof() : accepts one parameter that can be either a variable type or variable itself
and returns size in bytes
Example 1 4 2
int a = sizeof(char);
int b = sizeof(double);
int c = sizeof(int)
cout <<a<<“ ” <<b<<“ ”<<c<<“ ”;
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Operator Precedence Table
92
Develop an algorithm, flowchart and write C++ program for the following problems
4. That converts temperature in degrees Fahrenheit to Celsius, and displays the result.
5. That converts temperature in degrees Celsius to Fahrenheit t, and displays the result
Haramaya University, HiT, School of Electrical and Computer Saturday, June 8, 2024
Engineering
Exercises…
96