130 likes | 150 Views
This text provides examples and explanations of how to declare and initialize arrays and vectors, how to modify array elements, and how to print arrays and vectors. It also covers common errors and introduces the concept of multidimensional arrays.
E N D
Declaring an Array with an Initializer List • constintarraySize = 10; • Prevent using “magic numbers”, because the program may include other 10s that is not array size. • int s[arraySize] = { 32, 27, 64 }; • The remaining elements are initialized to zero. • int s[10] = { }; // initialize all elements to 0 • int a[] = {1, 2, 3, 4, 5}; • The compiler will create a five-element array.
Fig. 7.9 Bar Chart Printing (P.308) constintarraySize = 11; int n[ arraySize ] = { 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 }; for ( inti = 0; i < arraySize; ++i ) { cout << i * 10 << "-" << ( i * 10 ) + 9 << ": "; // print bar of asterisks for ( int stars = 0; stars < n[ i ]; ++stars ) cout << '*'; cout << endl; // start a new line of output } // end outer for Grade distribution: 0-9: 10-19: 20-29: 30-39: 40-49: 50-59: 60-69: * 70-79: ** 80-89: **** 90-99: ** 100: * pronounced “n sub i”
Fig. 7.10 Dice Rolling (P.310) constintarraySize = 7; // ignore element zero int frequency[ arraySize ] = {}; // initialize elements to 0 srand( time( 0 ) ); // seed random number generator // roll die 6,000,000 times; use die value as frequency index for ( int roll = 1; roll <= 6000000; ++roll ) ++frequency[ 1 + rand() % 6 ]; // array elements as counters cout << "Face" << setw( 13 ) << "Frequency" << endl; // output each array element's value for ( int face = 1; face < arraySize; ++face ) cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ] << endl; Compare fig06_09.cpp on P.240
Common Programming Error 7.5 (P.312) • Referring to an element outside the array bounds is an execution-time logic error. It isn’t a syntax error. • Which implies that the compiler will not check this for you. int c[4] = {9, 10, 11, 12}; int b[4] = {5, 6, 7, 8}; int a[4] = {1, 2, 3, 4}; b[-1] = 99; b[4] = 44; cout << a[3] << '\t' << c[0] << endl; cout << &a[3] << '\t' << &b[-1] << endl; cout << &c[0] << '\t' << &b[4] << endl; b[-1] b[4]
Fig. 7.13 Passing Arrays to a Function (P.316) void modifyArray( int [], int ); void modifyArray( int b[], intsizeOfArray ) { // multiply each array element by 2 for ( int k = 0; k < sizeOfArray; ++k ) b[ k ] *= 2; } // end function modifyArray Even if you include the array size, it will be ignored by the compiler.
Fig. 7.14 const parameter (P.318) void tryToModifyArray( constint b[] ) { b[ 0 ] /= 2; } compilation error if you modify elements in this array
Fig. 7.15 Class Variable (P.319) class GradeBook { public: // constant -- number of students who took the test static constint students = 10; Keyword static in this variable declaration indicates that the data member is shared by all objects of the class. Actually, you can even access this “class variable” by GradeBook::student before any object is created. (P.324)
Fig. 7.21 Multidimensional Arrays (P.330) • void printArray( constint[][ 3 ] ); • The size of the first dimension is not required. • All subsequent dimension sizes are required. • constint rows = 2; • constint columns = 3; • int array1[ rows ][ columns ] = { { 1, 2, 3 }, { 4, 5, 6 } }; • You may omit the inner braces. Then it is equivalent to { 1, 2, 3, 4, 5, 6 }. • int array2[ rows ][ columns ] = { 1, 2, 3, 4, 5 }; • This is equivalent to { {1, 2, 3}, {4, 5, 0} }. • int array3[ rows ][ columns ] = { { 1, 2 }, { 4 } }; • This is equivalent to { {1, 2, 0}, {4, 0, 0} }.
Fig 7.25 vector (P.340) copy all elements in a vector to another vector #include <iostream> #include <vector> using std::vector; using std::cout; using std::endl; int main() { vector<int> a(7); vector<int> b(7); inti; for (i=0; i<a.size(); ++i) a.at(i) = i; b = a; for (i=0; i<b.size(); ++i) cout << b.at(i); cout << endl; return 0; } a 7-element array a vector knows its own size similar to a[i] = i;
Fig 7.25 try-catch (P.341) #include <iostream> #include <vector> #include <stdexcept> using std::out_of_range; using std::vector; using std::cout; using std::endl; int main() { vector<int> a(7); // automatically initialed to 0. try { cout << a.at(15) << endl; } catch (out_of_range &ex) { cout << "An exception occurred: " << ex.what() << endl; } cout << "Program ends normally." << endl; return 0; }