1 Lab 2 Arrays Outline 2 1 2
1 Lab 2 - Arrays Outline 2. 1 2. 2 2. 3 2. 4 2. 5 2. 6 2. 7 2. 8 2. 9 Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays Case Study: Computing Mean, Median and Mode Using Arrays Searching Arrays: Linear Search and Binary Search Multiple-Subscripted Arrays 2004 Benjamin X. Wang All rights reserved.
2 2. 1 Introduction • Arrays – Structures of related data items – Static entity (same size throughout program) • A few types – Pointer-based arrays (C-like) – Arrays as objects (C++) 2004 Benjamin X. Wang All rights reserved.
3 2. 2 Arrays • Array – Consecutive group of memory locations – Same name and type (int, char, etc. ) • To refer to an element – Specify array name and position number (index) – Format: arrayname[ position number ] – First element at position 0 • N-element array c c[ 0 ], c[ 1 ] … c[ n - 1 ] – Nth element as position N-1 2004 Benjamin X. Wang All rights reserved.
4 2. 2 Arrays • Array elements like other variables – Assignment, printing for an integer array c c[ 0 ] = 3; cout << c[ 0 ]; • Can perform operations inside subscript c[ 5 – 2 ] same as c[3] 2004 Benjamin X. Wang All rights reserved.
5 2. 2 Name that this same Arrays of array (Note all elements of array have the name, c) c[0] -45 c[1] 6 c[2] 0 c[3] 72 c[4] 1543 c[5] -89 c[6] 0 c[7] 62 c[8] -3 c[9] 1 c[10] 6453 c[11] 78 Position number of the element within array c 2004 Benjamin X. Wang All rights reserved.
6 2. 3 Declaring Arrays • When declaring arrays, specify – Name – Type of array • Any data type – Number of elements – type array. Name[ array. Size ]; int c[ 10 ]; // array of 10 integers float d[ 3284 ]; // array of 3284 floats • Declaring multiple arrays of same type – Use comma separated list, like regular variables int b[ 100 ], x[ 27 ]; 2004 Benjamin X. Wang All rights reserved.
7 2. 4 Examples Using Arrays • Initializing arrays – For loop • Set each element – Initializer list • Specify each element when array declared int n[ 5 ] = { 1, 2, 3, 4, 5 }; • If not enough initializers, rightmost elements 0 • If too many syntax error – To set every element to same value int n[ 5 ] = { 0 }; – If array size omitted, initializers determine size int n[] = { 1, 2, 3, 4, 5 }; • 5 initializers, therefore 5 element array 2004 Benjamin X. Wang All rights reserved.
1 2 3 // Fig. 2. 3: fig 02_03. cpp // Initializing an array. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <iomanip> 9 10 using std: : setw; 11 12 13 14 int main() { int n[ 10 ]; Outline Declare a 10 -element array of integers. array to 0 using a for // n is an array of Initialize 10 integers loop. Note that the array has n[0] to n[9]. elements of array n elements to 0 15 16 17 18 // initialize for ( int i = 0; i < 10; i++ ) n[ i ] = 0; // set element at location i to 0 19 20 cout << "Element" << setw( 13 ) << "Value" << endl; 21 22 23 24 // output contents of array n in tabular format for ( int j = 0; j < 10; j++ ) cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl; fig 02_03. cpp (1 of 2) 25 2004 Benjamin X. Wang All rights reserved. 8
26 27 28 return 0; // indicates successful termination Outline } // end main Element 0 1 2 3 4 5 6 7 8 9 Value 0 0 0 0 0 fig 02_03. cpp (2 of 2) fig 02_03. cpp output (1 of 1) 2004 Benjamin X. Wang All rights reserved. 9
1 2 3 // Fig. 2. 4: fig 02_04. cpp // Initializing an array with a declaration. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <iomanip> 9 10 using std: : setw; 11 12 13 14 15 16 17 Note the use of the initializer int main() list. { // use initializer list to initialize array n int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; fig 02_04. cpp (1 of 1) cout << "Element" << setw( 13 ) << "Value" << endl; 18 19 20 21 // output contents of array n in tabular format for ( int i = 0; i < 10; i++ ) cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl; 22 23 return 0; 24 25 Outline 10 // indicates successful termination } // end main 2004 Benjamin X. Wang All rights reserved.
Element 0 1 2 3 4 5 6 7 8 9 Value 32 27 64 18 95 14 90 70 60 37 Outline 11 fig 02_04. cpp output (1 of 1) 2004 Benjamin X. Wang All rights reserved.
12 2. 4 Examples Using Arrays • Array size – Can be specified with constant variable (const) • const int size = 20; – Constants cannot be changed – Constants must be initialized when declared – Also called named constants or read-only variables 2004 Benjamin X. Wang All rights reserved.
1 2 3 // Fig. 2. 5: fig 02_05. cpp // Initialize array s to the even integers from 2 to 20. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <iomanip> 9 10 using std: : setw; 11 12 13 14 15 16 17 Note use of const keyword. can int main() Only const variables { specify array sizes. // constant variable can be used to specify array size const int array. Size = 10; int s[ array. Size ]; // array s 18 19 20 for ( int i = 0; i < array. Size; s[ i ] = 2 + 2 * i; 21 22 cout << "Element" << setw( 13 ) 23 Outline 13 fig 02_05. cpp (1 of 2) The program becomes more scalable when we set the array has 10 elements size using a const variable. i++ ) // We setcan thechange valuesarray. Size, and all the loops will still work (otherwise, we’d have to << "Value"update << endl; every loop in the program). 2004 Benjamin X. Wang All rights reserved.
24 25 26 // output contents of array s in tabular format for ( int j = 0; j < array. Size; j++ ) cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl; 27 28 return 0; 29 30 Outline 14 // indicates successful termination } // end main Element 0 1 2 3 4 5 6 7 8 9 Value 2 4 6 8 10 12 14 16 18 20 fig 02_05. cpp (2 of 2) fig 02_05. cpp output (1 of 1) 2004 Benjamin X. Wang All rights reserved.
1 2 3 // Fig. 2. 6: fig 02_06. cpp // Using a properly initialized constant variable. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 9 10 int main() { const int x = 7; Proper initialization of const variable. // initialized constant variable 11 12 13 cout << "The value of constant variable x is: " << x << endl; 14 15 return 0; 16 17 Outline 15 // indicates successful termination fig 02_06. cpp (1 of 1) fig 02_06. cpp output (1 of 1) } // end main The value of constant variable x is: 7 2004 Benjamin X. Wang All rights reserved.
1 2 // Fig. 2. 7: fig 02_07. cpp // A const object must be initialized. 3 4 5 6 int main() { const int x; // Error: x Uninitialized const results in a syntax error. Attempting to modify the const is must be initialized another error. 7 8 x = 7; // Error: cannot modify a const variable 9 10 return 0; // indicates successful termination 11 12 Outline 16 } // end main d: Fig 02_07. cpp(6) : error C 2734: 'x' : const object must be initialized if not extern d: Fig 02_07. cpp(8) : error C 2166: l-value specifies const object fig 02_07. cpp (1 of 1) fig 02_07. cpp output (1 of 1) 2004 Benjamin X. Wang All rights reserved.
1 2 3 // Fig. 2. 8: fig 02_08. cpp // Compute the sum of the elements of the array. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 9 10 int main() { const int array. Size = 10; 11 12 int a[ array. Size ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 13 14 int total = 0; 15 16 17 18 19 20 21 22 23 24 // sum contents of array a for ( int i = 0; i < array. Size; i++ ) total += a[ i ]; Outline 17 fig 02_08. cpp (1 of 1) fig 02_08. cpp output (1 of 1) cout << "Total of array element values is " << total << endl; return 0; // indicates successful termination } // end main Total of array element values is 55 2004 Benjamin X. Wang All rights reserved.
1 2 3 // Fig. 2. 9: fig 02_09. cpp // Histogram printing program. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <iomanip> 9 10 using std: : setw; 11 12 13 14 15 int main() { const int array. Size = 10; int n[ array. Size ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 }; Outline 16 17 18 cout << "Element" << setw( 13 ) << "Value" << setw( 17 ) << "Histogram" << endl; 19 20 21 22 23 // for each element of array n, output a bar in histogram for ( int i = 0; i < array. Size; i++ ) { Prints asterisks corresponding cout << setw( 7 ) << i << setw( 13 ) to size of array element, << n[ i ] << setw( 9 ); 24 25 26 18 fig 02_09. cpp (1 of 2) n[i]. for ( int j = 0; j < n[ i ]; j++ ) cout << '*'; // print one bar 2004 Benjamin X. Wang All rights reserved.
27 28 cout << endl; // start next line of output 29 30 } // end outer for structure 31 32 return 0; 33 34 Outline 19 // indicates successful termination } // end main Element 0 1 2 3 4 5 6 7 8 9 Value Histogram 19 ********** 3 *** 15 ******** 7 ******* 11 ****** 9 ***** 13 ******* 5 ***** 17 ********* 1 * fig 02_09. cpp (2 of 2) fig 02_09. cpp output (1 of 1) 2004 Benjamin X. Wang All rights reserved.
1 2 3 // Fig. 2. 10: fig 02_10. cpp // Roll a six-sided die 6000 times. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <iomanip> 9 10 using std: : setw; 11 12 13 #include <cstdlib> #include <ctime> 14 15 16 17 18 int main() { const int array. Size = 7; int frequency[ array. Size ] = { 0 }; Outline fig 02_10. cpp (1 of 2) Remake of old program to roll dice. An array is used instead of 6 regular variables, and the proper element can be updated easily (without needing a generator switch). 19 20 srand( time( 0 ) ); 21 22 23 24 25 // roll die 6000 times This creates a number for ( int roll = 1; roll <= 6000; roll++ ) between 1 and 6, which ++frequency[ 1 + rand() % 6 ]; // replaces 20 -line switch determines the index of // of Fig. 3. 8 // seed random-number 20 frequency[] that should be incremented. 2004 Benjamin X. Wang All rights reserved.
26 27 cout << "Face" << setw( 13 ) << "Frequency" << endl; 28 29 30 31 32 // output frequency elements 1 -6 in tabular format for ( int face = 1; face < array. Size; face++ ) cout << setw( 4 ) << face << setw( 13 ) << frequency[ face ] << endl; 33 34 return 0; 35 36 // indicates successful termination } // end main Face 1 2 3 4 5 6 Outline 21 Frequency 1003 1004 999 980 1013 1001 fig 02_10. cpp (2 of 2) fig 02_10. cpp output (1 of 1) 2004 Benjamin X. Wang All rights reserved.
1 2 3 // Fig. 2. 11: fig 02_11. cpp // Student poll program. #include <iostream> 4 5 6 using std: : cout; using std: : endl; 7 8 #include <iomanip> 9 10 using std: : setw; 11 12 13 14 15 16 int main() { // define array sizes const int response. Size = 40; const int frequency. Size = 11; Outline 22 fig 02_11. cpp (1 of 2) // size of array responses // size of array frequency 17 18 19 20 21 // place survey responses in array responses int responses[ response. Size ] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 }; 22 23 24 // initialize frequency counters to 0 int frequency[ frequency. Size ] = { 0 }; 25 2004 Benjamin X. Wang All rights reserved.
26 27 28 29 30 // for each answer, select value of an element of array // responses and use that value as subscript in array // frequency to determine element to increment for ( int answer = 0; answer < response. Size; answer++ ) ++frequency[ responses[answer] ]; 31 32 33 // display results the rating (from 1 to 10). cout << "Rating" << setw( 17 ) << "Frequency" << endl; 34 35 36 37 38 // output frequencies in tabular for ( int rating = 1; rating < frequency. Size; rating++ ) cout << setw( 6 ) << rating << setw( 17 ) << frequency[ rating ] << endl; 39 40 return 0; 41 42 Outline 23 responses[answer] is This determines the index in frequency[] to increment. format fig 02_11. cpp (2 of 2) // indicates successful termination } // end main 2004 Benjamin X. Wang All rights reserved.
Rating 1 2 3 4 5 6 7 8 9 10 Frequency 2 2 5 11 5 7 1 3 Outline 24 fig 02_11. cpp output (1 of 1) 2004 Benjamin X. Wang All rights reserved.
25 2. 4 Examples Using Arrays • Strings (more in lab 5) – Arrays of characters – All strings end with null ('