Sunteți pe pagina 1din 29

Data Structure Using C++

Overview of C++ (part 2)

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

C++ Data Type


Data Type
Simple

Structured

Integral

Floating

char

short

float

double

int

long

long double

bool

Array
Struct
Union
Class

A data type is called simple if variables of that type can


store only one value at a time
Simple types are also called: Primitive or Built-in Types
CT077-3-2-DSTR

Data Structures

C++ Data Type


Simple Data Types
Floating

Integral
unsigned char
unsigned int

unsigned short
unsigned long

float

double
long
double

bool

bool type is not supported by all C++ compilers


Can add unsigned modifier before char, short, int, and
long, so that variables of that type are positive always
(maximum storable number gets doubled)
CT077-3-2-DSTR

Data Structures

C++ Data Type


Data Type
Simple

Structured

Integral

Floating

char

short

float

double

int

long

long double

bool

Array
Struct
Union
Class

A structured data type is one in which each data item is a


collection of other data items
Very similar to classes, but should be thought of as not
having methods (just data grouping)
CT077-3-2-DSTR

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

Accessing Array Components


General syntax:

where indexExp, called an index, is any


expression whose value is a nonnegative integer
Index value specifies the position of the
component in the array (called random access)
The array index always starts at 0

CT077-3-2-DSTR

Data Structures

Accessing Array Components


Example:
list [3] = 10;
list [6] = 35;
list [5] = list [3] + list [6];
In memory, array elements
are reserved adjacent to
each other in order to
guarantee fast random
access to any element
CT077-3-2-DSTR

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

Some Restrictions on Array


Processing
Consider the following statements:

C++ does not allow aggregate operations on an


array:
Solution:

CT077-3-2-DSTR

Data Structures

12

Some Restrictions on Array


Processing
The following is illegal too:
Solution:

The following statements are legal, but do not


give the desired results This doesnt print all elements of the array.
It prints the memory address of the first
element (also called base address)
This doesnt compare all elements of
both arrays, but compares the memory
base addresses of the two arrays
CT077-3-2-DSTR

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:

where intexp1 and intexp2 specify the number of


rows and the number of columns, respectively in
the array
CT077-3-2-DSTR

Data Structures

14

Two-Dimensional Arrays
Example:
double sales [10][5];

CT077-3-2-DSTR

Data Structures

15

Accessing Array Components


Syntax:

where indexexp1 and indexexp2 are expressions


yielding nonnegative integer values, and specify
the row and column position
2D arrays are also specially arranged in memory
as adjacent rows, so that fast random access to
any element (any row, any column) is guaranteed
CT077-3-2-DSTR

Data Structures

16

Accessing Array Components


Example:
sales [5][3] = 25.75;

CT077-3-2-DSTR

Data Structures

17

Two-Dimensional Array
Initialization
Two-dimensional arrays can be initialized when
they are declared:

Elements of each row are enclosed within


braces and separated by commas
All rows are enclosed within braces
For number arrays, if all components of a row
arent specified, unspecified ones are set to 0
CT077-3-2-DSTR

Data Structures

18

Passing Parameters to Functions


Value parameter: the formal parameter receives
a copy of the content of corresponding actual
parameter

Reference parameter: the formal parameter


receives the location (memory address) of the
corresponding actual parameter
Syntax: put & mark before parameter name

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 inc (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

Summary - Passing Parameters


Value parameter:
During program execution, manipulating value parameter
changes its data copy (stored in its own memory space)
The actual parameter stays intact

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

Arrays as Parameters to Functions


Arrays are passed by reference, always
The symbol & is not used when declaring an
array as a formal parameter
The size of the array is usually omitted
If provided, it is ignored by the compiler

Unlike Java, array size must be passed-in to the

function by some means (parameter, etc.)


CT077-3-2-DSTR

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

What we will cover next


Overview of C++
Pointers
Passing parameters to functions using pointers
Dynamic Memory Allocation

Very Important Topics


- not to be missed out -

CT077-3-2-DSTR

Data Structures

30

S-ar putea să vă placă și