Jump to content

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);

}

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

Edited by nestaho

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?

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);
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.