100 likes | 124 Views
Learn about void vs. boolean functions, arrays, call-by-value vs. call-by-reference, array usage, loops with arrays, handling indexes, and more in programming.
E N D
Previous Lecture Review • what is a void-function? • what is a boolean function? • is it possible for a function to have no parameters? • what is the program stack? a function frame? • what is call-by-value? • what is call-by-reference? • can expressions be passed by reference? What about constants?
Array Definition • programmers sometimes need to manipulate variables of a similar nature • array - a list of similar variables with two names: • name of the array it is the same for all variables in the array • index (or subscript) - different for every variable, put in square brackets [] • example: the array score may have following the variables: …, score[2], score[3], score[4], … • variables in the array are indexed variables or elements of the array • all variables in the array have the same type called the base type of the array • the number of indexed variables is the size of the array • array is declared as follows: int score[5]; the number in brackets -5 is the size of the array • the indexes start from 0. The statement above declared the following variables: score[0], score[1], score[2], score[3], score[4] • note, score[5]is not there!
Array Terms Again baseType id [ sizeExpression ] ; arraybase type expressionspecifies number of array elements array name double X[100];// subscripts are 0 through 99 !
Array Usage • indexed variables can be used anywhere a regular variable can be: cin >> score[4] >> score[2]; max = score[4] + score[2]; score[4] = max; cout << score[2] << ” ” << score[4]; • index can be any 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 myarray[index] }
Array with Loops Example int main(){ int numbers[5]; // array of numbers cout << "Enter the numbers: "; for(int i=0; i < 5; ++i) cin >> numbers[i]; // finding the minimum int minimum=numbers[0]; // assume the first element for (int i=1; i < 5; ++i) // start from second if (minimum > numbers[i]) minimum=numbers[i]; cout << "The smallest number is: " << minimum << endl; }
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 Arrays in Memory Index out of Range • array elements are placed in memory consequently: int a[10], myvar=20; • no range checking is done on index • referring to index that is not present in the array results in an out of range error • it is a logical error (bug/run-time error) with unpredictable consequences. • these examples are both out-of-rage errors a[10]=55; a[myvar]=5;
Initializing Array • assigning values to the array variables at declaration is initalization int a[10]={0, 10, 20, 30, 40, 50, 60, 70, 80, 90}; • do not have to initialize all elements (rest hold arbitrary values): 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 global named constants for array size is good style: int score[NUM_STUDENTS];
Arrays and Functions • array elements can be passed as arguments to functions: int i=5, n, a[10]; myfunc(n); // ordinary variable as argument myfunc(a[3]); // element as argument myfunc(a[i]); // which element are we passing? • entire arrays can be passed as arguments • 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 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
const Parameter Type Modifier • array is always passed by reference • may lead to accidental value changes – run time error • The consttype modifier specifies that the parameter may not be modified in the function. • that is, it turns accidental value change into a syntactic error • this allows the compiler to detect it and alert the programmer void printArray(const int [], int); // prototype void printArray(const int a[], int size){ // definition a[0] = 33; // not allowed, syntax error!!! for(int i=0; i < size; ++i) cout << a[i]; } • function prototype and head have to agree on modifiers • function invocation is the same regardless of modifiers