0

In C , how do I make 1200 / 500 = 3.

I'm doing a homework assignment.

Shipping Calculator: Speedy Shipping company will ship your package based on how much it weighs and how far you are sending the package. They will only ship small packages up to 10 pounds. You need to have a program that will help you determine how much they will charge. The charges are based on each 500 miles shipped. They are not pro-rated, i.e., 600 miles is the same charge as 900 miles.

Here is the table they gave you:

Package Weight--------------------------Rate per 500 miles shipped

2 pounds or less------------------------$1.50

More than 2 but not more than 6---------$3.70

More than 6 but not more than 10--------$5.25

Here is one test case.

Test Case Data:

Weight: 5.6 pounds
Miles: 1200 miles

Expected results:

Your shipping charge is $11.10


My answer keeps coming out to 7.40

  • 7
    `1200 / 500` *doesn't* equal `3`. Perhaps you ought to explain why you think it should. – Jonathan Grynspan Jun 25 '12 at 19:00
  • 1
    At least tag your question as homework. – marktani Jun 25 '12 at 19:01
  • 4
    `ceil(1200 / 500) = 3`, but *could you explain why that should be the case*? – Makoto Jun 25 '12 at 19:01
  • 8
    @Makoto `ceil( 1200/500) = 2.0` – unkulunkulu Jun 25 '12 at 19:02
  • The miles are pro-rated soo , 1200 miles cost the same amount as 1500 which equals 11.10 $ – Austin Girau Jun 25 '12 at 19:03
  • 2
    It should actually be `ceil((double)1200/(double)500)` – Michael Boselowitz Jun 25 '12 at 19:03
  • I actually think its interesting that everybody wants to work in floating point. I would convert to int use the calculation I describe below, and add 1 if necessary. This would most likely be the FASTEST calculation route as floating point operations are normally the slowest calculations a processor does. – trumpetlicks Jun 25 '12 at 19:08
  • Integer arithmetic always rounds **down**, and two integers (or any data type like double or float) will always produce an integer even if a decimal (float) would more accurately represent the answer. Answer #3 fixes this elegantly. – Kevin Kostlan Jun 25 '12 at 19:08
  • @trumpetlicks while yes it is the quicker, for less experienced people it is harder to comprehend. Intuitively it is easier to understand as a floating point operation. Though I would think the difference in speed would be marginal at best, it is better practice. Of course if we are talking raw speed `result = (a + b - 1) / b;` would probably be even better instead of having a conditional. – Michael Boselowitz Jun 25 '12 at 19:17
  • @Michael Boselowitz - true, I see your point (it is a homework problem)!!! The ceil routine then is probably the best route from that perspective :-) – trumpetlicks Jun 25 '12 at 19:20
  • 1
    @mcwise The homework tag has been deprecated. – Beska Oct 15 '12 at 13:07
  • Does this answer your question? [Fast ceiling of an integer division in C / C++](https://stackoverflow.com/questions/2745074/fast-ceiling-of-an-integer-division-in-c-c) – phuclv Nov 04 '19 at 01:51
  • [Rounding integer division (instead of truncating)](https://stackoverflow.com/q/2422712/995714), [Dividing two integers and rounding up the result, without using floating point](https://stackoverflow.com/q/17005364/995714), [How to round up the result of integer division?](https://stackoverflow.com/q/17944/995714) – phuclv Nov 04 '19 at 01:51

6 Answers6

12

Are you trying to round up? Before dividing, you could add 499 to the number that is being divided.

(0 + 499) / 500 -> 0

(1 + 499) / 500 -> 1

(1200 + 499) / 500 -> 3

This will round up.

Daniel
  • 6,595
  • 9
  • 38
  • 70
  • 3
    @C0deH4cker: it's the same thing, and you don't have to get all confused with typecasting. – Daniel Jun 25 '12 at 19:05
6

Say you want to get a ceiling division a by b (in your example a = 1200 b = 500). You can do it in integer arithmetic like this.

result = (a + b - 1) / b;

Or you could use floating point numbers and do it like this (probably a bad idea)

result = (int) ceil( (double) a / b );

The thing is that as this is a homework, you could just make it up in small steps:

if( a % b == 0 ) {
    result = a / b;
} else {
    result = a / b + 1;
}

Another advantage of this code is that it actually doesn't overflow for too big as, but this is not relevant in this case, I guess.

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
  • If you were worried about overflowing with `a` too big, you could do `result = ((a - 1) / b) + 1;` instead. – Daniel Jun 25 '12 at 19:43
  • @Daniel, but that doesn't work for `a == 0`, yes, technically it works for more inputs, but the case of `a == 0` is easier to hit generally. – unkulunkulu Jun 25 '12 at 20:02
0

[EDIT1]

From your code you would implement this part as follows:

if ( weight <= 5.6 )
{
    int multiplier = (int) miles / 500;

    if( ((int)miles % 500) > 0)
        multiplier++;

    rate370 = (double)multiplier * 3.7;

    printf("Your total cost : %.2lf\n", rate370);      
}

[ORIGINAL] In "integer land" 1200 / 3 should equal to 2.

for what it "seems" you want try this:

int multFiveHundreds = (int)totalWeight / 500;
if(multFiveHundreds % 500 > 0)
    multFiveHundreds++;
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33
  • @AustinGirau, you should show some effort, what you tried, probably start a new question for this. – unkulunkulu Jun 25 '12 at 19:15
  • Im sorry if i seem to be not showing any "Effort" i just starting learning programming and all these concepts seem confusing for me to understand. – Austin Girau Jun 25 '12 at 19:23
  • Lets not be too hard on this guy, this is what we are here to do is answer coding questions for others who need help, and this is a coding question!!! +1 – trumpetlicks Jun 25 '12 at 19:40
0

I'd suggest using the mod and truncate functions. If mod comes out zero, it's fine, otherwise truncate and add 1.

JohnP
  • 402
  • 1
  • 8
  • 25
0

You have to use the ceiling of the division. This will round the quotient up to the next integer.

So when you are trying to find the number of 500-mile increments, you have to round the quotient up to the next integer.

Alternatively, (and inefficiently), you could increment the number of miles by 1, until it is divisible by 500...that is, while ( (q = x_miles++%500) != 0 ) {} . Then multipy q by the rate to get your answer (That is also assuming you will have an integer number of miles).

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
ShWebb
  • 39
  • 3
0

You could also use the stdlib div function. This might be nice if you only wanted integer math and specifically wanted to avoid floating point math.

http://www.cplusplus.com/reference/clibrary/cstdlib/div/

#include <stdlib.h>

int foo(void)
{
  div_t result = div(1200, 500);
  return result.quot + (0 < result.rem); 
}
Josh
  • 144
  • 1