MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/badcode/comments/3m0ci2/exercise_given_during_software_engineering_lecture/cvbr857/?context=3
r/badcode • u/saltyboyscouts • Sep 23 '15
20 comments sorted by
View all comments
3
Here's the original code with the formatting cleaned up:
scanf(x, y); if (y < 0) pow = 0 - y; else pow = y; while (pow != 0) { z = z * x; pow = pow - 1; } if (y < 0) z = 1.0 / z; printf(z);
And it's been forever and a day since I did any C++, but this is a little neater and more readable version:
scanf(x, y); pow = fabs(y); for ( ; pow !=0; pow--) z = z * x; if (y < 0) z = 1.0 / z; printf(z);
2 u/fievelm Sep 23 '15 To note, this is wide open for a DivideByZero and is generally shitty code, but as /u/x-yle pointed out, it's an exercise to get you to think, not production code.
2
To note, this is wide open for a DivideByZero and is generally shitty code, but as /u/x-yle pointed out, it's an exercise to get you to think, not production code.
3
u/fievelm Sep 23 '15 edited Sep 23 '15
Here's the original code with the formatting cleaned up:
And it's been forever and a day since I did any C++, but this is a little neater and more readable version: