c++ arrays

Arrays are a collection of like data with a single name.
Elements of the array are accessed using the name and a unique array index.

          (cplusplus.com)


. simple arrays
Array ===== - fixed # of components that have same data type - components are accessed using their positions [index] in the array dataType arrayName[ intExp ]; //intExp evaluates to a positive integer Array index =========== - any expression that evaluates to non-negative integer - value of index is less than size of the array, n (0 to n-1) - base address of array is address of first array component - when passing an array as a param, use only its name . arrays are passed by reference only . function cannot return a value of the type array
Declaring an array ================= The statement int num[ 5 ]; // declares an array 'num' of 5 int components components: num[0], num[1], num[2], num[3], and num[4] Initialize arrays: int num[ ] = {223, 2, 77, 40, 0}; // 5 elements
Accessing Array Components ========================== Form of accessing array component is: arrayName[ index ] where index is a nonnegative integer or expression index value specifies the position in the array [ ] operator is the array subscripting operator - index starts at 0
Processing One-Dimensional Arrays ================================= Basic operations performed on a one-d array are: - Initialize - Input data - Output data stored in an array - Find the largest and/or smallest element Each operation requires ability to step through elements of array Easily accomplished by a loop ^^^^ int list[ 100 ]; //list is array of size 100 int i;

Understand these examples!! // Step through each element of the array for( i = 0; i< 100; i++ ) list[ i ] = i; // executed 100 times list[0] thru list[99] // Step through each element of the array for( i = 0; i< 100; i++ ) list[ i ] = i + 1; // executed 100 times list[0] thru list[99] // Step through each element of the array for( i = 0; i< 100; i++ ) list[ i ] = ( i * 2 ) + ( 5 * ( i - 1 ); // list[0] thru list[99] // Step through each element of the array for( i = 100; i> 0; i-- ) list[ i - 1 ] = i; // executed 100
// If processing list requires inputting data into list for( i = 0; i < 100; i++ ) cin >> list[ i ]; OR for( i = 0; i < 100; i++ ) dataFile >> list[ i ];

Understand these examples!! while loops work also! ======================= int a[1000]; // Declare an array of 1000 ints int n = 0; // Number of values in a. int sum = 0; // Start the total sum at 0. cin >> a[n]; while (a[n] != 0) { cin >> a[++n]; } // n will be index of the zero entered for (int i=0; i<n; i++) sum = sum + a[i]; // add next element to total

. code: arrays: collections of like data (homogeneous) including strings

C++ Primer Plus Chapter 4
C++ code examples 1-10


. code
/* create an int function called doubleIt - declares an uninitialized array of 100 ints - set the first element to a random number between 1-100 - set each subsequent element to twice the previous element - return last element */ #include <ctime> int doubleIt( void ) { int nums[ 100 ]; srand( time(0) ); nums[ 0 ] = rand( ) %100 + 1; for ( int i=1; i<100; i++ ) nums[ i ] = nums [ i-1 ] * 2; return nums[ 99 ]; }
/* create an int function called doubleIt2 - declares an uninitialized array of 100 ints - set the first element to the int parameter passed in - set each subsequent element to twice the previous element - return sum of all elements */ int doubleIt2( int num ) { int nums[ 100 ], sum; nums[ 0 ] = num; sum = nums[ 0 ]; for ( int i=1; i<100; i++ ) { nums[ i ] = nums [ i-1 ] * 2; sum += nums[ i ]; } return sum; }
/* create an int function called doubleIt3 - declares an uninitialized array of 100 ints - set the first 2 elements to the 2 int parameters passed in - set each subsequent element to the sum of the previous 2 elements - return sum of all elements */ int doubleIt3( int num1, int num2 ) { int nums[ 100 ], sum; nums[ 0 ] = num1; nums[ 1 ] = num2; sum = nums[ 0 ] + nums[ 1 ]; for ( int i=2; i<100; i++ ) { nums[ i ] = nums[ i-1 ] + nums[ i-2 ]; sum += nums[ i ]; } return sum; }
/* create a driver (main) - call each function - print result */ #include <iostream> using namespace std; int doubleIt( ); int doubleIt2( int ); int doubleIt3( int, int ); int main( ) { int result; result = doubleIt(); cout << result << endl; result = doubleIt2( 3 ); cout << result << endl; result = doubleIt3( 3, 7 ); cout << result << endl; return 0; }

. array init & parallel arrays
Array Initialization ==================== As with simple variables - Arrays can be initialized while they are being declared int num[ 4 ] = { 2, 77, 40, 0 }; // 4 elements
When initializing arrays while declaring them - Not necessary to specify the size of the array - Size of array determined by number of initial values int num[ ] = { 223, 2, 77, 40, 0 }; // 5 elements int list[ ] = { 5, 6, 3 }; // list[0]= 5, list[1] = 6, list[2] = 3 double sales[ ] = { 12.25, 16.90, 23, 45.68 }; // 4 elements Partial Array Initialization ============================ The statement int list[ 10 ] = { 0 }; // 10 items and inits ALL to zero This statement declares list to be an array of 10 components int list[ 10 ] = { 8, 5, 12 }; // init 3 items and inits rest to 0 The statement int list[ 25 ]= { 4, 7 }; // first items set to 4 and 7, rest are 0
Restrictions on Array Processing ================================ Assignment does NOT work with arrays C++ does not allow any aggregate operations on an array - Even if x and y are two arrays of the same type and size: int x[ 10 ], y[ 10 ]; y = x; // illegal
To copy one array to another array we must copy EACH component Can accomplish by a loop: ^^^^^^^^^^^^^^ int x[ 25 ], y[ 25 ]; ... for( j = 0; j< 25; j++ ) y[ j ] = x[ j ];// copies x to y Comparison of arrays Reading data into an arrays Printing the contents of an array - Must ALL be done element-by-element!

Parallel Arrays =============== - arrays are parallel if corresponding components hold related information - use a common index to address related info in different arrays Assume that related info is stored at the SAME index at each array - all nth locations have related info about: student ID, grade, phone # int studentId[ 50 ]; char courseGrade[ 50 ]; string phoneNum[ 50 ]; For example: print student info at array index #6: cout << studentId[6] << courseGrade[6] << phoneNum[6] << endl;
/* create an void function called par - declares 2 initialized arrays (0) size 100: ids (int) and bal (double) - int param indicates how many items to read in */ void parr( int n ) { int ids[ 100 ] = { 0 }; double bal[ 100 ] = { 0 }; for ( int i=0; i<n; i++ ) { cin >> ids[ i ]; cin >> bal[ i ]; } return; }

2d arrays (later) ========= - elements are arranged in a table form - to access an element of 2-d array, need pair of indices: row, column SHOW THE ARRAY AFTER EXECUTION!! void twoD( void ) { int arr2d[ 5 ][ 3 ]; // 5 row, 3 columns int i, j; for( i = 0; i < 5; i++) for( j = 0; j < 3; j++) arr2d[ i ][ j ] = i + j; return; } Draw array here: arr2d: 0 1 2 1 2 3 2 3 4 3 4 5 4 5 6
0 1 2 1 2 3 2 3 4 3 4 5 4 5 6 ^ ^ ^ ^ ^ row0 row1 row2 row3 row4

. code
/* create a function called big that returns the biggest number in an uninitialized array of 10,000 ints: algorithm?? */ #include <iostream> using namespace std; int big (void) { int nums[ 10000 ]; int biggest; biggest = nums[ 0 ]; for ( int i=1; i<10000; i++ ) if ( num[ i ] > biggest ) biggest = num[ i ]; return biggest; }

/* create a function called big that returns the biggest number in an uninitialized array of 10,000 ints AND prints the index of that number in the array algorithm?? */ #include <iostream> using namespace std; int big (void) { int nums[ 10000 ]; int bigNum, bigInd; bigNum = nums[ 0 ]; for ( int i=1; i<10000; i++ ) if ( num[ i ] > bigNum ) { bigNum = num[ i ]; bigInd = i; } cout << "Index of biggest element is " << bigInd << endl; return bigNum; } There is a subtle bug that might never show up. But it might - at the worst possible time! Since we NEVER init bigInd, if the zeroth element is the biggest, then bigInd will never be set. (but what might bigInt be anyway that might actually make it work again?) bigNum = nums[ 0 ]; bigInd = 0; TWO wrongs do sometimes make a right!

/* create a function called print that prints an uninitialized array of 10,000 ints backwards IF the number is bigger than zero return?? params?? algorithm?? */ #include <iostream> using namespace std; void print ( void ) { int nums[ 10000 ]; for ( int i=9999; i>=0; i-- ) if ( num[ i ] > 0 ) { cout << nums[ i ]; } return; }