arrays one-dimensional initialize & display arrays as arguments two-dimensional initialize &...
Post on 22-Dec-2015
256 views
TRANSCRIPT
Arrays
One-Dimensionalinitialize & display
Arrays as Arguments
Two-dimensional initialize & display
Part I
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
SyntaxdataType arrayName [[ ConstIntExpression ]]
The number of elements in an array is stated within square brackets, [ ][ ].
Examplesdouble 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
Exampleangle [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
a write the contents of an array element:cout << angle[2];
a assign values to an array element:cin >> angle[3];angle[6] = pow(axis,4);
a use it as a parameter:y = sqrt(angle[0]);
a use it in an expression:x = 2.5 * angle[1] + 64;
* * * *
Array Component Access
for(indexindex=0; index index < arraySize; indexindex++)myArray[indexindex] = 0.0;
100
* *
Zero out an entire array. (Initialize every element to 0.0.)
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];
[ 0 ]
[ 1 ]
[ 2 ]
[ 3]
@#$
@#$
Declare an Array
Syntaxtype arrayName[index];
Exampledouble dailyHigh[23] ;
int quizGrade[132] ;
char ASURiteID[5] ;
*
Initialize an Array Element
SyntaxarrayName[index] = value;
ExampledailyHigh[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};*
Initialize an Array
int scores[ ] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189};
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
Exampleangle [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[ ]temp[ ]){
max = temp[0];
for(index = 1; index < 30; index++)if (temp[index] > max)
max = temp[index];
}
double find_max(int temp[ ]temp[ ])
*
return max;
Passing an Array/Element
find_max(temptemp); // no [ ]
inventory(priceprice, quantityquantity, amountamount); // 3 arrays
words(cupletcuplet);
find_max(temp[3]temp[3]);
inventory(priceprice, quantity[4]quantity[4], amount[12]amount[12]);
words(cupletcuplet);
*
Passing an Array
formal parameterformal parameter formal parameterformal parameterdeclaration fordeclaration for declaration fordeclaration for
parameterparameter Pass-by-ValuePass-by-Value Pass-by-ReferencePass-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 Declarationint myArray [9];
Array Initializationint myArray [9 ] = { 9, 9, 9, 8, 7, 6, 5, 4,
3};
Array Element ReferencemyArray [7]; // value = 4 *
99
1-D Array Review
Arrays are always passed by reference, unless const added to the formal parameter.
Array in a Function Prototypevoid myFunction(int [9]);
Array in a Function CallmyFunction(myArray);
Array in a Function Headervoid myFunction(int anyArrayName[9])
optional
Two Dimensional Arrays
a everything about one dimensional arrays applies
a sometimes referred to as a table
a has rows and columnsex. multiplication table
2-D Array Declaration
Syntax
dataType arrayName [[ ConstIntExpression ]] [[ ConstIntExpression ]]
Example
int highTemp [52] [7];double matrix [8] [8]; // checker boardint 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
a very similar to creating a multiplication table.
a use nested loops.outer for loop was for the rowsinner for loop was for the columns
Loading a 2-D Array
a The Multiplication Table
for (row =1; row <=10; row++){
cout <<setw(5)<<row<<" ";forfor (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
}
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];char code[26][10];
obtain(code);obtain(code);
char obtain(char key [26][10])char obtain(char key [26][10])