0% found this document useful (0 votes)
10 views61 pages

Programming in C++ (I) 2020-2021

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)
10 views61 pages

Programming in C++ (I) 2020-2021

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/ 61

‫)‪Programming in C++(I‬‬

‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec1 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 1

Array (One-dimensional array or 1D array)


An array is a collection of a fixed number of components all of the
same data type. A one-dimensional array is an array in which the
components are arranged in a list form. Individual elements are
referred to using common name and unique index of the elements.
The array itself is given name and its elements are referred to by
their subscripts. In C++, an array is denoted as follows:

The general form for declaring a one-dimensional array is:

[ ] array subscriber
int num[5+3]; Declaration statement :[Comment [DS21
In memory declares an array with name num of
eight elements of integer type.
int num[8];
Another declaration :[Comment [DS22
statement.

in which intExp is any constant expression that evaluates to a positive


integer. Also, intExp specifies the number of components in the
array.

For example, the statement:


int num[5]; An array with name num of :[Comment [DS23
five element, each is of type integer.

declares an array num of five components. Each component is of type


int. The components are num[0], num[1], num[2], num[3], and
num[4]. Figure below illustrates the array num.
Lec1 Programming in C++ (I) Sura Abed Sarab Hussien

num[0] :[Comment [DS24


num: the name of the array.
0: index of the first element.

num[3]: where 3 is the index of the fourth element.

Accessing Array Components


The general form (syntax) used for accessing an array component is:

in which indexExp, called the index, is any expression whose value is


a nonnegative integer and should be ranged from 0 to intExp – 1 (i.e.
from 0 to the number of components in the array minus one).
The index value specifies the position of the component in the array.
In C++, [ ] is an operator called the array subscripting operator.
Moreover, in C++, the array index starts at 0.

Consider the following statement:


int list[10];

This statement declares an array list of 10 components. The


components are
Lec1 Programming in C++ (I) Sura Abed Sarab Hussien

list[0], list[1], . . ., list[9]. In other words, we have declared 10


variables
(as shown in the following figure).
int list[10];

The assignment statement:


list[5] = 34;
stores (i.e. assigns) 34 in list[5], which is the sixth component of the
array list:

Suppose i is an int variable. Then, the assignment statement:


list[3] = 63;
is equivalent to the assignment statements:
i = 3;
list[i] = 63;
If i is 4, then the assignment statement:
Lec1 Programming in C++ (I) Sura Abed Sarab Hussien

int i = 4;
list[2 * i - 3] = 58;
list[5] = 58;
stores 58 in list[5] because 2 * i - 3 evaluates to 5. The index expression
is evaluated first, giving the position of the component in the array.

Next, consider the following statements:


list[3] = 10;
list[6] = 35;
list[5] = list[3] + list[6];
9
The first statement stores 10 in list[3], the second statement stores 35 in
list[6], and the third statement adds the contents of list[3] and list[6] and
stores the result in list[5] (see Figure below).
‫)‪Programming in C++(I‬‬
‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 2

Processing One-Dimensional Arrays


Some of the basic operations performed on a one-dimensional array
are initializing, inputting data, outputting data stored in an array,
and finding the largest and/or smallest element. Moreover, if the data
is numeric, some other basic operations are finding the sum and
average of the elements of the array. Each of these operations
requires the ability to step through the elements of the array. This is
easily accomplished using a loop. For example, suppose that we have
the following statements:

int list[100]; //list is an array of size 100


int i;

The following for loop steps through each element of the array list,
starting at the first element of list:
for (i = 0; i < 100; i++) //Line 1
//process list[i] //Line 2

main()
{
int a[100]; Declaration statement. :[Comment [DS21
It declares an array with 100 elements.
Each element is of type integer.
cin >> a[0]; The name of the array is a.

cin >> a[1];


cin >> a[2];
.
.
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien

.
cin >> a[99];
}

main()
{
int a[100]; Declaration statement. :[Comment [DS22
It declares an array with 100 elements.
Each element is of type integer.
for(int i = 0; i < 100; i++) The name of the array is a.

{
cout << "please, enter an integer number" << endl;
cin >> a[i];
}
}

main()
{
int a[100]; Declaration statement. :[Comment [DS23
It declares an array with 100 elements.
Each element is of type integer.
for(int i = 0; i < 100; i++) The name of the array is a.

cin >> a[i];


}

main()
{
int a[100];
for(int i = 0; i <= 99; i++)
cin >> a[i];
}

main()
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien

{
int a[100];
for(int i = 99; i >= 0; i--)
cin >> a[i];
}

main()
{
int a[100];
for(int i = 0; i < 100; i++)
cin >> a[i]; Loop for reading (reading :[Comment [DS24
loop)

for(int i = 0; i < 100; i++)


cout << a[i] << endl;
for(int i = 99; i >= 0; i--)
cout << a[i] << endl;
for(int i = 0; i < 100; i= i+2)
cout << a[i] << endl;
}
main()
{
const int n = 100;
int a[n];
for(int i = 0; i < n; i++)
cin >> a[i]; Loop for reading (reading :[Comment [DS25
loop)

for(int i = 0; i < n; i++)


cout << a[i] << endl;
for(int i = n-1; i >= 0; i--)
cout << a[i] << endl;
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien

for(int i = 0; i < n; i= i+2)


cout << a[i] << endl;
for(int i = 0; i < n; i= i++)
if(a[i]%2 == 1)
cout << a[i] << endl;

int sum = 0;
for(int i = 0; i < n; i++)
sum = sum + a[i];
cout << sum;
int sum_e = 0;
int c_e = 0;
for(int i = 0; i < n; i++)
if(a[i]%2 == 0){
c_e++;
sum_e = sum_e + a[i];
}
cout << sum_e << endl;
cout << sum_e / c_e << endl;
int max = a[0];
for(int i = 1; i < n; i++)
if(a[i] > max)
max = a[i];
cout << max << endl;
int max = -9999;
for(int i = 0; i < n; i++)
if(a[i]%2 == 0 && a[i] > max)
max = a[i];
cout << max << endl;
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien

If processing the list requires inputting data into list, the statement in
Line 2 takes the form of an input statement, such as the cin
statement. For example, the
following statements read 100 numbers from the keyboard and store
the numbers in list:

main()
{
int list[100];
int i;
for (i = 0; i < 100; i++)
cin >> list[i];
}

Initialize One Dimensional Array in C++


Here is an example, declaring and initializing values to the array
name arr of type int, containing 10 elements

int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};


Lec2 Programming in C++ (I) Sura Abed Sarab Hussien

C++ One Dimensional Array Example

Here are some example program, demonstrating one dimensional


array in C++
/* C++ One Dimensional Array */

#include<iostream.h>
#include<conio.h>
void main()
{
int arr[5] = {1, 2, 3, 4, 5};
int i;
for(i=0; i<5; i++)
{
cout<< arr[i] << endl;
}
getch();
}
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien

Here is another C++ example, also demonstrating one dimension


array in C++
/* C++ One Dimensional Array */

#include<iostream.h>
#include<conio.h>
void main()
{
int arr[10];
int i;
int sum=0, avg=0;
cout <<"Enter 10 array elements: ";
for(i=0; i < 10; i++)
{
cin >> arr[i];
sum = sum + arr[i];
}
cout<<"\nThe array elements are: \n";
for(i=0; i < 10; i++)
{
cout << arr[i] <<" ";
}
cout<<"\n\nSum of all elements is: " << sum << endl;
avg = sum/10;
cout<<"And average is: "<<avg;
}
Here is the sample run of the above C++ program:
Lec2 Programming in C++ (I) Sura Abed Sarab Hussien
‫)‪Programming in C++(I‬‬
‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec3 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 3

Two dimensional arrays (Matrices)


2D Array = Matrix
Two-dimensional array: A collection of a fixed number of components arranged in
rows and columns (that is, in two dimensions), wherein all components are of the
same type. The syntax for declaring a two-dimensional array is:

wherein intExp1 and intExp2 are constant expressions yielding positive integer
values. The two expressions, intExp1 and intExp2, specify the number of rows and
the number of columns, respectively, in the array.
The statement:
double sales[10][5];
declares a two-dimensional array sales of 10 rows and 5 columns, in which every
component is of type double. As in the case of a one-dimensional array, the rows are
numbered 0. . .9 and the columns are numbered 0. . .4:

To access the components of a two-dimensional array, you need a pair of indices: one
for the row position and one for the column position.
Lec3 Programming in C++ (I) Sura Abed Sarab Hussien

1) Two-Dimensional Array Initialization During Declaration


Like one-dimensional arrays, two-dimensional arrays can be initialized when they are
declared. The following example helps illustrate this concept. Consider the following
statement:
int board[4][3] = {{2, 3, 1}, Initialization of the first row :[Comment [DS21

{15, 25, 13}, Initialization of the 2nd row :[Comment [DS22

{20, 4, 7},
{11, 18, 14}}; Initialization of the 4th row :[Comment [DS23

This statement declares board to be a two-dimensional array of four rows and three
columns. The components of the first row are 2, 3, and 1; the components of the
second row are 15, 25, and 13; the components of the third row are 20, 4, and 7; and
the components of the fourth row are 11, 18, and 14, respectively.
Lec3 Programming in C++ (I) Sura Abed Sarab Hussien

2) Two-Dimensional Array Assignment Through cin Function


For example, we have 3 students, each has 3 marks. Declare a 2D array and read
the marks for each student using cin function.
main(){
const int n = 3;
int marks[n][n];
int i, j;
for(i=0; i < n; i++) Outer loop for the rows of :[Comment [DS24
the matrix
for(j=0; j < n; j++){ Loop control variable :[Comment [DS25

cout<<”please, enter a mark for student” << i <<endl; Inner loop for the columns of :[Comment [DS26
the matrix
cin>> marks[i][j]; You need a nested loop to :[Comment [DS27
visit all the rows and all the columns in the matrix.
} // end for j
LCV :[Comment [DS28
} // end main This LCV points to the rows :[Comment [DS29
of the matrix marks
In memory:
This LCV points to the :[Comment [DS210
i=0 1 2 3 columns of the matrix marks

j=0 1 2 3 0 1 2 3 0 1 2 3
marks
marks[0][0]=90 marks[0][1]=85 marks[0][2]=95
marks[1][0]=60 marks[1][1]=85 marks[1][2]=70
marks[2][0]=93 marks[2][1]=90 marks[2][2]=80

please, enter a mark for student 0


90
please, enter a mark for student 0
85
please, enter a mark for student 0
95
please, enter a mark for student 1
60
please, enter a mark for student 1
85
please, enter a mark for student 1
70
please, enter a mark for student 2
93
please, enter a mark for student 2
90
please, enter a mark for student 2
Lec3 Programming in C++ (I) Sura Abed Sarab Hussien

80
‫)‪Programming in C++(I‬‬
‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 4
Questions:
Write a C++ program to read a 2D array of 𝒏 × 𝒏 integer numbers. Then, find
the following:
1. The sum of all even elements.
2. The largest element in the matrix.
3. The smallest element in each row.
4. The sum of the smallest odd element in each column.
5. Print the elements of the main diagonal.
6. Print the elements of the minor (secondary) diagonal.
7. Swap between the elements of the main diagonal and the minor
diagonal.
8. Print the elements of the left triangle of the main diagonal.
9. The sum of the elements of the right triangle of the main diagonal.
10. The sum of the elements of the right triangle of the minor diagonal.
11. Print even elements of odd rows.
12. Print odd elements located at the even columns.
13. Print the index of the largest even number in the matrix.
14. Copy the factorial of each element in a second matrix. What is the size
of this matrix?
15. Print the elements of each row in a distinct line.
16. Print the elements of each column in a distinct line.

main(){
const int n = 5;
int a[n][n];
int i,j;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
cin>> a[i][j]; For reading the elements of :[Comment [DS21
the matrix. Visiting the elements row by row, and
// Answer for (1) column followed by column.
int sum = 0;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(a[i][j]%2 == 0)
sum = sum + a[i][j];// sum += a[i][j];
cout<< sum<<endl;
// Answer for (2)
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien

int Large = -9999999;


for(i=0; i<n; i++){
for(j=0; j<n; j++){
if(a[i][j] > Large){
Large = a[i][j];
cout<< Large;
}
cout<< Large;
}
cout<< Large;
}
cout<< Large;
// Answer for (3)
int small;
for(i=0; i< n; i++){
small = +9999999;
for(j=0; j<n; j++)
if(a[i][j] < small)
small = a[i][j];
cout<< small << “in row”<< i << endl;
}
// Answer for (4)
int sum_odd = 0;
int small_odd;
for(i=0; i<n; i++){ Visiting all columns. Column :[Comment [DS22
followed by the next column.
small_odd = +9999999;
for(j=0; j<n; j++) Re-set the value of :[Comment [DS23
small_odd for each new column
if(a[j][i]%2 == 1 && a[j][i] < small_odd)
small_odd = a[j][i]; Visiting all rows for the :[Comment [DS24
current column and check for the smallest odd
sum_odd += small_odd; number
}
In each column, do 3 :[Comment [DS25
statements
cout<< sum_odd; Perform sum for the smallest :[Comment [DS26
odd number in that column
// answer for 5
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(i == j)
cout<<a[i][j]<<endl; When visiting all the :[Comment [DS27
elements of the matrix and check for the main
// or you can solve it in another way diagonal
for(i=0; i<n; i++)
cout<< a[i][i]<<endl; Visiting only the elements of :[Comment [DS28
the main diagonal
//answer for 6
//first method: visiting all elements and check for secondary diagonal
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(i+j == n-1)
cout<< a[i][j]<< endl;
//second method: visiting only elements located at the secondary diagonal
for(i=0; i<n; i++)
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien

cout<< a[i][n-1 -i] <<endl;

//third method: visiting only elements located at the secondary diagonal


for(i=0; i<n;i++)
cout<<a[n-1-i][i]<<endl;
//answer for 7
int temp;
//method 1
for(i=0; i<n; i++){
temp = a[i][i];
a[i][i] = a[i][n-1-i];
a[i][n-1-i] = temp;
}
//method 2
for(i=0; i<n; i++){
temp = a[n-1-i][i];
a[n-1-i][i] = a[i][i];
a[i][i] = temp; Two methods for visiting :[Comment [DS29
only the elements of the diagonals.
}
//method 3
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(i == j){
temp = a[i][j];
a[i][i] = a[i][j];
a[i][j] = temp;
} Visiting all the elements of :[Comment [DS210
the matrix and check for the main diagonal
//method 4
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(j == n-1 –i){
temp = a[i][j];
a[i][j] = a[i][i];
a[i][i] = temp;
} Visiting all elemnts and :[Comment [DS211
check for the secondary diagonal.
//answer for 8
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(i >= j)
cout<< a[i][j]<<endl;
//answer for 9
int sum_rt = 0; // sum of the right triangle of the main diagonal
for(i=0; i<n; i++)
for(j=0; j<n; j++)
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien

if(i <= j)
sum_rt + = a[i][j];
cout<< sum_rt << endl;

//answer for 10
int sum_rs = 0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if( j >= n-1-i)
sum_rs + = a[i][j];
cout<< sum_rs << endl;
//answer for 11
//method 1: visiting all rows and check for odd row
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(i%2 == 1 && a[i][j]%2 == 0)
cout<< a[i][j];

//method 2: visiting only odd rows


for(i=1; i<n= i=i+2)
for(j=0;j<n;j++)
if(a[i][j]%2 == 0)
cout<< a[i][j];
// answer for 12
//method 1
for(i=0; i<n; i++) Outer loop :[Comment [DS212
for(j=0; j<n; j=j+2) Inner loop :[Comment [DS213
if(a[i][j]%2 == 1) Index i is controlled by the :[Comment [DS214
cout<< a[i][j]<<endl; outer loop,
While index j is controlled by the inner loop.
//method 2 This means that we have to visit all row, where in
for(i=0; i<n; i=i+2) each row, visit all its columns first then continuing
for(j=0; j<n; j++) to the next row and so on.

if(a[j][i]%2 == 1) Outer loop :[Comment [DS215


cout<< a[j][i]<<endl; Inner loop :[Comment [DS216
//method 3 Index j is controlled by the :[Comment [DS217
for(i=0; i<n; i++) outer loop,
While index i is controlled by the inner loop.
for(j=0; j<n; j++) This means that we have to visit all even columns,
if( j%2 == 0 && a[i][j]%2 == 1) where in each even column, visit all its rows first
then continuing to the next even column and so on.
cout<< a[i][j];
//answer for 13
int large_even = -99999999;
int i_index, j_index;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
if(a[i][j]%2 == 0 && a[i][j] > large_even){
large_even = a[i][j];
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien

i_index = i;
j_index = j;
}
cout<< “the row index of the largest even is:” << i_index<<end;
cout<< “the column index of the largest even is:” << j_index<<end;
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien

// answer for 14
int a_fact[n][n];
int fact, k;
for(i=0; i<n; i++)
for(j=0; j<n; j++){
fact = 1;
for(k=1; k<= a[i][j]; k++)
fact = fact * k;
a_fact[i][j] = fact;
cout<< a_fact[i][j];
}
for(i=0; i<n; i++)
for(j=0; j<n; j++)
cout<< a_fact[i][j]<<endln;

//answer for 15
for(i=0; i<n; i++){
for(j=0; j<n; j++)
cout<<a[i][j] << “ “;

cout<<endl;
}
//answer for 16
for(i=0; i<n; i++){
for(j=0; j<n; j++){
cout<<a[j][i] << “ “;
} First statement :[Comment [DS218
cout<<endl; Second statement :[Comment [DS219
}
}//end of main

i=0 1 2
S S

j=0 1 2 3 4 5 0 1 2 3 4
S S 5

a[0][0] a[1][0] a[2][0] a[3][0] a[4][0]

a[0][1] a[1][1] a[2][1] a[3][1] a[4][1]

a[0][3] a[1][3] a[2][3] a[3][3] a[4][3]


Lec4 Programming in C++ (I) Sura Abed Sarab Hussien

[0][0] [0][1] [0][2] Row index :[Comment [DS220


index :[Comment [DS221
[1][0] [1][1] [1][2]
located at the main :[Comment [DS222
[2][0] [2][1] [2][2] diagonal
column index :[Comment [DS223
this cell is located in the 1st :[Comment [DS224
row and the second column
located at the minor :[Comment [DS225
diagonal
located at the main :[Comment [DS226
diagonal
located at the minor :[Comment [DS227
diagonal
[0][0] [0][1] [0][2] [0][3] [0][4] located at the minor :[Comment [DS228
diagonal
[1][0] [1][1] [1][2] [1][3] [1][4]
indexing method :[Comment [DS229
[2][0] [2][1] [2][2] [2][3] [2][4] located at the main :[Comment [DS230
diagonal
[3][0] [3][1] [3][2] [3][3] [3][4]
all elements in yellow are :[Comment [DS231
[4][0] [4][1] [4][2] [4][3] [4][4] located in the left triangle of the main diagonal.

[0][0] [0][1] [0][2] [0][3] [0][4] All elements in cyan color :[Comment [DS232
are located at the right triangle of the main diagonal
[1][0] [1][1] [1][2] [1][3] [1][4]
[2][0] [2][1] [2][2] [2][3] [2][4]
[3][0] [3][1] [3][2] [3][3] [3][4]
[4][0] [4][1] [4][2] [4][3] [4][4]

[0][0] [0][1] [0][2] [0][3] [0][4]


[1][0] [1][1] [1][2] [1][3] [1][4]
[2][0] [2][1] [2][2] [2][3] [2][4]
[3][0] [3][1] [3][2] [3][3] [3][4]
[4][0] [4][1] [4][2] [4][3] [4][4]
i+j = n-1
j = n -1 -i
j >= n – 1 – i
3 ?>= n – 1 – 0 --- 3 ?>= 4 false
Lec4 Programming in C++ (I) Sura Abed Sarab Hussien

Sum of the Top Triangle

[0][0] [0][1] [0][2] [0][3] [0][4] [0][5] The indices of the elements :[Comment [DS233
of the main diagonal
i=j
[1][1] [1][2] [1][3] [1][4] j>=i && i+j <= n-1 :[Comment [DS234
The indices of the elements :[Comment [DS235
of the main diagonal
[2][2] [2][3] i+j=n-1

j>=i && i+j <= n-1 :[Comment [DS236


[3][2] [3][3]
The indices of the elements :[Comment [DS237
of the main diagonal
[4][1] [4][4] i=j

[5][0] [5][5] The indices of the elements :[Comment [DS238


of the main diagonal
i+j=n-1
S
j>=i && i+j <= n-1 :[Comment [DS239

The indices of the elements :[Comment [DS240


of the main diagonal
i=j

The indices of the elements :[Comment [DS241


of the main diagonal
i+j=n-1
The indices of the elements :[Comment [DS242
of the main diagonal
i+j=n-1
The indices of the elements :[Comment [DS243
of the main diagonal
i=j

The indices of the elements :[Comment [DS244


of the main diagonal
i+j=n-1
The indices of the elements :[Comment [DS245
of the main diagonal
i=j

The indices of the elements :[Comment [DS246


of the main diagonal
i+j=n-1
The indices of the elements :[Comment [DS247
of the main diagonal
i=j
‫)‪Programming in C++(I‬‬
‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec.5 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 5

Write a C++ program to read a 1D array of integer


elements. Then, find the output for each of the
following:

1. Print odd numbers only.


2. Print even numbers that are greater than 10. 3. Print
the location of the largest odd number. 4. Print the
location of the smallest even number. 5. Swap between
the locations of the largest odd number and the smallest
even number.
6. Find the sum of the even numbers.
7. Find the factorial of the first odd number occurs in the
array.
8. Find the second smallest number in the array.
9. Print the elements of the array in the reverse order.
10. Reverse the order of the elements in the array.

Testing: tracing
x[0]=9 x[1]=2 x[2]=19 x[3]=1 x[4]=30 x[5]=6 x[6]=99
x[7]=22 x[8]=33 x[9]=44
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9

Answer of question (5)


x[0]=9 x[1]=99 x[2]=19 x[3]=1 x[4]=30 x[5]=6 x[6]=2
x[7]=22 x[8]=33 x[9]=44
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9
Lec.5 Programming in C++ (I) Sura Abed Sarab Hussien

main( )
{
const int n = 10;
int x[n];
int i;
int large_odd = -99999;
int index_large_odd = -1;
int small_even = 10000000;
int index_small_even = -1;
for(i = 0; i < n; i++){ // i is the LCV of the first loop
cout << "please, enter an integer number" << endl;
cin >> x[i]; // index of the array x
}
for(i = 0; i < n; i++)// i is the LCV of the second loop

(question 1)
if(x[i]%2 == 1)
cout << x[i] << endl;
for(i = 0; i < n; i++)

// (question 2)
if(x[i]%2 == 0 && x[i] > 10)
cout << x[i] << endl;
for(i = 0; i < n; i++)

// (question 3)
if(x[i] %2 == 1 && x[i] > large_odd){
large_odd = x[i];
index_large_odd = i;
Lec.5 Programming in C++ (I) Sura Abed Sarab Hussien

}
Lec.5 Programming in C++ (I) Sura Abed Sarab Hussien
cout << index_large_odd << endl;
for(i = 0; i < n; i++) // (question 4)
if(x[i] %2 == 0 && x[i] < small_even){
small_even = x[i];
index_small_even = i;
}c
out << index_small_even << endl;

// question 5
x[index_small_even] = large_odd;
x[index_large_odd] = small_even;
}
‫)‪Programming in C++(I‬‬
‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec.6 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 6

Keywords in this lesson:

• Editor
• main function
• begin
• end
• variable
• assignement operator
• constant
• integer
• mathematical expression
• semicolon
• output operator
• string
• endl
Lec.6 Programming in C++ (I) Sura Abed Sarab Hussien

Processing steps for a C++ program.

The following steps, as shown in the above figure are necessary to process a C++

program.

1. You use a text editor to create a C++ program following the rules, or syntax, of the

high-level language. This program is called the source code, or source program. The

program must be saved in a text file that has the extension .cpp.

2. The C++ program contains the statement #include <iostream>. In a C++ program,

statements that begin with the symbol # are called preprocessor directives. These

statements are processed by a program called preprocessor.

3. After processing preprocessor directives, the next step is to verify that the program

obeys the rules of the programming language — that is, the program is syntactically

correct—and translate the program into the equivalent machine language. The

compiler checks the source program for syntax errors and, if no error is found,

translates the program into the equivalent machine language. The equivalent machine

language program is called an object program.

4. The programs that you write in a high-level language are developed using an

integrated development environment (IDE). The IDE contains many programs that are

useful in creating your program. This prewritten code (program) resides in a place

called the library. A program called a linker combines the object program with the

programs from libraries. Linker: A program that combines the object program with

other programs in the library and is used in the program to create the executable code.

5. You must next load the executable program into main memory for execution. A

program called a loader accomplishes this task. Loader: A program that loads an

executable program into main memory.


Lec.6 Programming in C++ (I) Sura Abed Sarab Hussien

6. The final step is to execute the program.

#include <iostream>
using namespace std;

int main() {

Main function of the :[Comment [DS21


program
int main() {
begin :[Comment [DS22

body of the program :[Comment [DS23

} end :[Comment [DS24

int main() {

return 0;
}

int main() {
cout << "This is a C++ Programming"; Output screen :[Comment [DS25

return 0; Output operator :[Comment [DS26

} string :[Comment [DS27


Output statement :[Comment [DS28
End of statement :[Comment [DS29

#include <iostream>
using namespace std;

int main() {
cout << "This is a C++ Programming lesson"<< endl <<"This is Output operator :[Comment [DS210

another not attached statement"; 1st output operator :[Comment [DS211

return 0; Output operator :[Comment [DS212


} 2nd output operator :[Comment [DS213
Output statement :[Comment [DS214
Lec.6 Programming in C++ (I) Sura Abed Sarab Hussien

Output

This is a C++ Programming lesson


This is another not attached statement

#include <iostream>
using namespace std;

int main() {
cout << "This is a C++ Programming"<< endl <<"This is the second Output operator :[Comment [DS215

attached statement"; 1st output operator :[Comment [DS216

return 0; Output operator :[Comment [DS217


} 2nd output operator :[Comment [DS218
Output operator :[Comment [DS219
3rd output operator :[Comment [DS220
Output statement :[Comment [DS221
‫)‪Programming in C++(I‬‬
‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec.7 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 7

This is a C++ Programming

This is the second attached statement

#include <iostream>
using namespace std;

int main() {
int x; integer :[Comment [DS21

x = 70; declaration statement :[Comment [DS22

double y = 256.783; declaration statement :[Comment [DS23


char ch = 'A'; assignment operator :[Comment [DS24
y = 0.1; assignment statement :[Comment [DS25
ch = 'z'; assignment statement :[Comment [DS26
cout << x << endl; real type :[Comment [DS27
cout << y << endl; declaration and assignment :[Comment [DS28
cout << "character: " << ch << endl; statement

return 0; declaration statement :[Comment [DS29

} output statement :[Comment [DS210

Output

70
256.783
character: z

Notes:
• The endl manipulator is used to insert a new line. That's why each output is displayed
in a new line.
• The << operator can be used more than once if we want to print different variables,
strings and so on in a single statement. For example:

cout << "character: " << ch << endl;


Lec.7 Programming in C++ (I) Sura Abed Sarab Hussien

#include <iostream>
using namespace std;
int main()
{
int num;
num = 6;
cout << "My first C++ program." << endl;
cout << "The sum of 2 and 3 = " << 5 << endl;
cout << "7 + 8 = " << 7 + 8 << endl;
cout << "Num = " << num << endl;
return 0;
}
Output

My first C++ program.


The sum of 2 and 3 = 5
7 + 8 = 15
Num = 6

Tips:

When there is no two quotations (also called double quotes denoted by the symbol"),
the output should be either:
a number,
a character,
a mathematical expression, or
a declared variable.
Anything in double quotes is a string.
For example, cout << "The sum of 2 and 3 = " << 5 << endl;
This output statement consists of two expressions. The first expression (after the first
<<) is "The sum of 2 and 3 = " and the second expression (after the second <<)
consists of the number 5. The expression "The sum of 2 and 3 = " is a string and
Lec.7 Programming in C++ (I) Sura Abed Sarab Hussien

evaluates to itself. (Notice the space after =.) The second expression, which consists
of the number 5 evaluates to 5. Thus, the output of the preceding statement is: The
sum of 2 and 3 = 5
Another example, "My first C++ program." and "7 + 8 = " are strings. Typically, a
string evaluates to itself.
Arithmetic expressions are evaluated according to rules of arithmetic operations,
which you typically learn in an algebra course.
Let us now consider the following statement.
cout << "7 + 8 = " << 7 + 8 << endl;
In this output statement, the expression "7 + 8 = ", which is a string, evaluates to
itself.
Let us consider the second expression, 7 + 8. This expression consists of the numbers
7 and 8 and the C++ arithmetic operator +. Therefore, the result of the expression 7 +
8 is the sum of 7 and 8, which is 15. Thus, the output of the preceding statement is:
7 + 8 = 15
Finally, consider the statement:
cout << "Num = " << num << endl;
This statement consists of the string "Num = ", which evaluates to itself, and the word
num. The statement num = 6; assigns the value 6 to num. Therefore, the expression
num, after the second <<, evaluates to 6. It now follows that the output of the previous
statement is:
Num = 6
‫)‪Programming in C++(I‬‬
‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec.8 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 8

Keywords in this lesson:

• Input statement
• Input operator
• Mathematical expression
• Arithmetic operators
• Operator precedence

#include <iostream>
using namespace std;
int main()
{
int num; Declaration statement :[Comment [DS21

num = 6; //num ----- 6 Assignment operator :[Comment [DS22

num = 6 * 2; //num ------ 12 Assignment statement :[Comment [DS23

cout << "My first C++ program." << endl; Output operator :[Comment [DS24

cout << "The sum of 2 and 3 = " << 5 << endl; Output operator :[Comment [DS25

cout << "7 + 8 = " << 7+8 << endl; Output state,ment :[Comment [DS26

cout << "Num = " << num - 2 << endl; Constant: integer :[Comment [DS27
keyword :[Comment [DS28
return 0;
mathematical expression :[Comment [DS29
}
variable :[Comment [DS210
Body of the program (body :[Comment [DS211
of the main function)
Output
20T

My first C++ program.


The sum of 2 and 3 = 5
7 + 8 = 15
Num = 10

#include <iostream>
using namespace std;
int main()
{
Mathematical expression :[Comment [DS212
Int num = 10 +2;
Declaration statement? :[Comment [DS213
cout << "My 2nd C++ program."<<endl; Assignment statement?
cout << endl<<"Result is: num = " << num *2 << endl; Assignment and declaration statement?
Declaration and assignment statement?
return 0; Output statement?
}
Mathematical expression :[Comment [DS214
Lec.8 Programming in C++ (I) Sura Abed Sarab Hussien

Output
My 2nd C++ program.

Result is: num = 24

C++ Input
In C++, cin takes formatted input from standard input devices such as the keyboard.
We use the cin along with the >> operator for taking input. The syntax of an input
statement using cin and the extraction operator >> is:

cin >> variable >> variable...;

#include <iostream>
using namespace std;

int main() {
int x1;
cout << "Enter an integer: ";
cin >> x1; Input operator :[Comment [DS215

cout << "The number is: " << x1; Input statement :[Comment [DS216

return 0; X1 = 32 :[Comment [DS217


}

Output

Enter an integer: 15

The number is: 15


Lec.8 Programming in C++ (I) Sura Abed Sarab Hussien

#include <iostream>
using namespace std;

int main() {
int x1;
cout << "Enter an integer: ";
cin >> x1;
cout << "The number is: " << x1;
return 0;
}

C++ Taking Multiple Inputs

#include <iostream>
using namespace std;

int main() {
char a;
int num;

cout << "Enter a character and an integer: ";


cin >> a >> num;

cout << "Character: " << a << endl;


cout << "Number: " << num;

return 0;
}

Output

Enter a character and an integer: F


23
Character: F
Number: 23
Lec.8 Programming in C++ (I) Sura Abed Sarab Hussien

Suppose you have the following variable declarations:


int a, b;
double z;
char ch;

The following statements show how the extraction operator >> works.

Statement Input Value Stored in Memory


1 cin >> ch; A ch = 'A'
2 cin >> ch; AB ch = 'A', 'B' is held for later input
3 cin >> a; 48 a = 48
4 cin >> a; 46.35 a = 46, .35 is held for later input
5 cin >> z; 74.35 z = 74.35
6 cin >> z; 39 z = 39.0
7 cin >> z >> a; 65.78 38 z = 65.78, a = 38
‫)‪Programming in C++(I‬‬
‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec.9 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 9
Mathematical expressions and mathematical operators:
One of the most important uses of a computer is its ability to calculate. You can use
the standard arithmetic operators to manipulate integral and floating-point data types.
There are five arithmetic operators.

Arithmetic Operators: + (addition), - (subtraction or negation), * (multiplication), /


(division), % (mod, (modulus or remainder)).

You can use the operators +, -, *, and / with both integral and floating-point data
types. You use % with only the integral data type to find the remainder in ordinary
division. Consider the following:
-5
8-7
3+4
2 + 3 *5
5.6 + 6.2 *3
x + 2 *5 + 6 / y3

Formally, an arithmetic expression is constructed using arithmetic operators and


numbers. The numbers appearing in the expression are called operands. The numbers
that are used to evaluate an operator are called the operands for that operator. In
Operators that have only one operand are called unary operators. Operators that have
two operands are called binary operators.
Unary operator: An operator that has only one operand.
Binary operator: An operator that has two operands.

Consider the following expression:


+27
In this expression, the operator + indicates that the number 27 is positive. Here, + has
only one operand and so acts as a unary operator. Both - and + are both unary and
binary arithmetic operators. However, as arithmetic operators, *, /, and % are binary
and so must have two operands.
Lec.9 Programming in C++ (I) Sura Abed Sarab Hussien

#include <iostream>
using namespace std;
int main()
{
cout << "2 + 5 = " << 2 + 5 << endl;
cout << "13 + 89 = " << 13 + 89 << endl;
cout << "34 - 20 = " << 34 - 20 << endl;
cout << "45 - 90 = " << 45 - 90 << endl;
cout << "2 * 7 = " << 2 * 7 << endl;
cout << "5 / 2 = " << 5 / 2 << endl;
cout << "14 / 7 = " << 14 / 7 << endl;
cout << "34 % 5 = " << 34 % 5 << endl;
cout << "4 % 6 = " << 4 % 6 << endl;
return 0;
}

2+5=7
13 + 89 = 102
34 - 20 = 14
45 - 90 = -45
5/2=2
14 / 7 = 2
34 % 5 = 4
4%6=4

Order of Precedence
When more than one arithmetic operator is used in an expression, C++ uses the
operator precedence rules to evaluate the expression. According to the order of
precedence rules for arithmetic operators,
*, /, %
are at a higher level of precedence than:
+, -
Note that the operators *, /, and % have the same level of precedence. Similarly, the
operators + and - have the same level of precedence. When operators have the same
level of precedence, the operations are performed from left to right. To avoid
confusion, you can use parentheses to group arithmetic expressions. For example,
using the order of precedence rules,
3*7-6+2*5/4+6
means the following:
(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6
= ((21 – 6) + (10 / 4)) + 6 (Evaluate *)
= ((21 – 6) + 2) + 6 (Evaluate /. Note that this is an integer division.)
Lec.9 Programming in C++ (I) Sura Abed Sarab Hussien

= (15 + 2) + 6 (Evaluate –)
= 17 + 6 (Evaluate first +)
= 23 (Evaluate +)

Note that the use of parentheses in the second example clarifies the order of
precedence. You can also use parentheses to override the order of precedence rules
In the expression:

3+4*5

* is evaluated before +. Therefore, the result of this expression is 23. On the other
hand,
in the expression: (3 + 4) * 5
+ is evaluated before * and the result of this expression is 35. Because arithmetic
operators are evaluated from left to right, unless parentheses are present, the
associativity of the arithmetic operators is said to be from left to right.

Increment and Decrement Operators


These operators are used frequently by C++ programmers and are useful
programming tools. Suppose count is an int variable. The statement:
count = count + 1;

increments the value of count by 1. To execute this assignment statement, the


computer first evaluates the expression on the right, which is count + 1. It then assigns
this value to the variable on the left, which is count.
C++ provides the increment operator, ++, which increases the value of a variable by
1, and the decrement operator, ––, which decreases the value of a variable by 1.

Increment and decrement operators each have two forms, pre and post. The syntax of
the increment operator is:
Pre-increment: ++variable
Post-increment: variable++
The syntax of the decrement operator is:
Pre-decrement: – –variable
Lec.9 Programming in C++ (I) Sura Abed Sarab Hussien

Post-decrement: variable– –
The statement:
++count;
or:
count++;
increments the value of count by 1.
Similarly, the statement:
––count;
or:
count––;
decrements the value of count by 1.

What is the difference between the pre and post forms of these operators?
The difference becomes apparent when the variable using these operators is employed
in an expression. Suppose that x is an int variable. If ++x is used in an expression,
first the value of x is incremented by 1, and then the new value of x is used to evaluate
the expression. On the other hand, if x++ is used in an expression, first the current
value of x is used in the expression, and then the value of x is incremented by 1. The
following example clarifies the difference between the pre- and post-increment
operators.
Suppose that x and y are int variables. Consider the following statements:
x = 5;
y = ++x;
The first statement assigns the value 5 to x. To evaluate the second statement, which
uses the pre-increment operator, first the value of x is incremented to 6, and then this
value, 6, is assigned to y. After the second statement executes, both x and y have the
value 6.

Now, consider the following statements:


x = 5;
y = x++;
As before, the first statement assigns 5 to x. In the second statement, the post-
increment operator is applied to x. To execute the second statement, first the value of
x, which is 5, is used to evaluate the expression, and then the value of x is
Lec.9 Programming in C++ (I) Sura Abed Sarab Hussien

incremented to 6. Finally, the value of the expression, which is 5, is stored in y. After


the second statement executes, the value of x is 6, and the value of y is 5.

Suppose a and b are int variables and:


a = 5;
b = 2 + (++a);
The first statement assigns 5 to a. To execute the second statement, first the
expression 2 +(++a) is evaluated. Because the pre-increment operator is applied to a,
first the value of a is incremented to 6. Then 2 is added to 6 to get 8, which is then
assigned to b. Therefore, after the second statement executes, a is 6 and b is 8.
On the other hand, after the execution of the following statements:
a = 5;
b = 2 + (a++);
the value of a is 6 while the value of b is 7.

Example
#include<iostream>

using namespace std;

main() {

int x, y, z;

x = 10;

y = 10;

z = ++x; //z will hold 11


Lec.9 Programming in C++ (I) Sura Abed Sarab Hussien

cout << "Z: " << z << endl;

z = y++; //z will hold 10, then y will be 11

cout << "Z: " << z << " and y is: " << y << endl;

Output
Z: 11
Z: 10 and y is: 11
‫)‪Programming in C++(I‬‬
‫‪2020-2021‬‬
‫ﺍﻟﺪﺭﺍﺳﺎﺕ ﺍﻷﻭﻟﻴﺔ ‪ /‬ﺍﻟﻔﺼﻞ ﺍﻻﻭﻝ‬
‫ﺃﺳﺘﺎﺫ ﺍﻟﻤﺎﺩﺓ‬
‫ﻡ‪.‬ﻡ‪ .‬ﺳﺮﻯ ﻋﺒﺪ ﺳﺮﺍﺏ‬
Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien

Lecture 10

C++ Taking Multiple Inputs

#include <iostream>
using namespace std;

int main() {
char a;
int num;

cout << "Enter a character and an integer: ";


cin >> a >> num;

cout << "Character: " << a << endl;


cout << "Number: " << num*2; Mathematical expression :[Comment [DS21

return 0;
}

Output
Memory: :[Comment [DS22
a='Z'
Enter a character and an integer: Z 40 num=40

Character: Z
Number: 80

Suppose you have the following variable declarations:


int a, b;
double z;
char ch;

The following statements show how the extraction operator >> works.

Statement Input Value Stored in Memory


1 cin >> ch; A ch = 'A'
2 cin >> ch; AB ch = 'A', 'B' is held for later input
3 cin >> a; 480 a = 480
4 cin >> a; 480.35 a = 480, 0.35 is held for later input
5 cin >> z; 74.35 z = 74.35
6 cin >> z; 39 z = 39.0
7 cin >> z >> a; 65.78 38 z = 65.78, a = 38
Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien

Mathematical expressions and mathematical operators:


One of the most important uses of a computer is its ability to calculate. You can use
the standard arithmetic operators to manipulate integral and floating-point data types.
There are five arithmetic operators.

Arithmetic Operators: + (addition), - (subtraction or negation), * (multiplication), /


(division), % (mod, (modulus or remainder)).
• Mathematical operator
• Unary operator (needs only one operand – right operand)
• Binary operator (needs two operands: left and right operands)
• Operand
• Left operand
• Right operand

You can use the operators +, -, *, and / with both integral and floating-point data
types. You use % with only the integral data type to find the remainder in ordinary
division. Consider the following:
int x =8;
-5 negation operator :[Comment [DS23
unary operator
8-7
left operand :[Comment [DS24
3+4
subtraction operator :[Comment [DS25
2 + 3 *5
right operand :[Comment [DS26
5.6 + 6.2 *3

x + 2 *5 + 6 / 2 10 :[Comment [DS27
2nd operator :[Comment [DS28

Formally, an arithmetic expression is constructed using arithmetic operators and


numbers. The numbers appearing in the expression are called operands. The numbers
that are used to evaluate an operator are called the operands for that operator. In
Operators that have only one operand are called unary operators. Operators that have
two operands are called binary operators.
Unary operator: An operator that has only one operand.
Binary operator: An operator that has two operands.

Consider the following expression:


Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien

+27
In this expression, the operator + indicates that the number 27 is positive. Here, + has
only one operand and so acts as a unary operator. Both - and + are both unary and
binary arithmetic operators. However, as arithmetic operators, *, /, and % are binary
and so must have two operands.

#include <iostream>
using namespace std;
int main()
{
cout << "2 + 5 = " << 2 + 5 << endl;
cout << "13 + 89 = " << 13 + 89 << endl;
cout << "34 - 20 = " << 34 - 20 << endl;
cout << "45 - 90 = " << 45 - 90 << endl;
cout << "2 * 7 = " << 2 * 7 << endl;
cout << "5 / 2 = " << 5 / 2 << endl;
cout << "14 / 7 = " << 14 / 7 << endl;
cout << "34 % 5 = " << 34 % 5 << endl;
cout << "4 % 6 = " << 4 % 6 << endl;
return 0;
}

2+5=7
13 + 89 = 102
34 - 20 = 14
45 - 90 = -45
5/2=2
14 / 7 = 2
34 % 5 = 4
4%6=4

Order of Precedence
When more than one arithmetic operator is used in an expression, C++ uses the
operator precedence rules to evaluate the expression. According to the order of
precedence rules for arithmetic operators,
*, /, %
are at a higher level of precedence than:
+, -
Note that the operators *, /, and % have the same level of precedence. Similarly, the
Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien

operators + and - have the same level of precedence. When operators have the same
level of precedence, the operations are performed from left to right. To avoid
confusion, you can use parentheses to group arithmetic expressions. For example,
using the order of precedence rules,
3*7-6+2*5/2+6
(3*7) - 6 + 2 * 5 / 2 + 6
21- 6 + 2 * 5 / 2 + 6
21- 6 + (2 * 5) / 2 + 6
21- 6 + 10 / 2 + 6
21- 6 + (10 / 2) + 6
21- 6 + 5 + 6
(21- 6) + 5 + 6
15 + 5 + 6
(15 + 5) + 6
20 + 6
26

3*7-6+2*5/4+6

means the following:


(((3 * 7) – 6) + ((2 * 5) / 4 )) + 6
= ((21 – 6) + (10 / 4)) + 6 (Evaluate *)
= ((21 – 6) + 2) + 6 (Evaluate /. Note that this is an integer division.)
= (15 + 2) + 6 (Evaluate –)
= 17 + 6 (Evaluate first +)
= 23 (Evaluate +)

Note that the use of parentheses in the second example clarifies the order of
precedence. You can also use parentheses to override the order of precedence rules
In the expression:
3+4*5
Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien

* is evaluated before +. Therefore, the result of this expression is 23. On the other
hand,
in the expression: (3 + 4) * 5
+ is evaluated before * and the result of this expression is 35. Because arithmetic
operators are evaluated from left to right, unless parentheses are present, the
associativity of the arithmetic operators is said to be from left to right.

Increment and Decrement Operators


These operators are used frequently by C++ programmers and are useful
programming tools. Suppose count is an int variable. The statement:
int count = 0;
count = count + 1; count = 1 :[Comment [DS29
Assignment operator :[Comment [DS210
0+1=1 :[Comment [DS211
Assignment statement :[Comment [DS212

int count = 0;
count++; ------- count = count + 1; Increment operator by one :[Comment [DS213

int count = 0;
++count; ------- count = count + 1; Increment operator by one :[Comment [DS214

int x = 10;
x++; x ---- 11 Post increment :[Comment [DS215

++x; x ---- 12 Pre-increment :[Comment [DS216

|
|
|
V
int x = 10;
x = x + 1; X=11 :[Comment [DS217

x = x + 1; X=12 :[Comment [DS218


Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien

int i = 5; Memory; :[Comment [DS219


i=5
++i; i=6 :[Comment [DS220

cout<<i<<endl;
i++; i=7 :[Comment [DS221

cout<<i<<endl;

int i = 5; Memory; :[Comment [DS222


i=5
i--; i=4 :[Comment [DS223

cout<<i<<endl;
--i; i=3 :[Comment [DS224

cout<<i<<endl;

int i = 5; Memory; :[Comment [DS225


i=5
i--; i=4 :[Comment [DS226

cout<<i<<endl;
++i; i=5 :[Comment [DS227

cout<<i<<endl;

increments the value of count by 1. To execute this assignment statement, the


computer first evaluates the expression on the right, which is count + 1. It then assigns
this value to the variable on the left, which is count.
C++ provides the increment operator, ++, which increases the value of a variable by
1, and the decrement operator, – –, which decreases the value of a variable by 1.

Increment and decrement operators each have two forms, pre and post. The syntax of
the increment operator is:
Pre-increment: ++variable
Post-increment: variable++
Lec.10 Programming in C++ (I) Sura Abed Sarab Hussien

The syntax of the decrement operator is:


Pre-decrement: – –variable
Post-decrement: variable– –
The statement:
++count;
or:
count++;
increments the value of count by 1.
Similarly, the statement:
––count;
or:
count––;
decrements the value of count by 1.

int x = 10; X = 10 :[Comment [DS228

++x; X = 11 :[Comment [DS229

x--; X=10 :[Comment [DS230

You might also like