Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and...

Post on 01-Jan-2016

269 views 3 download

Transcript of Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and...

Lecture Contents

Arrays and Vectors:• Concepts of array.• Memory index of array.• Defining and Initializing an array.• Processing an array.• Parsing an array to a function.• Multi-dimensional array.• Vectors.• Examples.

Concept of Array

• An array is sequence of objects which have same types.

• Array is a very common data structure in nature and in computer programming languages.

• The Objects are called elements of the array and they are indexed by their order in the sequence.

Memory Index

• An array is stored in consecutive memory cells.• Array name is used to determine the base address of the

array (cell 0).• Index starts from 0.

Array Declaration

<type> <name>[<number of elements>];

Note:

Indices are from 0 to <number of elements>-1;

E.g:

int a[20];

double b[10];

char str[17];

Initializing an Array

E.g:

int a[5]={1,5,8,2,4};

double b[3]={1.2,3.4,5.15};

char a[5]={"S","E","O","U","L"};

Note: C++ does not check whether an index gets over the boundary of an array.

Processing an Array

The basic Loop:

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

{

<processing the element ith of the array>;

}

To access an element in an array, use its index:

<name of array> [<index>]

a[5], b[i], c[i+5], d[i+j]

Passing an Array to Functions

You need to tell the function two things:• Address of the first element of the array (name of the

array, or pointer to the first element (study later)).• The number of elements in the array.

int sum(int [],int);

int a[24];

….

b= sum(a,24)

Note: the array is passed by reference to functions!

Multi-Dimensional Arrays

• Each element of an array could be an ARRAY. In that case, we have two-dimensional array (a table). It can be extended to more general multi-dimensional cases.

• In reality, you hardly use array of dimension more than 3.

Memory Index

• A 2-dimensional array is stored in consecutive memory cells as a table.

• Array name is used to determine the base address of the array (cell 0,0).

• Indices of row and column start from 0.

Array Declaration

<type> <name>[<# of elements>][<# of elements>];

Note:

Indices are from 0 to <number of elements>-1;

E.g:

int a[3][4];

double b[10][10];

char str[17][5];

Initializing an Array

E.g:

int a[2][3]={{1,5,8},{2,4,7}};

Note: C++ does not check whether an index gets over the boundary of an array.

Processing an Array

The basic Loop:

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

for(j=0; j<m; j++)

{

<processing the element at row ith column jth of the array>;

}

To access an element in an array, use its indices:

<name of array> [<row index>][<column index>]

a[5][6], b[i][2], c[i+5][j-5], d[i+j][7]

Passing an Array to Functions

You need to tell the function two things:• Address of the first element of the array (name of the

array, or pointer to the first element (study later)).• The sizes of row and column of the array.

int sum(int [][26],int);

int a[24][26];

….

b= sum(a,24)

Note: the array is passed by reference to functions!

Vector Template in C++

Disadvantages of Array:• C-based -> obsolete.• No out-of-bound checking.• Have to remember its maximal size (bounds).• Not convenient for copy and comparison.

Solution? - Vector Template provided by C++.• Similar to JAVA based array.• More Flexible.• More Universal.• Safer then C-based array.

Vector Declaration

//using vetor

#include <vector>

E.g:

vector<int> a(10);

vector<double>b(5);

vector<char>c(6);

Vector Processing

• Get the size of a vector:

a.size();• Access an element of a vector:

– a.at(5)– a[5]

• An attempt to access an element of out bound will result in error.

Passing a Vector to Functions

You only need to tell the function:

• Name and type of the vector

InputVector(vector<int> a)

Operator Overloading for Vectors

You could manipulate vectors at primitive data types in a very natural way with operators like ==,=,!=:

E.g:

if (a==b){

}

……

b=c;

More on vector class: http://msdn2.microsoft.com/en-us/library/9xd04bzs(VS.80).aspx

Examples

Examples:

- Sorting an array using Bubble sort.

- Sorting an array using Selection sort.

- Linear search of a sorted array.

- Binary Search of a sorted array.

- Insert a member in to an sorted array that maintain the sorted order of the array.

- Check if an array of numbers is increasing in values.

- Find the longest run (subsequence) of increasing values in an numeric array.

Examples

Examples:

- Count a frequencies of a value x in an array.

- Find the most frequent (mode) value in an array.

- Delete all duplications of values within an array.

- Compute the trace (sum of diagonal numbers) and Transpose a numeric matrix.

- Compute the product of two numeric matrices.

- Compute the Minimax and its location (row, column) of a numerical matrix, where Minimax is the member that is minimal in its row and maximal in its column.

Further Readings

• Textbook 1: Chapter 6.• Workbook 1: Chapter 5.