03-Overview of C++ (Part 2)

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Data Structures

CT077-3-2- DSTR and Version VC1

Overview of C++ (part 2)


Topic Structure

• Data type
• Arrays
• Parameter passing

CT077-3-2-DSTR Data Structures 2


Learning Objective

At the end of this lesson, 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 3


Key Terms

• Primitive and structured data type


• 1-D array, 2-D array
• Passing by value
• Passing by reference

CT077-3-2-DSTR Data Structures 4


C++ Data Type
Data Type

Simple

• 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 5


C++ Data Type
Simple Data Types

Integral

• 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 6
C++ Data Type
Data Type

Simple

• 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 7


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 8


Arrays

• Example:
int num[5];
//this is a static array

• In Java:
int [ ] arr; //declares array & creates reference only
arr = new int [ 5 ] ; //creates the actual array

CT077-3-2-DSTR Data Structures 9


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 10


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 11
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 12


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 13


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 14


Some Restrictions on Array
Processing
• The following is illegal too:

• Solution:

• The following statements are legal, but do not


give the desired results This doesn’t print all elements of the array.
It prints the memory address of the first
element (also called base address)

This doesn’t compare all elements of


both arrays, but compares the memory
base addresses of the two arrays
CT077-3-2-DSTR Data Structures 15
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 16
Two-Dimensional Arrays

• Example:
double sales [10][5];

CT077-3-2-DSTR Data Structures 17


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 18
Accessing Array Components

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

CT077-3-2-DSTR Data Structures 19


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
aren’t specified, unspecified ones are set to 0
CT077-3-2-DSTR Data Structures 20
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 21


Example Program

void grow(int& x) {
x= x+ 1;
cout << "grow age is " << x<< 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 22
Example Program - 2

void inc (int x) { void dec (int & x) {


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 23
Example Program - 3

CT077-3-2-DSTR Data Structures 24


Example Program - 3

CT077-3-2-DSTR Data Structures 25


Example Program 3 - Output

Execute program 3 and


check the output

CT077-3-2-DSTR Data Structures 26


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 27


Reference Parameters

• Reference parameters are useful in three main


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 28


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 29
Array Number

1 2 3 4 5

10 9 8 7 6

11 12 13 14 15

20 19 18 17 16

CT077-3-2-DSTR Data Structures 30


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 31


Summary of main teaching

• Data type
• Arrays
• Parameter passing

CT077-3-2-DSTR Data Structures 32


CT077-3-2-DSTR Data Structures 33
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 34

You might also like