Jump to content

find absolute smallest value (close to zero)


nestaho

Recommended Posts

// there is a problem in the for loop. I am an ameteur, please help me to correct and give explanation

 

 

 

fscanf(STDIN, "%d",

    $n // the number of temperatures to analyse

);

 

$minTemp = -273;

$maxTemp = 5526;

 

$inputs = fgets(STDIN);

$inputs = explode(" ",$inputs);

 

$near = $inputs[0];

 

 

for ($i = 0; $i < $n; $i++)

{  

 if (abs($near) <= abs($inputs[$i+1]) {

     $near = abs($inputs[$i+1])

     

    }

echo($near);

}

Link to comment
Share on other sites

I am a bit confused actually

 

I wanna write

if abs($near) is smaller than the absolute of next number

then abs($near) stay the same

otherwise if abs($near) is larger than the absolute of next number

the abs($near) change to next number

Link to comment
Share on other sites

if abs($near) is smaller than the absolute of next number

then abs($near) stay the same

In other words, don't do anything.

 

otherwise if abs($near) is larger than the absolute of next number

the abs($near) change to next number

This is the one to focus on. How does this logic compare to what you've written in the code?
Link to comment
Share on other sites

CAN U REWRITE THE CODE FOR ME? PLEASE...

I rewrite code as follow, with following error

 

Parse error: syntax error, unexpected ';' in the answer code at line 20

 

 

--------------------------------------------------------------------------------------------

fscanf(STDIN, "%d",
$n // the number of temperatures to analyse
);
 
$inputs = fgets(STDIN);
$inputs = explode(" ",$inputs);
 
$near = abs($inputs[0]);
 
 
for ($i = 0; $i < $n; $i++)
if (abs($near) >= abs($inputs[$i+1]) {
    $near = abs($inputs[$i+1]);
 
}
echo($near);
}
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.