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); } Quote Link to comment https://forums.phpfreaks.com/topic/306380-find-absolute-smallest-value-close-to-zero/ 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. Quote Link to comment https://forums.phpfreaks.com/topic/306380-find-absolute-smallest-value-close-to-zero/#findComment-1555909 Share on other sites More sharing options...
nestaho Posted January 31, 2018 Author Share Posted January 31, 2018 (edited) 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 January 31, 2018 by nestaho Quote Link to comment https://forums.phpfreaks.com/topic/306380-find-absolute-smallest-value-close-to-zero/#findComment-1555910 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? Quote Link to comment https://forums.phpfreaks.com/topic/306380-find-absolute-smallest-value-close-to-zero/#findComment-1555914 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); } Quote Link to comment https://forums.phpfreaks.com/topic/306380-find-absolute-smallest-value-close-to-zero/#findComment-1555916 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. Quote Link to comment https://forums.phpfreaks.com/topic/306380-find-absolute-smallest-value-close-to-zero/#findComment-1555917 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.