CS 107 - 7/6/15 Code: 5 5 6 6 used: y x x y Guess: 6 5 6 5 used: y x x y Correct Positions: 2 Incorrect Positions: 2 Code Copy: 15 15 16 16 Curr Guess: 26 25 26 25 Overflow: When we try to represent a number that falls outside of the range of possible values. IF we stored integers using 16 bits of data. We can 2^16 different values, from the range -32,768 to +32,767 32767 + 1 ===> results in the bit pattern for -32768 Out of Bounds: We attempt to access a location in an array that doesn't exist int array1[100]; // valid supscripts are in the range // from 0 to 99 array1[125] = 0; // attempt to store the value // of zero outside of the allocated // space for array1 Input Stream State Error int value1; int value2; cin >> value1; // if the user enters 10.5 cin >> value2; // the input given does no meet the // requirements for integer input // // This sets the internal state of the // input stream to an error; however, // the user/program is never told this. Arrays are not always full. int array3[526]; More arrays have two additional values associated with them Max Size: the maximum number of values the array can hold Curr Size: the current number of values held in the array Example: const int MAX = 600; int scores[MAX]; int currSize = 0; // read in values and store them into the array cout << "Enter positive integer values to store in the array" << ", (Enter a negative value to quit): " << endl; int input; cin >> input; while ( ( input >= 0 ) && (currSize < MAX) ) { scores [currSize] = input; currSize++; cin >> input; } // to calculate the average of the scores int total = 0; int i; for ( i = 0 ; i < currSize ; i = i + 1 ) { total = total + scores[i]; } double average = ((double) total)/ currSize; Another version of the loop to read in value could be: int input; cin >> input; while ( input >= 0 ) // loop while input is >= 0 { if ( currSize >= MAX ) // end loop if array is full break; scores [currSize] = input; currSize++; cin >> input; } The above code highlights the use of the break statement. In this case, break terminate the current loop being executed by the program. For the Exam on Friday 7/10/15 2 sections on the exam First Section: 26 multiple choice questions Answers a-e. Most often answer e will be: none of the above. 2 points per question Second Section: 3 write code questions 16 points each. "Write the C++ code to do X. Be sure to declare all variables used in your code." Chapter 8 - Functions A section of code that gets "called" from another section of code. When a function ends, it "returns" back to the section of code where it was called. Functions allow for organization of code "modularization". Functions also allow for the same algorithm to be performed using different sets of data. The different sets of data get indicated via the parameters of the function. For example: calculate the area of a circle a = PI * r * r ; double circleArea ( double r ) { return ( 3.14159 * r * r ); } void main() { double c1, c2; double area1, area2; area1 = circleArea (c1); area2 = circleArea (c2);