1 / 10

what is a void-function? what is a boolean function?

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?.

burnetteb
Télécharger la présentation

what is a void-function? what is a boolean function?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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?

  2. Arrays

  3. 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!

  4. Array Terms Again baseType id [ sizeExpression ] ; arraybase type expressionspecifies number of array elements array name double X[100];// subscripts are 0 through 99 !

  5. 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] }

  6. 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; }

  7. 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;

  8. 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];

  9. 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

  10. 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

More Related