1 / 36

Arrays

Arrays. One-Dimensional initialize & display Arrays as Arguments Part I. char, int, float, double. array, struct, union, class. Data Types. simple or atomic structured. * *. Structured Data Type - Array.

ferrol
Download Presentation

Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. char, int, float, double array, struct, union, class Data Types • simple or atomic • structured * *

  3. 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.

  4. 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];

  5. 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.

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

  7. 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 6.21 15.89 7.5 -45.7 angle3 --Mathematical notation * * *

  8. 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 * * * *

  9. 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

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

  11. [ 0 ] [ 1 ] [ 2 ] [ 3] @#$ @#$ Off Set intangle[4]; angle • memoryaddresses • starting address • off set by one unit • off set by two units • off set by three units

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

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

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

  15. 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 *

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

  17. 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 * *

  18. 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

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

  20. 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? *

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

  22. 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 ); • } *

  23. 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];

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

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

  26. Comparing Arrays as Arguments to Simple Data Types • 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[ ] Keeps function from changing values in the array

  27. 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 addressof the array or a pointer to the array.

  28. Slide 39 of 55

  29. Slide 40 of 55

  30. 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 const int MAXELS = 9; Use everywhere instead of the literal 9 *

  31. 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])

More Related