{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture9 - Wednesday November 3 Arrays,Part#2 rd Array...

Info icon This 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
Image of page 1

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

View Full Document Right 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!
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++) cout << names[j] << “\n”; In this case, we must  make our array big  enough to hold the  maximum expected  number of items.
Image of page 3

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

View Full Document Right 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++) cout << names[j] << “\n”; This variable will be less  than or equal to the  maximum number of  items. Not Entirely Full Arrays
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() …
Image of page 5

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

View Full Document Right 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[ ]   This is how you define a    formal array parameter.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern