lecture9%2Dpost

lecture9%2Dpost - Wednesday, November 3 Arrays,Part#2...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Wednesday, November 3 rd Arrays, Part #2 Searching Through Arrays Double Dimensional Arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Array Challenge int main(void) { const int NUM_STUDS = 3; int grades[NUM_STUDS] = {0}; int j=0; while(j++<NUM_STUDS) { cout << “Enter grade “ << j << “: “; cin >> grades[j]; } int avg; for (j=0;j<NUM_STUDS;j++) avg += grades[j]; avg /= NUM_STUDS; cout << “The average is: “ << avg; } Find all  of the problems  in this program. grades          0 0 0          j           0 < 3  1 Enter grade 1: 90 90 0 1 2 1 < 3  2 Enter grade 2: 80 80 2 < 3  3 Enter grade 3: 30 30 BOOM!
Background image of page 2
Not Entirely Full Arrays Sometimes we don’t  know how many items  we want to store in our  array ahead of time. int main() { const int MAX_CS_PROFS = 30; // MAX! int actual_profs = 0; string names[ MAX_CS_PROFS ]; while ( actual_profs < MAX_CS_PROFS ) { cout << “Enter name (blank=done): “; cin >> names[ actual_profs ]; if (names[ actual_profs ] == “”) break; ++ actual_profs ; // we just added one } // print out as many names were entered for (int j=0;j< actual_profs ;j++) In this case, we must  make our array big  enough to hold the  maximum expected  number of items.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
You then use another  variable to indicate how  many items are  actually   in the array. int main() { const int MAX_CS_PROFS = 30; // MAX! int actual_profs = 0; string names[ MAX_CS_PROFS ]; while ( actual_profs < MAX_CS_PROFS ) { cout << “Enter name (blank=done): “; cin >> names[ actual_profs ]; if (names[ actual_profs ] == “”) break; ++ actual_profs ; // we just added one } // print out as many names were entered for (int j=0;j< actual_profs ;j++) This variable will be less  than or equal to the  maximum number of  items. Not Entirely Full Arrays
Background image of page 4
Global and Static Arrays Static  and  global  arrays behave  exactly like regular  static  and  global   variables :  1. If they’re not explicitly initialized by  the user, all elements are set to 0. 2. Statics are initialized only once, the  first time the function is called. 3. Static or global arrays maintain their  value between function calls. 4. Static arrays are only visible to the  function where they’re defined.  #include <iostream> int ages[10]; // global array void print_ages(void) { for (int i=0;i<10;i++) cout << ages[i] << endl; } void record_rainfall(int amt) { static int rain[52]; // static // array! static int week = 0; rain[week++] = amt; } main() …
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Passing Arrays to Functions int sum(int array[], int elems) { int i, total = 0; for (i=0;i<elems;i++) total += array[i]; return(total); } void main(void) { int money[5] = {40,30, 10, 10, 10}; int result; result = sum(money,5); cout << result << endl; } The general form is:     type varname[ ]    e.g.      int arrayname[ ]  
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 28

lecture9%2Dpost - Wednesday, November 3 Arrays,Part#2...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online