Presentation is loading. Please wait.

Presentation is loading. Please wait.

L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s.

Similar presentations


Presentation on theme: "L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s."— Presentation transcript:

1 l what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s call-by-reference? how is it different syntactically? l can expression be passed by reference? Previous Lecture Review 1

2 Arrays

3 l programmers sometimes need to manipulate variables of similar nature l aggregate construct – a construct that allows manipulation of multiple entities as a single element n array is an aggregate construct l array - a collection of similar variables with two names: n name of the array – the same for all variables in the array index (or subscript) - different for every variable, put in square brackets [] example: array score may have following variables: …, score[2], score[3], score[4], … l variables in the array are indexed variables or elements of the array l all variables in the array have the same type called base type of the array l 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 array size l 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! l non-array (regular) variable is scalar variable Array Definition 3

4 Array Terms Again array base type baseType id [ sizeExpession ] ; array name expression specifies number of array elements double x[100]; // indexes are 0 through 99 4

5 l indexed variables 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]; l index can be an expression: int student=2; score[student]=99; score[student+1]=100; cout << score[1] << ” ” << score[student]; l loops are ideal for arrays: for (int index=0; index < arraySize; ++index){ // do something with score[index] } Array Usage 5

6 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 with Loops Example 6

7 l array elements are placed in memory consequently: int a[10], myvar=20; l no range checking is done on index l referring to index that is not present in the array results in an out of range error n it is a logical error (bug/run-time error) with unpredictable consequences. these are both out-of-rage errors a[10]=55; a[myvar]=5; 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 7

8 l assigning values to the array variables at declaration is initalization int a[10]={0, 10, 20, 30, 40, 50, 60, 70, 80, 90}; l do not have to initialize all elements (rest hold zeroes): int a[10]={0, 10, 20}; l may skip array size at initialization (size computed to hold all values) int a[]={10,20,30}; // array size is three l using named constants for array size is good style: const int numStudents = 55; int score[numStudents]; Initializing Array 8

9 l 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? l entire array can be passed as argument always passed by reference (no & needed) n 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 l 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 Arrays and Functions 9

10 l array is always passed by reference n may lead to accidental value changes: run-time error const type modifier specifies that the parameter shall not be modified in the function n 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]; } l function prototype and head have to agree on type modifiers l function invocation is the same regardless of modifiers const Parameter Type Modifier 10

11 l what is an aggregate construct? l what is an array? l what is name of the array? index? l what is indexed variable? wlement of the array? scalar variable? l What is array size? l what is array’s base type? l how is array declared? l what number do indexes start from? l what is out-of-range error? is it a syntax error or a bug? l 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 []); l how is array initialized at declaration? Review Questions 11


Download ppt "L what is a void-function? l what is a predicate? how can a predicate be used? l what is program stack? function frame? l what’s call-by-value? l what’s."

Similar presentations


Ads by Google