• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

mini programming challenge! if you prgram at all, come try this, it's more fun than hard.

notfred

Lifer
Feb 12, 2001
38,241
4
0
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.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Beau6183
Command line only? or do VBS's work as well?

I guess you can write it in VB Script if you want. I didn't even think of someone wanting to write soemthing as short as possible and include a GUI :)
 

rgwalt

Diamond Member
Apr 22, 2000
7,393
0
0
I could write it in Fortran, but I couldn't beat your length.

Besides, you might laugh at me.

Ryan
 

joohang

Lifer
Oct 22, 2000
12,340
1
0
Originally posted by: notfred
Originally posted by: Beau6183
Command line only? or do VBS's work as well?

I guess you can write it in VB Script if you want. I didn't even think of someone wanting to write soemthing as short as possible and include a GUI :)

VBScript doesn't have a GUI.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Either fortran or assembly would be cool :)

It doesn't have to be shorter than mine, just that there was some effort to keep it from sprawling over hundreds of lines :)
Really, I just want to see what it looks like in different languages with different people's ideas for solving it.

Anyway, mine looks a lot longer when it's formatted normally.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: joohang
Originally posted by: notfred
Originally posted by: Beau6183
Command line only? or do VBS's work as well?

I guess you can write it in VB Script if you want. I didn't even think of someone wanting to write soemthing as short as possible and include a GUI :)

VBScript doesn't have a GUI.

Well, then if there's no GUI and no command line, how do you run it?

There has to be somewhere for the user to input two numbers, and a rounded number has to be displayed to the user.
 

joohang

Lifer
Oct 22, 2000
12,340
1
0
I am not sure if VBScript can display and take input from command line, but there are built-in functions for InputBox and MessageBox.
 

Haircut

Platinum Member
Apr 23, 2000
2,248
0
0
My entry in VB:

Dim val1 As Double
Dim val2 As Integer
val1 = InputBox("Enter value")
val2 = InputBox("Enter rounding precision")
Do
val1 = val1 * 10
factor = factor + 1
Loop Until val1 = Int(val1)
If Right(Str$(val1), 1) >= 5 Then val1 = val1 + 10 ^ (factor - val2)
val1 = (Int(val1 / (10 ^ (factor - val2)))) / (10 ^ val2)
MsgBox val1
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: notfred
Originally posted by: joohang
Originally posted by: notfred
Originally posted by: Beau6183
Command line only? or do VBS's work as well?

I guess you can write it in VB Script if you want. I didn't even think of someone wanting to write soemthing as short as possible and include a GUI :)

VBScript doesn't have a GUI.

Well, then if there's no GUI and no command line, how do you run it?

There has to be somewhere for the user to input two numbers, and a rounded number has to be displayed to the user.

you use parameters but this is a pretty easy problem to solve using type casts in c or c++ i'm just too lazy to write it.
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
VBScript:

Dim dblVar, intStep, intRnd
dblVar = CDbl(InputBox("Number to round:"))
intStep = (CInt(InputBox("Place to round:" & vbCr & "ex: -3 = Thousands, 3 = thousandths")))
msgbox("Answer:" & (Round(dblVar*10^(intStep * -1)) * 10 ^ intStep))
 

Beau

Lifer
Jun 25, 2001
17,730
0
76
www.beauscott.com
Originally posted by: joohang
I am not sure if VBScript can display and take input from command line, but there are built-in functions for InputBox and MessageBox.

It can, but you have to declare it to do so using the CreateObject("WSH.shell")
 

joohang

Lifer
Oct 22, 2000
12,340
1
0
Originally posted by: Beau6183
Originally posted by: joohang
I am not sure if VBScript can display and take input from command line, but there are built-in functions for InputBox and MessageBox.

It can, but you have to declare it to do so using the CreateObject("WSH.shell")

Oh I see. Cool. Thanks.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Beau6183
VBScript:

Dim dblVar, intStep, intRnd
dblVar = CDbl(InputBox("Number to round:"))
intStep = (CInt(InputBox("Place to round:" & vbCr & "ex: -3 = Thousands, 3 = thousandths")))
msgbox("Answer:" & (Round(dblVar*10^(intStep * -1)) * 10 ^ intStep))

oh, Round() hardly counts..... try writing the round funtion yourself!
 

DanFungus

Diamond Member
Jul 27, 2001
5,857
0
0
this is in QwaziScript:
start
Round the damn numbers or I'll kick the crap outta you!
/end

good, no? Try it on any person you find, I'm sure it'll work
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Haircut
My entry in VB:

Dim val1 As Double
Dim val2 As Integer
val1 = InputBox("Enter value")
val2 = InputBox("Enter rounding precision")
Do
val1 = val1 * 10
factor = factor + 1
Loop Until val1 = Int(val1)
If Right(Str$(val1), 1) >= 5 Then val1 = val1 + 10 ^ (factor - val2)
val1 = (Int(val1 / (10 ^ (factor - val2)))) / (10 ^ val2)
MsgBox val1

Does that need to be compiled or is there some way I can run that?
 

Haircut

Platinum Member
Apr 23, 2000
2,248
0
0
If you have VB installed than just copy that text into Forum_Load and run

If not, then you can try this executable and hope you have any run-time files that may be necessary.
executable
 

joohang

Lifer
Oct 22, 2000
12,340
1
0
Originally posted by: notfred
Originally posted by: Beau6183
VBScript:

Dim dblVar, intStep, intRnd
dblVar = CDbl(InputBox("Number to round:"))
intStep = (CInt(InputBox("Place to round:" & vbCr & "ex: -3 = Thousands, 3 = thousandths")))
msgbox("Answer:" & (Round(dblVar*10^(intStep * -1)) * 10 ^ intStep))

oh, Round() hardly counts..... try writing the round funtion yourself!

Why write one if it is built in and works well already?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Haircut
If you have VB installed than just copy that text into Forum_Load and run

If not, then you can try this executable and hope you have any run-time files that may be necessary.
executable

It doesn't work for negative numbers (enter -25.69, 1).
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: notfred
Originally posted by: Haircut
If you have VB installed than just copy that text into Forum_Load and run

If not, then you can try this executable and hope you have any run-time files that may be necessary.
executable

It doesn't work for negative numbers (enter -25.69, 1).

ohhh what about numbers larger then a float can handle?