Documente Academic
Documente Profesional
Documente Cultură
Learning Objective
At the end of the lecture, you would know:
the simple data types in C++
how to declare and manipulate data into arrays
the difference between passing parameters to
functions by value or by reference
how to pass arrays to functions
CT077-3-2-DSTR
Data Structures
Structured
Integral
Floating
char
short
float
double
int
long
long double
bool
Array
Struct
Union
Class
Data Structures
Integral
unsigned char
unsigned int
unsigned short
unsigned long
float
double
long
double
bool
Data Structures
Structured
Integral
Floating
char
short
float
double
int
long
long double
bool
Array
Struct
Union
Class
Data Structures
Arrays
Array: a collection of a fixed number of
components wherein all of the components have
the same data type
In a one-dimensional array, the components are
arranged in a list form
Syntax for declaring a one-dimensional array:
CT077-3-2-DSTR
Data Structures
Arrays
Example:
int num[5];
CT077-3-2-DSTR
Data Structures
CT077-3-2-DSTR
Data Structures
Data Structures
Array Initialization
Arrays can be initialized during declaration
In this case, it is not necessary to specify the
size of the array
Size determined by the number of initial
values in the braces
Example:
double sales[] = {12.25, 32.50, 16.90, 23, 45.68};
CT077-3-2-DSTR
Data Structures
10
Array Initialization
The statement:
int list[10] = {0};
declares list to be an array of 10 components
and initializes all of them to zero
The statement:
int list[10] = {8, 5, 12};
declares list to be an array of 10 components,
initializes list[0] to 8, list[1] to 5, list[2] to 12 and
all other components are initialized to 0
CT077-3-2-DSTR
Data Structures
11
CT077-3-2-DSTR
Data Structures
12
Data Structures
13
Two-Dimensional Arrays
Two-dimensional array: collection of a fixed
number of components (of the same type)
arranged in two dimensions
Sometimes called matrices or tables
Declaration syntax:
Data Structures
14
Two-Dimensional Arrays
Example:
double sales [10][5];
CT077-3-2-DSTR
Data Structures
15
Data Structures
16
CT077-3-2-DSTR
Data Structures
17
Two-Dimensional Array
Initialization
Two-dimensional arrays can be initialized when
they are declared:
Data Structures
18
CT077-3-2-DSTR
Data Structures
19
Example Program
void grow(int& age) {
age = age + 1;
cout << "grow age is " << age << endl;
}
int main() {
int age = 20;
cout << "main age is " << age << endl;
grow(age);
cout << "main age is " << age << endl;
return 0;
}
CT077-3-2-DSTR
Data Structures
20
Example Program - 2
void dec (int & x) {
x--;
}
void main() {
int value= 20;
inc (value);
cout << "value is " << value << endl;
dec (value);
cout << "value is " << value << endl;
}
CT077-3-2-DSTR
Data Structures
21
Example Program - 3
CT077-3-2-DSTR
Data Structures
22
Example Program - 3
CT077-3-2-DSTR
Data Structures
23
Reference parameter:
Received address leads to memory space of the
corresponding actual parameter
Changing reference parameter affects the actual
parameter
CT077-3-2-DSTR
Data Structures
25
Reference Parameters
Reference parameters are useful in three
situations:
You want to change the actual parameter
No easy way to return more than one value
from a function, so you pass more reference
parameters
When passing the address would save
memory space and time (no copying)
CT077-3-2-DSTR
Data Structures
26
Data Structures
27
Homework
Implement examples given in the lecture
Write a: void swap (int , int) function which
swaps the values of its two given parameters
CT077-3-2-DSTR
Data Structures
28
CT077-3-2-DSTR
Data Structures
29
CT077-3-2-DSTR
Data Structures
30