Arrays

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.

An array is a data structure that contains finite set of indexed elements of the same type, united by the same name.

1D array is similar to a table of objects or primitive types, keyed by index.

Examples, 

1D Array: students

0 1 2 3 4
Diana Abai Malika Zhanna Ruslan

1D Array: marks

0 1 2 3 4
34 30 37 33 31

1D arrays in pseudocode:

students = ["Diana","Abai","Malika","Zhanna","Ruslan"]
marks = [34, 30, 37, 33, 31]
OUTPUT marks[3]              // output 33
OUTPUT students[0]           // output Diana
OUTPUT students[1], " - ", marks[1]    // output Abai - 30

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:

<?php
$students = array("Diana","Abai","Malika","Zhanna","Ruslan");   // array of five elements
$marks = array(34, 30, 37, 33, 31);
echo $marks[3]."<br>";               // output 33
echo $students[0]."<br>";            // output Diana
echo $students[1]." - ".$marks[1];     // output Abai - 30
?>

Output all elements of an Indexed Array students.

<?php
$students = array("Diana","Abai","Malika","Zhanna","Ruslan");
$arrlength = count($students);   // count() - Counts the number of elements in an array
for($x = 0; $x < $arrlength; $x++)  // $arrlength = 5 (in array five elements)
{
  echo $students[$x]."<br>";
}
?>

PHP Associative Arrays

Associative arrays are arrays that use named keys that you assign to them.

1D array result:

Diana Abai Malika Zhanna Ruslan
34 30 37 33 31

Example,

$result = array("Diana"=>34, "Abai"=>30, "Malika"=>37, "Zhanna"=>33, "Ruslan"=>31);

It means that

$result["Diana"] = 34

$result["Malika"] = 37

$result["Ruslan"] = 31

Output all elements of an Associative Array result.

<?php
$result = array("Diana"=>34, "Abai"=>30, "Malika"=>37, "Zhanna"=>33, "Ruslan"=>31);
foreach($result as $x => $x_value) // iterate each element of array by key
{
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>

Display:

Key=Diana, Value=34
Key=Abai, Value=30
Key=Malika, Value=37
Key=Zhanna, Value=33
Key=Ruslan, Value=31

Task. Determine who got 34 or more marks.

<?php
$result = array("Diana"=>34, "Abai"=>30, "Malika"=>37, "Zhanna"=>33, "Ruslan"=>31);
echo "Students who got 34 or more marks:<br>";
foreach($result as $x => $x_value) 
{
  if ($x_value >= 34)
      echo $x . "<br>";
}
?>

Some examples:

Convert string to array

$my_string="4 7 9 1 89";
$my_array = explode(" ", $my_string); //split a string into an array by spaces
$num = (int)($my_array[0]); // convert string to integer

Fill array of random numbers

$n = 10;
for($i=0; $i<$n; $i++){
    $arr[$i] = rand(1, 100); // assign an element of array random number
}

Output elements of array in line

for($i=0; $i<$n; $i++){
    print $arr[$i]." ";
}

Next line

print "<br>";

 


Questions:


Exercises:

Ex. 1 Define array elements

Ex. 2

Tasks on https://www.w3resource.com/php-exercises/php-array-exercises.php


Exam questions:


 

 

 

Категория: Programming languages | Добавил: bzfar77 (03.02.2021)
Просмотров: 4680 | Теги: Index, 2D, data structure, Element, matrix, Bound, Table, 1D, array | Рейтинг: 5.0/5
Всего комментариев: 0
avatar