Download presentation
Presentation is loading. Please wait.
1
Previous Lecture Review
what is a void-function? what is a predicate? how can a predicate be used? what is program stack? function frame? what’s call-by-value? what’s call-by-reference? how are the two different syntactically? can expression be passed by reference?
2
aggregating varabiles
Arrays aggregating varabiles
3
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
4
expression specifies number
Array Terms Again baseType id [ sizeExpession ] ; array base type expression specifies number of array elements array name double x[100]; // indexes are 0 through 99
5
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] }
6
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; }
7
Arrays in Memory, Index Out of Range
-- a[4] a[5] a[6] a[3] a[0] a[2] a[8] a[9] a[7] a[1] 20 myvar other vars array 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;
8
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 hold zeroes): 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];
9
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
10
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
11
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 []);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.