-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAmplitudeOfArray.php
More file actions
41 lines (41 loc) · 1.08 KB
/
FindAmplitudeOfArray.php
File metadata and controls
41 lines (41 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
system("cls");
print("Input a count of elements in array:\r\n");
$nElems = intval(chop(fgets(STDIN)));
if ($nElems == 0)
{
echo "Empty arrays not allowed\r\n";
fgetc(STDIN);
exit(1);
}
$iNumbers = array();
srand();
echo("Generating array by filling " . $nElems . " elements\r\n");
for ($i = 0; $i < $nElems; $i++)
{
$nItem = rand(1, 100);
$iNumbers[] = $nItem;
printf("%d ", $iNumbers[$i]);
}
$iMin = $iNumbers[0];
$nMinIndex = 0;
$iMax = $iNumbers[0];
$nMaxIndex = 0;
for ($i = 1; $i < $nElems; $i++)
{
if($iNumbers[$i] > $iMax)
{
$iMax = $iNumbers[$i];
$nMaxIndex = $i;
}
if($iNumbers[$i] < $iMin)
{
$iMin = $iNumbers[$i];
$nMinIndex = $i;
}
}
printf("\r\nFound maximum element:%d with index %d\r\n", $iMax, $nMaxIndex);
printf("Found minimum element:%d with index %d\r\n", $iMin, $nMinIndex);
printf("Found amplitude: %d\r\n", $iMax - $iMin);
fgetc(STDIN);
?>