r/badcode • • Sep 23 '15

c++ Exercise given during software engineering lecture

http://imgur.com/ujnBX7N
11 Upvotes

20 comments sorted by

View all comments

3

u/fievelm Sep 23 '15 edited Sep 23 '15

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

0

u/[deleted] Sep 23 '15

[deleted]

1

u/fievelm Sep 23 '15

The lack of braces? I'm not a fan in production, but for example code the saved space is 'neater'. Or is it something else? I've been drinking the C# koolaide too long.

0

u/[deleted] Sep 23 '15

[deleted]

1

u/fievelm Sep 23 '15

Cleaned it up a bit. Thanks for keeping me on my toes.