nestaho Posted January 31, 2018 Share Posted January 31, 2018 // 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 More sharing options...
requinix Posted January 31, 2018 Share Posted January 31, 2018 if (abs($near) $near = abs($inputs[$i+1]) }Explain to me, in words, exactly what that is doing. Link to comment Share on other sites More sharing options...
nestaho Posted January 31, 2018 Author Share Posted January 31, 2018 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 More sharing options...
requinix Posted January 31, 2018 Share Posted January 31, 2018 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 More sharing options...
nestaho Posted January 31, 2018 Author Share Posted January 31, 2018 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 More sharing options...
requinix Posted January 31, 2018 Share Posted January 31, 2018 CAN U REWRITE THE CODE FOR ME? PLEASE...No. You solved the problem. That's great. Now you just have to deal with a typo: if (abs($near) >= abs($inputs[$i+1]) {Count your parentheses. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.