Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays One-Dimensional initialize & display Arrays as Arguments Part I.

Similar presentations


Presentation on theme: "Arrays One-Dimensional initialize & display Arrays as Arguments Part I."— Presentation transcript:

1

2 Arrays One-Dimensional initialize & display Arrays as Arguments Part I

3 Data Types a simple or atomic a structured * char, int, float, double array, struct, union, class

4

5 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. An homogeneous aggregate of elements Each element is referred to as an indexed or subscripted variable.

6

7

8

9

10 Array Declaration [] Syntax dataType arrayName [ ConstIntExpression ] The number of elements in an array is [ ] stated within square brackets, [ ]. Examples double angle [4];const int POLY = 8; int testScore [12];double angle [POLY]; char password [8];

11 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 elements occupy contiguous memory locations.

12 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. A[0] => add zero to the address of A (1 st element) A[3] => add (3 * width of element) to the address of A (4 th element)

13 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.21 angle sub one = 15.89 angle sub two = 7.5 angle sub three = -45.7 * * * 6.21 15.89 7.5 -45.7 angle 3 -- Mathematical notation

14 Using Array Elements  write the contents of an array element: cout << angle[2]; // the third element  assign values to an array element: cin >> angle[3]; // the fourth element angle[2] = pow(axis,4); // the third element  use it as a parameter: y = sqrt(angle[0]); // the first element  use it in an expression: x = 2.5 * angle[1] + 64; // the second element * *

15 Declare an Array float myArray[100]; Want to reset myArray to zeros myArray = 0.0; // works in some languages In C++, the array name is a pointer to an address in memory myArray is an address in memory An attempt to assign a float to a pointer to a float

16 Array Component Access indexindex index index for(index=0; index < arraySize; index++) myArray[index] = 0.0; 100 * Zero out an entire array. (Initialize every element to 0.0.) For loops were “made for” arrays You always know how many times the loop will execute – once for each element

17 Off Set memory addresses starting address off set by one unit off set by two units off set by three units [ 0 ] [ 1 ] [ 2 ] [ 3] @#$ angle int angle[4];

18 Out-of-Bounds Array Index memory addresses angle[4] = 135; cout << angle[5]; [ 0 ] [ 1 ] [ 2 ] [ 3] @#$ angle grade score Overwrites value in memory without warning!

19 Declare an Array Syntax type arrayName[index]; Example double dailyHigh[31] ; int quizGrade[29] ; char YSUID[9] ; *

20 Initialize an Array Element Syntax arrayName[index] = value; Example dailyHigh[18] = 16.7; quizGrade[2] = 15; YSUID[3] = ‘7’; *

21 Initialize an Array at Declaration Time double angle[4] = {16.21, 15.89, 7.5, -45.7}; double convert[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}; // fill with zeros *

22 Initialize an Array - Variations int scores[ ] = {210, 198, 203, 188, 200, 224, 220, 217, 211, 194, 197, 189}; char name1[4] = {‘Z’, ‘o’, ‘l’, ‘a’}; char name2[4] = “Zola”;// no braces or, char phrase [ ] = “Hello World”; * Space for 12 integers Compiler will allocate 12 bytes Will not compile: “init string too long” 5

23 Character Arrays - \0 char name1[4] = {‘Z’, ‘o’, ‘l’, ‘a’}; char name2[5] = “Zola”;// no braces or, needs 5 slots to have space for the end of string character, \0 char phrase [ ] = “Hello World”; * Compiler will allocate 12 bytes

24 Sequencing Through an Array Use the for statement to sequence through an array. Assume an array with 7 grades in it… Total the contents of an array: sum = 0; for (index=0; index < 7; index++) sum += grades[index]; char name1[4] = {‘Z’, ‘o’, ‘l’, ‘a’}; Print out an array for (i = 0, i < 4, i++) cout << name1[i];// prints Zola

25 Loading an Array – with cin double grade[10]; int index; for(index=0; index < 10; index++) { cout<<“Enter a grade “; cin >> grade[index]; } 75 85 65 95 77 88 68 93 59 90 grade 0 9

26 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? *

27 Finding the Maximum Value double find_max(int temp[30]) { int max = temp[0]; //only max seen thus far for(int index=1; index max) max = temp[index]; return (max); } * If the current temp[ ] is bigger than the max seen thus far. Remember this one!

28 Finding the Minimum Value double find_min(int temp[30]) { int min = temp[0]; // minimum thus far < for(int index = 1; index < 30; index++) if (temp[index] < min) min = temp[index]; return ( min ); } *

29 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[ ]; for (i=0;i<5;i++) y[i] = x[i];

30 Arrays as Arguments temp[ ] double find_max(int temp[ ]) { max = temp[0]; for(index = 1; index max) max = temp[index]; return max; } temp[ ] double find_max(int temp[ ]) * Leave blank or use constant

31 Passing an Array or Elements temp find_max(temp); // whole array, no [ ] pricequantityamount inventory(price, quantity, amount); // 3 arrays cuplet words(cuplet); temp[3] find_max(temp[3]); pricequantity[4]amount[12] inventory(price, quantity[4], amount[12]); cuplet[0] words(cuplet[0]); * Elements of an array

32 Comparing Arrays as Arguments to Simple Data Types formal parameter formal parameter declaration fordeclaration for parameterPass-by-ValuePass-by-Reference simple var.int costint& price arrayimpossibleint myArray[ ] arrayconst int source[ ] Keeps function from changing values in the array

33 Passing an Array Element Follows the rules for the type of an element in the array int cntA[1111]; cntA[33] is an int and is treated as any other int when used as an actual argument. cntA refers to the entire array. It is the address of the array or a pointer to the array.

34 Slide 39 of 55

35 Slide 40 of 55

36 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 [3];// value = 8 * 9 const int MAXELS = 9; Use everywhere instead of the literal

37 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]) optional


Download ppt "Arrays One-Dimensional initialize & display Arrays as Arguments Part I."

Similar presentations


Ads by Google