[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 25 KB, 384x365, 1321661782812.jpg [View same] [iqdb] [saucenao] [google]
5578470 No.5578470 [Reply] [Original]

Any compsci bros in here?

I'm a total beginner and I just wrote this big program and it compiles and stuff but there's one math error.

Here's the part that is ruining me:
>For example, for a package that weighs 22 pounds that is being
>shipped 55 miles, you should charge the individual $20.67
>because: you should charge $6.89 (based on the weight) for the
>first 25 miles, $6.89 for the second 25 miles and another $6.89 for
>the remaining 5 miles, which is a fraction of 25 miles. Do not prorate >the shipping charges based on partial mileage (like 5/25
miles).

In this example, how would I add in the extra $6.89 for the last 5 miles?

I tried to skeez it and do:
>cost = (rate * (distance/25));

But that leaves out the last thingy that I need to incorporate.

>> No.5578483

http://en.wikipedia.org/wiki/Floor_and_ceiling_functions are your friend.

>> No.5578486
File: 35 KB, 376x260, 1362085349590.jpg [View same] [iqdb] [saucenao] [google]
5578486

bump

>> No.5578497

>>5578470
cost = (rate * (int((distance+24)/25)));

>> No.5578495

java is terrible
use c

>> No.5578511

>>5578497
This is incorrect. Please see >>5578483 ...

>> No.5578527
File: 14 KB, 343x383, 1326168505838.png [View same] [iqdb] [saucenao] [google]
5578527

>>5578470
>java

kill yourself

>> No.5578532

>>5578527
umad, bro?

>> No.5578540

>>5578511
???

not only would what i posted work, it'd be more efficient than calling ceil()

>> No.5578546
File: 6 KB, 200x105, 1346702200274.jpg [View same] [iqdb] [saucenao] [google]
5578546

Official thread music:
http://www.youtube.com/watch?v=HVO5WhIm4uI
I just used a gimmicky modulus in an if statement and I got it.

>>5578497
WRONGO

>>5578527
It's not my major. It's a required programming class for all students.

>>5578495
Java is terrible I agree.

>> No.5578548

>>5578527
that may be true, but i'm now looking for a CS job with my math major

>> No.5578555

>>5578546
how is "cost = (rate * (int((distance+24)/25)));" wrong?

>> No.5578558
File: 34 KB, 640x427, 1358395895357.jpg [View same] [iqdb] [saucenao] [google]
5578558

>>5578555
Cuz this is java, bruh. Compiler said no way jose.

We programmers now.

>> No.5578560

>>5578470
cost=0
x= mass*rate

for(i=25,;i<dist;i=i+=25){price+=x;}

>> No.5578567

>>5578558
bah, i forgot what the actual int caster is in Java, but if you cast ((distance+24)/25) to an int it'll round it down and give you what you need. way more elegant than using an if statement

>> No.5578573
File: 2.00 MB, 433x325, 1329702381626.gif [View same] [iqdb] [saucenao] [google]
5578573

God, I'm hype as fuck right now. This was worth a bajillion points.

>> No.5578574 [DELETED] 

>>5578540
Distance of 0 fails. Distance of 25 fails. 25n for all n =0 -> infinity... fail.

For distance = 0, int((distance + 24)/25) evaluates to 1. For distance = 25, your expression evaluates to 2.

You were saying?

>> No.5578583

>>5578573

why the fuck are you still bumping your shitty thread you faggot if you have the answer

at least have the decency to sage.

>> No.5578586
File: 1000 KB, 500x281, 1354133609562.gif [View same] [iqdb] [saucenao] [google]
5578586

>>5578583
My thread, my rules.

>> No.5578589

>>5578567
If int rounds things down, then for a range of distance between 0 and 1 noninclusive your formula is incorrect, no?

Distance can be a non-integer.

>> No.5578600

>>5578470
if (distance) < 50
cost = cost + rate * (distance)
else
if (distance) >= 50
cost = cost + (2 * rate) * (distance)
stop

imi not sure what your asking?