CISC181 Introduction to Computer Science Dr. McCoy Lecture 11 October 5, 20094.1 Introduction4.2 ArraysSlide 4Slide 54.3 Declaring ArraysAn Array in Memory4.4 Examples Using Arraysfig04_03.cpp (1 of 2)fig04_03.cpp (2 of 2) fig04_03.cpp output (1 of 1)fig04_04.cpp (1 of 1)fig04_04.cpp output (1 of 1)Initializing an ArraySlide 14fig04_05.cpp (1 of 2)fig04_05.cpp (2 of 2) fig04_05.cpp output (1 of 1)fig04_06.cpp (1 of 1) fig04_06.cpp output (1 of 1)fig04_07.cpp (1 of 1) fig04_07.cpp output (1 of 1)fig04_08.cpp (1 of 1) fig04_08.cpp output (1 of 1)fig04_09.cpp (1 of 2)fig04_09.cpp (2 of 2) fig04_09.cpp output (1 of 1)fig04_10.cpp (1 of 2)fig04_11.cpp (1 of 2)fig04_11.cpp (2 of 2)fig04_11.cpp output (1 of 1)4.5 Passing Arrays to FunctionsSlide 27Slide 28fig04_14.cpp (1 of 3)fig04_14.cpp (2 of 3)fig04_14.cpp (3 of 3)fig04_14.cpp output (1 of 1)fig04_15.cpp (1 of 2)fig04_15.cpp (2 of 2) fig04_15.cpp output (1 of 1)Storing Strings in Character ArraysStrings in Character ArraysSlide 37Reading Character StringsSlide 39fig04_12.cpp (1 of 2)fig04_12.cpp (2 of 2) fig04_12.cpp output (1 of 1)1CISC181 Introduction to Computer ScienceDr. McCoyLecture 11October 5, 2009 2003 Prentice Hall, Inc. All rights reserved.24.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++) 2003 Prentice Hall, Inc. All rights reserved.34.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 cc[ 0 ], c[ 1 ] … c[ n - 1 ]–Nth element as position N-1 2003 Prentice Hall, Inc. All rights reserved.44.2 Arrays•Array elements like other variables–Assignment, printing for an integer array cc[ 0 ] = 3;cout << c[ 0 ];•Can perform operations inside subscriptc[ 5 – 2 ] same as c[3] 2003 Prentice Hall, Inc. All rights reserved.54.2 Arraysc[6]-4560721543-89062-31645378Name of array (Note that all elements of this array have the same name, c)c[0]c[1]c[2]c[3]c[11]c[10]c[9]c[8]c[7]c[5]c[4]Position number of the element within array c 2003 Prentice Hall, Inc. All rights reserved.64.3 Declaring Arrays•When declaring arrays, specify–Name–Type of array•Any data type–Number of elements–type arrayName[ arraySize ];int c[ 10 ]; // array of 10 integersfloat d[ 3284 ]; // array of 3284 floats•Declaring multiple arrays of same type–Use comma separated list, like regular variablesint b[ 100 ], x[ 27 ];An Array in Memory5-7Copyright © 2010 Pearson Addison-Wesley. All rights reserved. 2003 Prentice Hall, Inc. All rights reserved.84.4 Examples Using Arrays•Initializing arrays –For loop•Set each element–Initializer list•Specify each element when array declaredint 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 valueint n[ 5 ] = { 0 };–If array size omitted, initializers determine sizeint n[] = { 1, 2, 3, 4, 5 }; •5 initializers, therefore 5 element array 2003 Prentice Hall, Inc.All rights reserved.Outline9fig04_03.cpp(1 of 2)1 // Fig. 4.3: fig04_03.cpp2 // Initializing an array.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 #include <iomanip>9 10 using std::setw;11 12 int main()13 {14 int n[ 10 ]; // n is an array of 10 integers15 16 // initialize elements of array n to 0 17 for ( int i = 0; i < 10; i++ ) 18 n[ i ] = 0; // set element at location i to 019 20 cout << "Element" << setw( 13 ) << "Value" << endl;21 22 // output contents of array n in tabular format 23 for ( int j = 0; j < 10; j++ ) 24 cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;25 Declare a 10-element array of integers.Initialize array to 0 using a for loop. Note that the array has elements n[0] to n[9]. 2003 Prentice Hall, Inc.All rights reserved.Outline10fig04_03.cpp(2 of 2)fig04_03.cppoutput (1 of 1)26 return 0; // indicates successful termination27 28 } // end mainElement Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 2003 Prentice Hall, Inc.All rights reserved.Outline11fig04_04.cpp(1 of 1)1 // Fig. 4.4: fig04_04.cpp2 // Initializing an array with a declaration.3 #include <iostream>4 5 using std::cout;6 using std::endl;7 8 #include <iomanip>9 10 using std::setw;11 12 int main()13 {14 // use initializer list to initialize array n 15 int n[ 10 ] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };16 17 cout << "Element" << setw( 13 ) << "Value" << endl;18 19 // output contents of array n in tabular format20 for ( int i = 0; i < 10; i++ )21 cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl;22 23 return 0; // indicates successful termination24 25 } // end mainNote the use of the initializer list. 2003 Prentice Hall, Inc.All rights reserved.Outline12fig04_04.cppoutput (1 of 1)Element Value 0 32 1 27 2 64 3 18 4 95 5 14 6 90 7 70 8 60 9 3713Initializing an Array•Write a program that initializes a 20 integer array to 0’s. It then reads in an arbitrary number of elements (less than 20), and stores them in the array. The end of reading is indicated by -9999. Print out the contents of the array numbers read in. 2003 Prentice Hall, Inc. All rights reserved.144.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 2003 Prentice Hall, Inc.All rights reserved.Outline15fig04_05.cpp(1 of 2)1 // Fig. 4.5: fig04_05.cpp2 // Initialize array s to the even integers from 2 to 20.3 #include <iostream>4 5 using std::cout;6 using
View Full Document