410 likes | 416 Views
Learn how to initialize and display one-dimensional and two-dimensional arrays in various data types such as char, int, float, and double. Explore array declaration, element access, component access, and out-of-bounds index handling. Implement array element operations, including accessing, assigning values, using as parameters, and using in expressions. Discover how to sequence through arrays using for loops and find the maximum and minimum values in an array. Also, learn about passing arrays and elements as arguments.
E N D
Arrays One-Dimensional initialize & display Arrays as Arguments Two-dimensional initialize & display Part I
char, int, float, double array, struct, union, class Data Types • simple or atomic • structured * *
Structured Data Type - Array • An array is a collection of data storage locations, each of which holds the same type of data. Each storage location is called an element of the array. • Each element is referred to as an indexed or subscripted variable.
Array Declaration • Syntax dataType arrayName [ ConstIntExpression ] • The number of elements in an array is • stated within square brackets, [ ]. • Examples double angle [4]; constant POLY = 8; int testScore [12]; double angle [POLY]; char password [8];
Array Storage • Each array has sufficient memory reserved to hold the number of data items of the given type. • Initializing an array sets up the address of the first element. Addresses of all other elements are offsets from the starting address.
Array Element Access • Syntax • arrayName [ indexExpression ] • Program access to each of the array elements is by referring to an offset from the array name. Array elements are counted beginning with zero.
Array Element Access • double angle[4]; // declaration • Example angle [0] = 6.21; angle [1] = 15.89; angle [2] = 7.5; angle [3] = -45.7; angle sub zero = 6.21angle sub one = 15.89angle sub two = 7.5angle sub three = -45.7 * * *
Using Array Elements • write the contents of an array element: cout << angle[2]; • assign values to an array element: cin >> angle[3]; angle[6] = pow(axis,4); • use it as a parameter: y = sqrt(angle[0]); • use it in an expression: x = 2.5 * angle[1] + 64; * * * *
Array Component Access • for(index=0; index < arraySize; index++) myArray[index] = 0.0; Zero out an entire array. (Initialize every element to 0.0.) 100 * *
[ 0 ] [ 1 ] [ 2 ] [ 3] @#$ @#$ Off Set • memoryaddresses • starting address • off set by one unit • off set by two units • off set by three units
[ 0 ] [ 1 ] [ 2 ] [ 3] @#$ @#$ Out-of-Bounds Array Index • memoryaddresses • angle[4] = 135; cout << angle[5];
Declare an Array • Syntax type arrayName[index]; Example double dailyHigh[23] ; int quizGrade[132] ; char ASURiteID[5] ; *
Initialize an Array Element • Syntax arrayName[index] = value; Example dailyHigh[18] = 16.7; quizGrade[2] = 15; ASURiteID[3] = ‘d’; *
Initialize an Array double angle[4] = {16.21, 15.89, 7.5, -45.7}; double ATtoCG[5] = {.64, .89, .76, .83, .65}; int scores[12] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189}; double iona[8] = {0.0}; *
int scores[ ] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189}; Initialize an Array char name[4] = {‘Z’, ‘o’, ‘l’, ‘a’}; char name[4] = “Zola”; // no braces or , char phrase [ ] = “Hello World”; * *
Initialize an Array double angle[4]; // declaration Example angle [0] = 6.21; angle [1] = 15.89; angle [2] = 7.5; angle [3] = -45.7; angle sub zero = 6.21angle sub one = 15.89angle sub two = 7.5angle sub three = -45.7 *
Sequencing Through an Array • Use the for statement to sequence through an array. • Total the contents of an array: sum = 0; for(index=0; index < 7; index++) sum = sum + grades[index];
Loading an Array • double grade[10]; • int index; • for(index=0; index < 10; index++){ cout<<“Enter a grade “; • cin >> grade[index]; • }
Finding the Max/Min Value • Set the max or min to element zero. • Use a for loop to cycle through the array. • Compare each element to the max/min. • If necessary, assign a new value to max/min. How would you do this? *
Finding the Maximum Value • double find_max(int temp[30]) • { • max = temp[0]; • for(index=0; index < 30; index++) if (temp[index] > max) max = temp[index]; • return (max); • } *
Finding the Minimum Value • double find_min(int temp[30]) • { • min = temp[0]; • for(index = 1; index < 30; index++) if (temp[index] <min) min = temp[index]; • return ( min ); • } *
Aggregate Assignment - NOT! • There are no aggregate assignments with arrays. That is, you may not assign one array to another. • int x[5] = {11, 22, 33, 44, 55}; • int y[5]; • y = x; y[ ] = x[ ];
Arrays as Arguments • void find_max(int temp[ ]) • { • max = temp[0]; • for(index = 1; index < 30; index++) if (temp[index] > max) max = temp[index]; • } double find_max(int temp[ ]) return max; *
Passing an Array/Element • find_max(temp); // no [ ] • inventory(price, quantity, amount); // 3 arrays • words(cuplet); • find_max(temp[3]); • inventory(price, quantity[4], amount[12]); • words(cuplet); *
Passing an Array • formal parameter formal parameter declaration for declaration forparameterPass-by-Value Pass-by-Reference • simple var. int cost int& pricearray impossible int myArray[ ] • array const int source[ ]
1-D Array Review • An array is a structured data type • 1st element is “arrayName subzero” • Array Declaration int myArray [9]; • Array Initialization int myArray [9 ] = { 9, 9, 9, 8, 7, 6, 5, 4, 3}; • Array Element Reference myArray [7]; // value = 4 9 *
optional 1-D Array Review • Arrays are always passed by reference, unless const added to the formal parameter. • Array in a Function Prototype • void myFunction(int [9]); • Array in a Function Call myFunction(myArray); • Array in a Function Header void myFunction(int anyArrayName[9])
Two Dimensional Arrays • everything about one dimensional arrays applies • sometimes referred to as a table • has rows and columns ex. multiplication table
2-D Array Declaration • Syntax • dataType arrayName [ ConstIntExpression ][ ConstIntExpression ]l l l • Example • int highTemp [52] [7]; double matrix [8] [8]; // checker board int roomSchedule [237] [13];
2-D Array Initialization • int nums [3] [2] = {7, 4, 1, 8, 5, 2} • int roomsPSF[3] [7] = { {17, 18,19, 110, 111, 112, 113}, {27, 28, 29, 210, 211,212, 213}, {37, 38, 39, 310, 311, 312, 313} };
2-D Array Initialization • double ATtoCG[2] [3] = {.74, .79, .76, .83, .65, .89}; • double ATtoCG[2] [3] = { {.74, .79, .76}, {.83, .65, .89} }; • int scores[4] [3] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189};
2-D Array Initialization • You can assign values to individual elements: • ATtoGC [0] [0] = 0.74; ATtoGC [0] [1] = 0.79; ATtoGC [0] [2] = 0.76; ATtoGC [1] [0] = 0.83; ATtoGC [1] [1] = 0.65; ATtoCG [1] [2] = 0.89; *
Accessing a 2-D Array • very similar to creating a multiplication table. • use nested loops. outer for loop was for the rows inner for loop was for the columns
Loading a 2-D Array • The Multiplication Table • for (row =1; row <=10; row++) • { • cout <<setw(5)<<row<<" "; • for (column=1; column <= 10; column++) • cout << setw(5)<< column*row; • cout << endl; • }
Loading a 2-D Array • int scores [4][3]; • for(row=0; row<4; row++) • for(col=0; col<3; col++) • { • cout<<"Enter the value :"; • cin>>scores[row][col]; • }
Loading a 2-D Array • scores [0][0] [2][0] [0][1] [2][1] [0][2] [2][2] • [1][0] [3][0] [1][1] [3][1] [1][2] [3][2] [row][col] = somevalue;
Displaying a 2-D Array • int scores [4] [3]; • for(row=0; row<4; row++) • { • for(col=0; col<3; col++) • cout << setw(6) << scores[row][col]; • cout << endl; // new line for each row • }
Displaying a 2-D Array • Output • 210 198 203 188 200 224 220 217 211 194 197 189
Functions and 2-D Arrays • int test[7][19]; // declaration • find_min(test); // function call • int find_min(int num [7][19]) // funct. header char code[26][10]; obtain(code); char obtain(char key [26][10]) * * * *
Local vs. Global • No difference
“One might as well say he has sold when no one has bought as to say he has taught when no one has learned.” John Dewey