Thread: Some mathematical coding problem

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    37

    Some mathematical coding problem

    So my assignment is calculating shipping costs. But in the problem, it states that the shipping rates are based on per 500 miles shipped. (Example - if the number of miles is 2000, it will be rate * 4).
    How do I write this part up? Thanks. I can't figure it out.

    If you need more information, please ask.

    Edit: I tried a code but it does not work properly:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
     
     double weight, miles, distanceRate, rate, shippingCharge;
     
     // User enters the weight and mileage
     printf("Enter the weight of the package:");
     scanf("%lf", &weight);
     printf("Enter the amount of miles it would take:");
     scanf("%lf", &miles);
    // Find the shipping rate and charge when based on per 500 miles
     distanceRate = (miles / 500) + 1;
     shippingCharge = distanceRate * rate;
     
    // When the weight is greater than 50
    if (weight > 50); {
     printf("Sorry, we only ship packages of 50 pounds or less.");
     }
     // When the weight is less than or equal to 10
    if (weight <= 10) {
     rate = 2.00;
     printf("Shipping charge is $ %lf \n", shippingCharge);
     }
     
     // When the weight is greater than 10 but less than or equal to 50
    else 
     if(weight > 10) {
      rate = 4.50;
      printf("Shipping charge is %lf \n", shippingCharge);
      }
      
     else(weight <= 50); {
      rate = 4.50;
      printf("Shipping charge is %lf \n", shippingCharge);
      }
       
     system("pause");
    }
    Last edited by Kayla Hoyte; 10-08-2017 at 10:29 AM.

  2. #2
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Another note is that we have not covered loops yet. I am using double for this assignment. And lastly, if the amount of miles is 300, it will be the rate of a 500 mile package.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    33
    Try using conditional statement "if"
    Code:
    if(distance<500)
    {
    cost = 500*x;
    }
    else
    {
    cost = (your condition);
    }

  4. #4
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    The only problem is that i would have to do this for every 500 interval. Also, wouldn't it be cost = 1 * x (x = rate) as each 500 interval counts as 1. Sorry, i did not make that clear.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if (weight > 50);
    Watch the ; at the end of this line.
    Your if statement now does nothing, and the next statement happens all the time.

    > else(weight <= 50);
    Else statements don't have conditions.
    The implied condition is "everything not matching all the previous if / else if statements.

    > shippingCharge = distanceRate * rate;
    You need to do this after you've done things like
    rate = 2.00;

    C doesn't re-evaluate expressions based on future assignments.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Thank you, that solved everything!

    Edit: below
    Last edited by Kayla Hoyte; 10-08-2017 at 11:38 AM.

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    37
    Problem, I disregarded the important part. How to I write it if within each 500 miles, it would cost a certain rate?

    New code:

    Code:
    
    
    /* 
    
    
    Kayla Hoyte
    
    
    10/08/17
    
    
    
    
    
    Packages above 50pounds will not be shipped. 
    
    
    Less than or equalto 10 pounds is $2.00. 
    
    
    More than 10 poundsbut less than or equal to 50 pounds is  $4.50.
    
    
    The shipping ratesare based on per 500 miles shipped.
    
    
    Calculate theshipping charge.
    
    
    */
    
    
    
    
    
    #include<stdio.h>
    
    
    #include<stdlib.h>
    
    
    
    
    
    int main() {
    
    
    
    
    
                double weight, miles, distanceRate,rate, shippingCharge;
    
    
    
    
    
                // User enters the weight andmileage
    
    
                printf("Enter the weight of thepackage:");
    
    
                scanf("%lf", &weight);
    
    
                printf("Enter the amount ofmiles it would take:");
    
    
                scanf("%lf", &miles);
    
    
    
    
    
    
    
    
    // When weight isgreater than 50
    
    
    if(weight > 50){
    
    
                printf("Sorry, we only shippackages of 50 pounds or less. \n");
    
    
                }
    
    
                else
    
    
                // When weight is less than 10
    
    
                if(weight < 10) {
    
    
                            rate = 2.00;
    
    
                            shippingCharge = (miles% 500) * rate;
    
    
                            printf("Shippingcharge is $%.2lf \n", shippingCharge);
    
    
                }
    
    
                // When weight is greater than 10
    
    
                else
    
    
                if(weight > 10) {
    
    
                            rate = 4.50;
    
    
                            shippingCharge = (miles% 500) * rate;
    
    
                            printf("Shippingcharge is $%.2lf \n", shippingCharge);
    
    
                }
    
    
                // When weight is less than or equalto 50
    
    
                else
    
    
                if(weight <= 50) {
    
    
                            rate = 4.50;
    
    
                            shippingCharge = (miles% 500) * rate;
    
    
                            printf("Shippingcharge is $%.2lf \n", shippingCharge);            
    
    
                }
    
    
    
    
    
                system("pause");         
    
    
    }
    Last edited by Kayla Hoyte; 10-08-2017 at 11:28 AM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I think the answer is in the unit. $2/500 mi for the smallest package. So a basic way is distance / 500 * rate.

    You will need to adjust distance if it isn't a multiple of 500.

    if (distance % 500 != 0) distance = 500 * ((distance / 500) + 1);
    Last edited by whiteflags; 10-08-2017 at 05:11 PM.

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I think I see a wrongful check on value of 10 and maybe one too many else if checks

    over 50 kick out, or forget it, move on, you did that
    > 50

    less than or equal to
    10,
    <= 10
    greater than 10 but not over 50.
    you already got rid over 50,
    so locally all that is really left is over 10 because over 50 is already gone on your first check. (right?)
    > 10

    I'd go back an recheck your if else statements. and/ or to play it safe you can put the two together in one if statement if you've covered that, or use logic and test your results first.

    Code:
    if (  value > 10  && value <= 50)
    {
        printf(" GOT IT  %lf ", value);
    }
    else
    {
        
        printf("not   %lf ", value);
    }
    puts it greater than 10 but not greater than 50. && means and
    if greater than 10 and less than or equal to 50 value is somewhere between 11 and 50

    with float or double, it then is value is somewhere between 10.x and 50.0 right?

    you could reverse your logic

    start low then work your way up then you'd get to over fifty , with an ending of else "its too big" logic

    3. Packages above 50 pounds will not be shipped.


    2. Less than or equal to 10 pounds is $2.00.


    1. More than 10 pounds but less than or equal to 50 pounds is $4.50.

    start from the bottom and work your way up ending with a block of code that would consist of this.
    Code:
    if
    else if
    else
    Last edited by userxbw; 10-09-2017 at 05:59 PM.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You can even arrange the cases for weights so that you don't have to compare for floating point numbers for equality at all.

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by whiteflags View Post
    I think the answer is in the unit. $2/500 mi for the smallest package. So a basic way is distance / 500 * rate.

    You will need to adjust distance if it isn't a multiple of 500.

    if (distance % 500 != 0) distance = 500 * ((distance / 500) + 1);
    OP mostly,
    requires a cast
    Code:
    double value, distance;
    
     
    printf("what distance is it? : ");
    scanf("%lf", &distance);
    //added a cast to int on the double
    if ((int)distance % 500 != 0) distance = 500 * ((distance / 500) + 1); 
    printf("cost :  %lf ",distance);
    printf("\n");

    Code:
    [userx@void bin]$ gcc getweight.c
    getweight.c: In function 'main':
    getweight.c:30:14: error: invalid operands to binary % (have 'double' and 'int')
     if (distance % 500 != 0) distance = 500 * ((distance / 500) + 1);
                  ^
    [userx@void bin]$ gcc getweight.c
    [userx@void bin]$ ./a.out
    what distance is it? : 1000
    cost : 1000.000000
    anyone cover cast data types yet?
    Last edited by userxbw; 10-09-2017 at 06:18 PM.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That line only rounds distance up by 500, it doesn't compute the cost.

    You are getting that error because floating point does not support modular arithmetic with %, you have to use the function modf(). There are many ways around doing that as well, including not using floating point, since we will only need to round up by 500 on occasion, anyway. The important point is to round up if the quotient of distance/500.0 has a fractional part.

    If I were lazy and not allowed to change distance's type, simply multiplying rate by ceil(distance / 500.0) computes the cost with the least noise.
    Last edited by whiteflags; 10-09-2017 at 06:50 PM.

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Example - if the number of miles is 2000, it will be rate * 4).
    rate is 2 per mile or 4.5 per mile.
    if rate doubles only every exact 500 extra miles?
    then it is rate times miles to get cost of shipping, to change it to a higher rate it then only elevates every other 500 miles no more no less.
    changeRate = 500;

    Code:
    rate = 2.5
    rate = rate * (miles/changeRate)something missing here;
    30/changeRate = ?
    500/changeRate = ?
    600/changeRate =?

    if you use that equation you'll see something hopefully it is missing one part of it.
    a little code to help you look at results to see how to figure it out, at least this is what I did. then I seen the pattern.
    Code:
    rate = 2;
    //or
    rate = 4.5;
     
    // depending on weight
    
    printf("Enter the amount of miles it would take:");
     scanf("%lf", &miles);
    
        if( miles > 500 )
        {
            printf("%lf miles/500\n", miles/500);
    
            rate = rate * (miles/500); // something else  missing here  to complete the equation;
    
            printf("rate = %lf\n", rate);
        }
    else
    {
    rate = rate * miles;
    }
    if and when you figure it out you'll have to make it fit within your code block to apply to to the two different rates.

    that actually is still not perfectly correct even with the missing part of the equation, because that teacher is doing crazy math.

    it might be what he or she is looking for above 500 prorate it by percent
    Last edited by userxbw; 10-09-2017 at 07:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About mathematical programming problem...
    By yeohwl91 in forum C Programming
    Replies: 1
    Last Post: 09-27-2011, 08:24 AM
  2. help with mathematical problem in C
    By feelseez in forum C Programming
    Replies: 3
    Last Post: 09-10-2006, 11:44 AM
  3. Mathematical problem
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2006, 02:38 AM
  4. Problem with a mathematical formula
    By BianConiglio in forum C Programming
    Replies: 13
    Last Post: 04-29-2005, 11:26 PM
  5. I need help with mathematical coding.......911
    By Semma in forum C++ Programming
    Replies: 2
    Last Post: 10-01-2001, 03:10 AM

Tags for this Thread