Programming Essentials
in C++
Dr. Esraa M. Elhariri
Computer Science Department,
Faculty of Computers and Information
Fayoum University
2023-2024
How to think like acompiler
and act like in run-time?
Steps:
Scan the source code for syntax errors.
Scan the source code for logical errors.
Perform static allocation for all variable.
Execute instructions one by one
Scan the source code for syntax errors:
Braces,
Semi-colon,
Declaration errors,
Ambiguous,
Undefined variables.
Array
You can declare 1D array You can access array
as follows: elements as follows:
int x[5]; arrName[index] or
int x[5]={1,2,3,4,5}; index[arrName]
int x[]={1,2,3,4,5};
int x[5]={0}; // all is zero To access the first element in
int x[5]={1}; // first one is 1,
x: x[0] or 0[x]
the rest are zeros
Array
You can declare 2D array as follows:
int x[3][3];
int x[3][3]={1,2,3,4,5,6,7,8,9};
int x[][3]={1,2,3,4,5,6,7,8,9};
int x[3][3]={{1,2,3},{4,5,6},{7,8,9}};
int x[][3]={{1,2,3},{4,5,6},{7,8,9}};
//Error
int x[][]={{1,2,3},{4,5,6},{7,8,9}};
int x[3][]={{1,2,3},{4,5,6},{7,8,9}};
int x[3][]={1,2,3,4,5,6,7,8,9};
Some Restrictions on Array Processing
• Consider the following statements:
• C++ does not allow aggregate operations on an array:
• Solution:
7
Finding address of an element
with given base address
8
The Address Of Array Elements
When an array is declared, a contiguous block of
memory is assigned to it which helps in finding address
of elements from base address.
For a single dimensional array a[100], address of i^th
element can be found as:
• addr(a[i]) = BASE+ i*SIZE
Where BASE represents base address
(address of 0^th element) and SIZE
represents size of each element in the array.
9
The Address Of Array Elements
• Given an array double y [10], assume that the
base address in 2000, what is the address of
element at index 5?
• addr(y[5]) = 2000+ 5*8
a) 2016
b) 2005
c) 2040
d) 2020
10
The Address Of Array Elements
When an array is declared, a contiguous block of
memory is assigned to it which helps in finding address
of elements from base address.
For a single dimensional array a[100], address of i^th
element can be found as:
addr(a[i]) = BASE+ i*SIZE
Where BASE represents base address (address of
0^th element) and SIZE represents size of each
element in the array.
11
The Address Of Array Elements
• For a two dimensional array x[3][3], the elements can
be represented as:
• As 2-D array is stored in row major order in C++
language, row 0 will be stored first followed by row 1
and row 2.
12
The Address Of Array Elements
• For finding the address of x[2][2], we need to go to
2nd row (each row having 3 elements).
• After reaching 2nd row, it can be accessed as single
dimensional array. Therefore, we need to go to 2nd
element of the array.
• Assuming BASE as 0 and size as 1, the address of
x[2][2] will be 0 + (2 * 3 + 2) * 1 = 8.
13
The Address Of Array Elements
For a given array with m rows and n columns, the
address can be calculated as:
add(a[i][j]) = BASE + (i*n + j) * SIZE
Where BASE represents base address (address of
0th element), n represents number of columns in 2-D
array and SIZE represents size of each element in
the array.
14
Consider the following declaration of a ‘two-
dimensional array in C++:
char a[100][100];
Assuming that the main memory is byte-addressable
and that the array is stored starting from memory
address 0, the address of a[40][50] is:
(A) 4040
(B) 4050
(C) 5040
(D) 5050
• Solution: Using the formula
• add(a[i][j]) = BASE + (i*n + j) * SIZE,
• addr[40][50] = 0 + (40*100 + 50) * 1 = 4050 15
Static, Global and Automatic
Variables
16
Predict the output of the following code?
#include <iostream> i i<5 a b
using namespace std;
void increment( )
{
int a=0;
static int b=0;
cout<< "a: "<<a <<", "<< "b: "<<b<<endl;
a++;
b++;
}
int main()
{
int loop;
for(loop=0; loop<5; loop++)
increment();
return 0;
} 17
Predict the output of the following code?
#include <iostream> i i<5 a b
using namespace std;
int a=0;
void increment( )
{
static int b=0;
cout<< "a: "<<a <<", "<< "b: "<<b<<endl;
a++;
b++;
}
int main()
{
int loop;
for(loop=0; loop<5; loop++)
increment();
return 0;
} 18
Predict the output of the following code?
#include <iostream> i i<5 a b
using namespace std;
void increment( )
{ int a=0;
static int b=0;
b=5;
cout<< "a: "<<a;
a++; b++;
cout<<", "<< "b: "<<b<<endl;
}
int main()
{
int loop;
for(loop=0; loop<5; loop++)
increment();
return 0;
} 19
Predict the output of the following code?
#include <iostream> i i<5 a b
using namespace std;
static int b=0;
void increment( )
{ int a=0;
cout<< "a: "<<a <<", "<< "b: "<<b<<endl;
a++;
b++;
}
int main()
{ b=5;
int loop;
for(loop=0; loop<5; loop++)
increment();
return 0;
} 20
Predict the output of the following code?
static int b=0; i i<5 a b
void increment( )
{ int a=0;
cout<< "a: "<<a <<", "<< "b: "<<b<<endl;
a++; b++;
}
void decrement( )
{ int a=0;
cout<< "a: "<<a <<", "<< "b: "<<b<<endl;
a--; b--;
}
int main()
{ b=5;
int loop;
for(loop=0; loop<5; loop++){
increment();}
decrement();
21
return 0; }
Predict the output of the following code?
static int b=0; i i<5 a b
void increment( ){
int a=0;
cout<< "a: "<<a <<", "<< "b: "<<b<<endl;
a++; b++;
}
void decrement( ) {
int a=0;
cout<< "a: "<<a <<", "<< "b: "<<b<<endl;
a--; b--;
}
int main()
{ b=5;
int loop;
for(loop=0; loop<5; loop++){
increment();
decrement(); }
return 0;} 22
In below: host is a local integer variable host Modify
the cin statement to allow the global variable host to
receive a float value as a result of the cin statement.
#include <iostream>
using namespace std;
float host;
void main ()
{
int host;
cin >> host; cin >>::host;
}
23
Assume the following definition and initialization:
int link[5] = { 2, 3, 4, 0, 1} ;
What output does each of the following statements
produce:
(a) cout << link[0]; 2
1- get link[2] which is 4
2- get link[4] which is 1.
(b) cout << link[link[2]]; 1
(c) Given any integer k (0<=k<=4), what is the
value of the following expression when k=4
link[link[link[link[link[k]]]]] 4
1- get link[k] link[4] which is 1 5- get link[3] which is 0
2- Now you have 6- Now you have link[link[0]]
link[link[link[link[1]]]] 7- get link[0] which is 2.
3- get link[1] which is 3. 8- Now you have link[2] which is 4.
4- Now you have link[link[link[3]]] 24
Given the following function prototypes...
int suspend (int, int);
float suspend (int, int=30, int=25, int=53);
• Which of the following function calls leads to
Which function call will cause
an ambiguity? confusion for the compiler?
(b) by passing only two
(a) suspend (3); values, compiler won’t know
which function you mean the
(b) suspend (3,5); first one or the second one
using last two parameters with
default values.
(c) suspend (3,5,7);
(d) suspend (3,5,7,9);
25
Which of the following best describes the statement
z = sum (5.2); below?
float sum (float = 1.0, float = 0.0);
z = sum (5.2);
(a) equivalent to z = sum (1.0, 0.0);
(b) equivalent to z = sum (5.2, 0.0);
(c) equivalent to z = sum (5.2, 1.0);
(d) leads to a compile-time error -- not enough
arguments to function sum
26
Initialization of 2-D Arrays
int Array1[2][3] = { {1, 2, 3} , {4, 5, 6} }; int Array2[2][3] = { 1, 2, 3, 4, 5 };
Rows of Array1: Rows of Array2:
123 123
456 450
27
Initialization of 2-D Arrays
int Array3[2][3] = { {1, 2} , {4 } }; int Array4[][3] = { {1, 2} , {4 } };
Rows of Array3: Rows of Array4:
120 120
400 400
28
Accessing 2-D Array elements
Example :
0 1 2 3
0
float marks [6] [4] ; 1
2
marks [4][2]= 20 ; 3
4 20
Col[row[marks]] 5
2[4[marks]]
29
Predict the output of the following code?
int main(){
int a[2][] = {{1,2},{3,4}}; (A) 1 2 3 4
int i, j; (B)Compiler Error
for (i = 0; i < 2; i++) (C)4 garbage values
for (j = 0; j < 2; j++) (D) 4 3 2 1
cout<< a[i][j];
return 0; }
Number of columns must be
specified.
30
Predict the output of thefollowing code?
int main(){
int a[][2] = {{1,2},{3,4}}; (A) 1 2 3 4
int i, j; (B)Compiler Error
for (i = 0; i < 2; i++) (C)4 garbage values
for (j = 0; j < 2; j++) (D) 4 3 2 1
cout<<a[i][j];
return 0; }
31
Functions
What would be printed by the following program?
#include<iostream>
using namespace std;
int sum(int x= 10, int y, int z=20)
{ Compiler Error variables
int result = x+y+z; return result; with default values must be at
} the end of parameters list.
int main() {
cout<<sum(10); Error: 'sum': missing default
return 0; argument for parameter 2.
}
Functions
What would be printed by the following program?
#include<iostream>
using namespace std;
int sum(int x, int y=10, int z=20)
{ Correct Code variables with
int result = x+y+z; return result; default values all at the end of
} parameters list so, output will
int main() { be 40.
cout<<sum(10);
return 0; It will take value of 10 for x and
} both default values for y and z.
What will this program print?
#include<iostream>
using namespace std;
A. 4525
int main(){
B. 2525
int i = 2; C. 4545
{ D. None of the these
int i = 4, j = 5;
cout << i << " " << j;
Compiler Error j is defined
}
only in the inner block.
cout << i << " " << j; So, It will give ‘j’ undeclared
return 0; identifier.
}
#include<iostream>
using namespace std;
static int x = 5;
int main(){
x = 9;
{ Output will be 9 x here is the
int x = 4; global one
}
But for int x =4; it is only
cout<<x; defined locally in the inner
block.
return 0;
}
#include<iostream>
using namespace std;
static int x ;
int main()
{
x++; Output will be 1 x here is the
global one
{
int x = 4; Uninitialized global variable will
} take default value of zero.
cout<<x; 1- static int x will be zero
2- x++ will increment it to 1.
return 0;
For int x =4; it is only defined
} locally in the inner block.