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

21
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.

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

Page 1: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 2: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 3: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 4: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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];

Page 5: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 6: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing 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]

Page 7: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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!

Page 8: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 9: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 10: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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];

Page 11: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 12: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing 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]

Page 13: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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!

Page 14: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 15: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

Vector Declaration

//using vetor

#include <vector>

E.g:

vector<int> a(10);

vector<double>b(5);

vector<char>c(6);

Page 16: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 17: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

Passing a Vector to Functions

You only need to tell the function:

• Name and type of the vector

InputVector(vector<int> a)

Page 18: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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

Page 19: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

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.

Page 20: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an 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.

Page 21: Lecture Contents Arrays and Vectors: Concepts of array. Memory index of array. Defining and Initializing an array. Processing an array. Parsing an array.

Further Readings

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