You are on page 1of 10

Arrays

aggregating varabiles
Array Terms
 aggregate construct – a construct that allows manipulation of multiple entities
as a single entity
 array - a collection of variables called (array) elements or indexed variables
 array is an aggregate construct
 elements have two names
 name of the array – the same for all elements of single array
 index (or subscript) - different for element, put in square brackets []
 example: array score may have following elements:
…, score[2], score[3], score[4], …

 (array) base type – type of the array elements


 all elements have the same type
 array size – number of elements
 array declaration: int score[5];
 the number in brackets: 5 is array size
 the indexes start from 0. Above statement declares the following variables:
score[0], score[1], score[2], score[3], score[4]
note, score[5] is not there!
 scalar variable – non array (regular) variable
2
Array Terms Again

baseType id [ sizeExpession ] ;

array expression
base type specifies number
array of array elements
name

double x[100]; // indexes are 0 through 99

3
Array Usage
 indexed variable can be used anywhere a scalar variable can be:
cin >> score[4] >> score[2];
max = score[4] + score[2];
score[4] = max;
cout << score[2] << ” ” << score[4];

 index can be an expression:


int student=2;
score[student]=99;
score[student+1]=100;
cout << score[1] << ” ” << score[student];

 loops are ideal for arrays:


for (int index=0; index < arraySize; ++index){
// do something with score[index]
}

4
Array with Loops Example
// finds minimum of array
int main(){
const int numNumbers=5;
int numbers[numNumbers]; // array of numbers

cout << "Enter the numbers: ";


for(int i=0; i < numNumbers; ++i)
cin >> numbers[i];

// finding the minimum


int minimum=numbers[0]; // assume the first element
for (int i=1; i < numNumbers; ++i) // start from second
if (minimum > numbers[i])
minimum=numbers[i];

cout << "The smallest number is: "


<< minimum << endl;
}
5
Arrays in Memory, Index Out of Range
array other vars
-- -- -- -- -- -- -- -- -- -- 20

a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] myvar

 array elements are placed in memory consequently:


int a[10], myvar=20;
 no range checking is done on index
 out-of-range error – referring to index that is not in array
 it is a logical error (bug/run-time error) with unpredictable
consequences.
 these are both out-of-range errors a[10]=55; a[myvar]=5;

6
Initializing Array
 what is initialization again?
 array elements can be initialized at once:
int a[10]={0, 10, 20, 30, 40, 50, 60, 70, 80, 90};

 do not have to initialize all elements (rest are assigned zeros):


int a[10]={0, 10, 20};

 may skip array size at initialization (size computed to hold all values)
int a[]={10,20,30}; // array size is three

 using named constants for array size is good style:


const int numStudents = 55;
int score[numStudents];

7
Arrays and Functions
 array elements can be passed as arguments to functions:
int i=5, n, a[10];
myfunc(n); // scalar variable as argument
myfunc(a[3]); // element as argument
myfunc(a[i]); // which element is passed?
 entire array can be passed as argument
 always passed by reference (no & needed)
 function does not know array size, it is usually passed as a separate variable

– alternatively – make size a global named constant;


using a literal constant is bad style
 example:
void fillUp(int [], int); // prototype
void fillUp(int a[], int size){ // definition
cout << ”Enter ” << size << ” numbers:”;
for(int i=0; i < size; ++i)
cin >> a[i];
}
fillUp(score, 5); // invocation, note no brackets
8
const Parameter Type Modifier
 array is always passed by reference
 may lead to accidental value changes: run-time error
 const type modifier specifies that the parameter shall not be modified in the
function
 that is, it turns accidental value change into compile-time error

void printArray(const int [], int); // prototype


void printArray(const int a[], int size){ // definition
a[0] = 33; // not allowed, compile-time error!!!
for(int i=0; i < size; ++i)
cout << a[i];
}

 function prototype and head have to agree on type modifiers


 function invocation is the same regardless of modifiers

9
Review Questions
 what is an aggregate construct?
 what is an array?
 what is name of the array? index?
 what is indexed variable? element of the array? scalar variable?
 what is array size?
 what is array’s base type?
 how is array declared?
 what number do indexes start from?
 what is out-of-range error? is it a syntax error or a bug?
 how is array initialized?
 can arrays be passed as arguments to functions? If yes by value or by
reference? If yes, how is size of array passed?
 what does const mean in the following declaration?
void myfunc(const int []);

10

You might also like