Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1

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

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

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

5 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];

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

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

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

9 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; * *

10 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.)

11 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] @#$

12 Out-of-Bounds Array Index memory addresses angle[4] = 135; cout << angle[5]; [ 0 ] [ 1 ] [ 2 ] [ 3] @#$

13 Declare an Array Syntax type arrayName[index]; Example double dailyHigh[23] ; int quizGrade[132] ; char ASURiteID[5] ; *

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

15 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}; *

16 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”; *

17 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.21 angle sub one = 15.89 angle sub two = 7.5 angle sub three = -45.7 *

18 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];

19 Loading an Array double grade[10]; int index; for(index=0; index < 10; index++) { cout<<“Enter a grade “; cin >> grade[index]; }

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]) { max = temp[0]; for(index=0; index max) max = temp[index]; return (max); } *

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

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[ ];

24 Arrays as Arguments temp[ ] void find_max(int temp[ ]) { max = temp[0]; for(index = 1; index max) max = temp[index]; } temp[ ] double find_max(int temp[ ]) * return max;

25 Passing an Array/Element temp find_max(temp); // 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 words(cuplet); *

26 Passing an Array formal parameter formal parameter declaration fordeclaration for parameterPass-by-ValuePass-by-Reference simple var.int costint& price arrayimpossibleint myArray[ ] arrayconst int source[ ]

27 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

28 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

29 Two Dimensional Arrays a everything about one dimensional arrays applies a sometimes referred to as a table a has rows and columns ex. multiplication table

30 2-D Array Declaration Syntax [] [] dataType arrayName [ ConstIntExpression ] [ ConstIntExpression ]  Example int highTemp [52] [7]; double matrix [8] [8]; // checker board int roomSchedule [237] [13];

31 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} };

32 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};

33 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;

34 Accessing a 2-D Array a very similar to creating a multiplication table. a use nested loops. outer for loop was for the rows inner for loop was for the columns

35 Loading a 2-D Array a The Multiplication Table for (row =1; row <=10; row++) { cout <<setw(5)<<row<<" "; for for (column=1; column <= 10; column++) cout << setw(5)<< column*row; cout << endl; }

36 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]; }

37 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;

38 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 }

39 Displaying a 2-D Array Output 210 198 203 188 200 224 220 217 211 194 197 189

40 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])

41 Local vs. Global No difference

42 “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


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

Similar presentations


Ads by Google