Last updated: Friday 24th March 2006, 18:09, AHD

 

CSCI101

An Introduction to Programming using C++

 

Arrays

 

1  Introduction to Arrays

2  Arrays in Functions

3  Programming with Arrays

 

 

1 Introduction to Arrays

A variable (or simple variable) can take a single value.

 

An array variable is a special kind of object

that can take many values - all of the same type.

 

Say you want to store the exam results of 30 students.

You could create 30 variables:

 

 

int result1;

int result2;

int result3; and so on . . .

 

If you only had a few results to score,

this method would be acceptable.

But what if you had to store the results

of each student in the college, not just one class?

 

Storing 2000 results in this way would

be a very tedious and error-prone process.

You would have to type 2000 declaration statements

and 2000 assignments.

 

Fortunately, by using an array variable,

we get around this problem . . .

 

 

What is an array variable?

 

First consider a simple variable, the type we have used do far:

 

     int result;

 


 


An Array Variable

 

 

 


 

 

 

 

 

 

 


An Array Variable

 

The line of boxes is known as an array variable.

An array variable is given a name, just like the simple variable,

for example result, and each box is referred to by its number:

result[0], result[1] and so on...

 

 

The number of the box is known as the subscript or index

of the array element (box).

 

The variables that make up the array

are known as indexed variables, subscripted variables or elements.

 

Note: In C++ the index of an array always starts at 0.

 

 

 

 

 

 

 

 

Declaring an Array Variable

 

int result[8];

 

The number 8 is said to be the size of the array.

 

int is said to be the base type of the array.

 

The base type can be any type,

 

in particular it can be a class type, i.e. an object.

 

 

int result[8];

// note use of square brackets

 

after declaring an array, until you assign values to the elements,

the elements contain garbage (denoted by *)

 

 


 

 

 

 

 


Assigning Values to Arrays

 

result[0] = 75;

result[1] = 90;

result[4] = 72;

 

 

 

 


 

 


Assigning Values to Arrays at Declaration

 

  int result[8] = {75,90,0,0,72,0,0,0};

  int result[] = {75,90,0,0,72,0,0,0};

 

 

 


 

 

 

 

 

 

 


Initializing Arrays

int children[3] = {2, 12, 1};

 

The above declaration is equivalent to the following code:

 

int children[3];

children[0] = 2;

children[1] = 12;

children[2] = 1;

 

Initializing an Array

                  

    int b[ ] = {5, 12, 11};

 

is equivalent to:

 

          int b[3] = {5, 12, 11};

 

How big are the Elements?

 

An element is one of the boxes making up the array.

The size of the box in bytes depends on the data type of what you put in there. . .

 

An Array Variable

Consider an array variable as a line of these boxes. The boxes are all the same size.

 

Data Types

 

Each data type takes up a fixed amount of memory. . .

 

If you declare result as an array of integers, each element occupies 4 bytes of memory.

 

If you declare result as an array of double, then each element occupies 8 bytes of memory

 

Integer Data Types

 

short (int) 2 bytes

int 4 bytes

long (int) 8 bytes

 

Floating point Data Types

 

float 4 bytes

double 8 bytes

long double 10 bytes

 

A character

 

A character occupies a single byte,

and its byte value is the decimal value given by its ASCII code.

 

A is decimal 65 or binary 01000001

 

A Program Using an Array: array-01.cpp

http://www.annedawson.com/array-01.cpp

 

 

Indexed variable as an argument:array-03.cpp

http://www.annedawson.com/array-03.cpp

 

 

 

adjust_days(vacation[n]);

 

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

vacation[n] = adjust_days(vacation[n]);

/****************************************/

 

 

int adjust_days(int old_days)

{

   return (old_days + 5);

}

 

 

An entire array can be passed to a function. . .

 

 

 

2   Arrays in Functions

 

The name of any array is the address of its first element.

 

Array Parameters

 

A formal parameter for an entire array is neither call-by-value

or call-by-reference.

 

It is a new kind of formal parameter referred to as an array parameter.

 

The formal parameter (in the function header) for an entire array is, for example:

int arrayname[]

 

An example function header:

void fill_up(int a[], int size)

 

void fill_up(int a[], int size); // prototype

 

 

void fill_up(int a[], int size) // definition

{

   cout << "Enter " << size << "numbers:\n";

   for (int i = 0; i < size; i++)

     cin >> a[i];

   size--; // reduces size by 1

   cout << "The last array index used is "

        << size << endl;

}

 

 

An example function call

int score[5];

int number_of_scores = 5;

 

fill_up(score, number_of_scores);

 

 

 

An array parameter behaves very much like a call-by-reference parameter.

This is because, when you send an entire array to a function,

you pass across to the function the array name.

 

The name of any array is the address of its first element.

 

Using an entire array as a parameter to a function

 

You are actually passing the address of the array variable (the start address). . .

 

Changes made to an array passed to a method are permanent changes,

as the function is working on the actual array, not on a copy.

 

 

Call-by-reference parameters also work by passing the address of the variable,

rather than a copy of the value of the variable as in call-by-value.

Think of the word 'reference' as 'referring to an address'.

 

 

Why is an array parameter different to a call-by-reference parameter?

 

Call-by-reference is for simple variables (int, char etc),

and the compiler knows the size (in bytes) of all the simple variables,

as the size is fixed for each data type.

 

 

For example: integer data types

 

short (int) 2 bytes

int 4 bytes

long (int) 8 bytes

 

Floating point Data Types

 

float 4 bytes

double 8 bytes

long double 10 bytes

 

 

Using a simple variable as a call-by-reference parameter to a function

 

you are actually passing the address of the variable when you use call-by-reference.

And the compiler 'knows' the size in bytes of the variable,

because you also have to specify the data type in the prototype.

 

Using an entire array as a parameter to a function

 

Again, you are actually passing the address of the array variable (the start address). . .

but the compiler does not know the size in bytes of the entire array,

only the size in bytes of a single element.

 

 

This is why when you pass an array to a function,

 you should also pass an integer

which is the number of elements in the array.

 

int score[5];

int number_of_scores = 5;

 

fill_up(score, number_of_scores);

 

 

 

 


 

 


Changes made to an array passed to a function are permanent changes,

as the function is working on the actual array, not on a copy.

 

 

The const parameter modifier

 

 

If you don't want a function to change an array passed to it

you should then pass the array using the const parameter modifier.

The array parameter is then known as a constant array parameter:

 

The constant array parameter. . . can be used with any type of parameter,

but it is usually used only with array parameters

and call-by-reference parameters for classes.

 

 

 

 

3  Programming with Arrays

 

Most programs involving arrays commonly sort and search the elements of the array. . .

 

Sorting an Array

 

Sorting an array is the process of converting an unordered array into an ordered array.

 

 

 

Searching Arrays....

 

A very common task is to search an array for a given value.

For example, an array contains student numbers

for students enrolled in a course. . .

you may want to search the array to see

if a particular student number is contained in it.

 

Linear Search Animation

 

http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/LSearch.html

 

 

Searching an Array: search01.cpp

 

http://www.annedawson.com/

                   search01.cpp

                   search02.cpp

                   search03.cpp

 

http://www.annedawson.com/

                   array-10.cpp

 

 

Sorting Arrays. . .

http://www.annedawson.com/sort01.cpp

 

(see Powerpoint for graphics)

 

 

 

Sorting is the process of converting an unordered array

into an ordered array.

 

Sorting an Array of Integers

 

Algorithm for a Selection Sort

 

(see Powerpoint for graphics)

 

http://www.cosc.canterbury.ac.nz/people/mukundan/dsal/SSort.html

 

The Selection Sort: array-12.cpp

http://www.annedawson.com/array-12.cpp

 

 

Homework

 

Read the chapter on Arrays - the first three sections only.