11.5.2.1 use the technical terms associated with arrays including upper and lower bounds 11.5.2.3 write program code using 1D and 2D arrays Arrays A data structure is a collection of elementary data types such as integer, real, boolean, char.
1D array is similar to a table of objects or primitive types, keyed by index. Examples, 1D Array: students
1D Array: marks
1D arrays in pseudocode:
Array processing n - length of array arr - array Input elements of array from the keyboard: FOR i = 0 TO n - 1 INPUT arr[i] endFOR Output elements of array: FOR i = 0 TO n - 1 OUTPUT arr[i] endFOR Determining the index of the minimum element of the array and its value: index = 0 // index of minimal element min = arr[index] // value of minimal element FOR i = 1 TO n - 1 IF min > arr[i] THEN min = arr[i] endFOR OUTPUT index, min Сalculating the sum of array elements: sum = 0 // start sum FOR i = 0 TO n - 1 sum = sum + arr[i] endFOR OUTPUT sum Сounting the number of even elements of an array: count = 0 // start count FOR i = 0 TO n - 1 IF arr[i] MOD 2 = 0 THEN count = count + 1 endFOR OUTPUT count Outputting array elements with odd indices: FOR i = 0 TO n - 1 IF i MOD 2 <> 0 THEN OUTPUT arr[i] endFOR PHP Indexed Array 1D arrays in PHP program:
Output all elements of an Indexed Array students.
PHP Associative Arrays Associative arrays are arrays that use named keys that you assign to them. 1D array result:
Example,
It means that $result["Diana"] = 34 $result["Malika"] = 37 $result["Ruslan"] = 31 Output all elements of an Associative Array result.
Display: Key=Diana, Value=34 Task. Determine who got 34 or more marks.
Some examples: Convert string to array$my_string="4 7 9 1 89"; Fill array of random numbers$n = 10; Output elements of array in linefor($i=0; $i<$n; $i++){ Next lineprint "<br>";
Questions: Exercises: Ex. 1 Define array elements Ex. 2 Tasks on https://www.w3resource.com/php-exercises/php-array-exercises.php Exam questions:
| |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
Просмотров: 4680 | | |
Всего комментариев: 0 | |