OK, your goal is to write a program that rounds numbers. It must take two numbers as inputs from the command line. One is a floating point value, the other is an integer. The interger value determines where to round the floating point value.
Explanation:
integer value of -3 rounds to the nearest thousand
integer value of -2 rounds to the nearest hundred
integer value of -1 rounds to the nearest ten
integer value of 0 rounds to the nearest integer
integer value of 1 rounds to the nearest tenth
integer value of 2 rounds to the nearest hundredth
integer value of 3 rounds to the nearest thousandth
etc......
the program must take the arguments from the command line, and spit the answer back out on the command line. You may NOT use any external libraries or #include files. You can use any language you want. Try to make the code as SHORT as possible. It must work with both positive and negative integer and decimal values. No error checking of inputs is required.
here's my solution:
print"decimal:";$b=<>;chomp$b;print"integer:";$a=int<>;
if($a<0){for($c=$a;$c<0;$c++){$b/=10}$b+=0.5;$b=int$b;
if($b<0){$b-=1}for($c=$a;$c<0;$c++){$b*=10}}if($a>0){
for($c=0;$c<$a;$c++){$b*=10}$b+=0.5;$b=int$b;if($b<0)
{$b-=1}for($c=0;$c<$a;$c++){$b/=10}}if($a==0){$b+=0.5;
$b=int$b;if($b<0){$b-=1}}print"rounded:$b\n";
Why's it so poorly formatted? cause it looks kinda cool, and makes it slightly harder for you to steal my code. What language is it? if you don't know, I'll say later. Please, if you're going to attempt this, don't just copy what I wrote in another language, that's not any fun. I mostly want to do this to see what other languages it comes out in, and what kinda innovations people come up with to do the same task.
EDIT: Your program only needs to handle numbers that actulaly fit in the standard sized variables in your language. you dont need to handle 256 bit integers or anything.
Also: if there's a library for IO function (i.e. stdio.h in C++) you can include that one library, but only for use in input and output, not the rounding.
Explanation:
integer value of -3 rounds to the nearest thousand
integer value of -2 rounds to the nearest hundred
integer value of -1 rounds to the nearest ten
integer value of 0 rounds to the nearest integer
integer value of 1 rounds to the nearest tenth
integer value of 2 rounds to the nearest hundredth
integer value of 3 rounds to the nearest thousandth
etc......
the program must take the arguments from the command line, and spit the answer back out on the command line. You may NOT use any external libraries or #include files. You can use any language you want. Try to make the code as SHORT as possible. It must work with both positive and negative integer and decimal values. No error checking of inputs is required.
here's my solution:
print"decimal:";$b=<>;chomp$b;print"integer:";$a=int<>;
if($a<0){for($c=$a;$c<0;$c++){$b/=10}$b+=0.5;$b=int$b;
if($b<0){$b-=1}for($c=$a;$c<0;$c++){$b*=10}}if($a>0){
for($c=0;$c<$a;$c++){$b*=10}$b+=0.5;$b=int$b;if($b<0)
{$b-=1}for($c=0;$c<$a;$c++){$b/=10}}if($a==0){$b+=0.5;
$b=int$b;if($b<0){$b-=1}}print"rounded:$b\n";
Why's it so poorly formatted? cause it looks kinda cool, and makes it slightly harder for you to steal my code. What language is it? if you don't know, I'll say later. Please, if you're going to attempt this, don't just copy what I wrote in another language, that's not any fun. I mostly want to do this to see what other languages it comes out in, and what kinda innovations people come up with to do the same task.
EDIT: Your program only needs to handle numbers that actulaly fit in the standard sized variables in your language. you dont need to handle 256 bit integers or anything.
Also: if there's a library for IO function (i.e. stdio.h in C++) you can include that one library, but only for use in input and output, not the rounding.
