lecture9

# Lecture9 - Wednesday November 3 Arrays,Part#2 rd Array Challenge int main(void cfw const int NUM_STUDS = 3 int grades[NUM_STUDS = cfw_0 int j=0

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

Wednesday, November 3 rd Arrays, Part #2 Searching Through Arrays Double Dimensional Arrays

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

View Full Document
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!
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.

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

View Full Document
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
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() …

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

View Full Document
Passing Arrays to Functions int sum(int array[], int elems) { 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.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/20/2009 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

### Page1 / 28

Lecture9 - Wednesday November 3 Arrays,Part#2 rd Array Challenge int main(void cfw const int NUM_STUDS = 3 int grades[NUM_STUDS = cfw_0 int j=0

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

View Full Document
Ask a homework question - tutors are online