I have homework for a basic scripting class and I'm stuck on this single problem. The problem is as follows:
"Use rand(0,1) to generate an array of 100 random floating numbers (between 0 and 1).
Then find the maximum values of the array and print out the maximum value AND its
index in the array.
For example, if floatArray[25] has the maximum value of 0.9989,
you need to print out message "floatArray[25] = 0.9989 is the maximum value in the array"
So far this is what I have come up with:
float $array[];
int $counterA = 0;
float $random;
while ($counterA <= 99) //assign 100 random numbers into an array.
{
$random = rand(0,1);
$array[$counterA] = $random;
$counterA ++;
}
int $counterB;
int $counterC = 0;
float $largestNumber = 0;
int $largestArray = 0;
for ($counterB=0; $counterB<=99; $counterB++)
{
if ($array[$counterC + 1] > $array[$counterC]) //compare two numbers
{
$largestArray = $counterC + 1; //store the numbers
$largestNumber = $array[$counterC + 1];
$counterC ++;
}
}
print ("The largest number is: " + $largestNumber + "\n");
print ("The largest array is: " + $largestArray + "\n");
The problem is that the results aren't correct. It only ever print the last few numbers that it compares, whether they are the highest or not.
Could anyone point out where I'm going wrong, or show me a better way to do this?
Btw, we are not allowed to use the 'sort' command 