1 / 13

Arrays

Arrays. Overview. Defining arrays Arrays with for-loops Passing arrays as arguments Constant arrays as parameters Projects with arrays and functions (Disposable kitchen utensils plants) Searching and sorting arrays. Array Definition.

Télécharger la présentation

Arrays

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

  2. Overview • Defining arrays • Arrays with for-loops • Passing arrays as arguments • Constant arrays as parameters • Projects with arrays and functions (Disposable kitchen utensils plants) • Searching and sorting arrays

  3. Array Definition • 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 [] • Variables in the array are indexed variables or elements of the array • All variables in the array have the same type called base type of the array • Number of indexed variables is the size of the array • Array declaration int score[5]; • Indexes start from 0 score[0], score[1], score[2], score[3], score[4] • score[5]is not there!

  4. Array terms again base_type id [ size_exp ] ; 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 < array_size; index++){ // do something with myarray[index] }

  6. Example of Array with Loops int main() { int i, score[5], max; cout << "Enter 5 scores:\n"; cin >> score[0]; max = score[0]; for (i=1; i < 5; i++) { cin >> score[i]; if (score[i] > max) max = score[i]; //max is the largest } cout << "The highest score is " << max << endl << ”the differences from the highest are:\n"; for (i = 0; i < 5; i++) cout << score[i] << " off by " << (max - score[i]) << 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 MemoryIndex out of Range, Initializing Array • 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 a logical error (bug) with unpredictable consequences a[10]=55; a[myvar]=5; • assigning values to the array variables at declaration is initalization int a[10]={0, 10, 20, 30, 40, 50, 60, 70, 80, 90}; • don’t have to initialize all elements: int a[10]={0, 10, 20}; • using global named constants for array size is good style: int score[NUM_STUDENTS];

  8. 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 • Example: void fill_up(int [], int); // prototype void fill_up(int a[], int size){ // definition cout << “Enter “ << size << “numbers:”; for(int i=0; i < size; i++) cin >> a[i]; } fill_up(score, 5); // invocation, note no brackets

  9. const Parameter Type Modifier • When function accepts array as parameter, const type modifier can be used with array • const specifies that the elements of the array are not modified in the function. This specification allows compiler to check for errors at time of compilation. If function is not intended to modify array use const • Example: void printarray(const int [], int); // prototype void printarray(const int a[], int size){ // def. 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

  10. Disposable kitchen utensils plants Example

  11. Input/Output for the program Items of each type foreach plant (4 plants, arbitrary number of types ) 245 567 -1 1567 2456 456 -1 3457 -1 6891 987 -1 total production for each plant, round to thousands, output bar graph Units produced in thousands of units: Plant #1 * Plant #2 **** Plant #3 *** Plant #4 ********

  12. Functions Used input functions // inputs data on plants void input_data(int a[], int last_plant_number); // inputs the number of units // produced by each department // at a plant, returns the sum void get_total (int& sum); processing functions // scales the values a down to // thousands void scale(int a[], int size); // returns number rounded to // the nearest integer. int round(double number); output functions // displays a bar graph on the number // for each value of a void graph(const int asterisk_count[], int last_plant_number); // prints n asterisks to the screen void print_asterisks(int n);

  13. Files used plants.h wrapper, tests input functions testinput.cpp plants.cpp input_data get_total scale round graph print_asterisks wrapper, tests scale functions testscale.cpp final main() function useplants.cpp

More Related