Math functions and if statements

closed account (2EURX9L8)
This is the assignment I am having trouble with, sorry for the wall of text but I feel it is necessary to get help.

The Fast Freight Shipping Company charges the following rates:
Weight of Package (in Kiolograms) Rate per 500 Miles Shipped
2 Kg or less $1.10
Over 2 Kg but not more than 6 Kg $2.20
Over 6 Kg but not more than 10 Kg $3.70
Over 10 Kg but not more than 20 Kg $4.80


make sure the user entered a correct weight
make sure the user entered a correct distance
add an if/else if statement that uses logical operators to write a range for each row in the rate table
add an if/else if statement that uses simple conditions and orders the conditions to determine the rate for each row in the rate table

I am having trouble figuring out how to add the if statements and getting the right expressions

this is the code I have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
#include<iomanip>
using namespace std;

/*This program gets the weight of a package and 
the distance it is to be shipped. Then it computes
the shipping charges for that package. 
*/
int main(){
	const int DISTANCE_UNIT = 500;
	double rate=0, shippingCost;
	int weight, distance, distanceUnits;

	//set up output formatting
	cout<<setprecision(2)<<fixed;

	//get weight
	cout<<"How much does the package weigh (in Kilograms)? ";
	cin>>weight;

	if(weight<=0||weight>20)          //The Fast Freight Shipping Company does not accept values of 0 or less for the weight of a package. 
	{cout<<"Sorry, the weight you entered, "<<weight<<"Kg, is not in the range of weight Fast Freight Shipping is able to ship"<<endl;
	return 0;}
	else
	
	//get distance
	cout<<"How far will the package be going? ";
	cin>>distance;

	if(distance<10||distance>3000) // The Fast Freight Shipping Company does not accept distances of less than 10 miles or more than 3000 miles. 
	{cout<<"Sorry, the distance you entered, "<<distance<<" miles,  is not in the range of weight Fast Freight Shipping is able to ship"<<endl;
	return 0;}
	else


	
	//compute how many distance units 
	distanceUnits = distance/DISTANCE_UNIT;
	if(distance%DISTANCE_UNIT != 0)
	
	//deteremine rate using complex conditions with 
	//logical operators to identify ranges
	if(weight<=2&&distance<=500) 	
	{rate=1.10;}				
	else if (weight<=2&&distance>500&&distance<=3000)
	{rate=2.20;}
	else if (weight<=2&&distance>50
	
	
	//calculate shipping cost
	shippingCost = distanceUnits * rate;

	//print shipping cost
	cout<<"\nUsing logical operators to identify ranges:\n"
		<<"The cost to ship a package that weighs "<<weight
		<<" Kilograms\nfor a distance of "<<distance
		<<" miles is $"<<shippingCost<<endl<<endl;
	
	
	//deteremine rate using if/else if with simple conditions
	//using order to correctly separate the options

	//calculate shipping cost
	shippingCost = distanceUnits * rate;

	//print shipping cost
	cout<<"Using the order of the if/else if statement:\n"
		<<"The cost to ship a package that weighs "<<weight
		<<" Kilograms\nfor a distance of "<<distance
		<<" miles is $"<<shippingCost<<endl<<endl;
	
	return 0;
}

Last edited on
closed account (2EURX9L8)
on this part
1
2
3
4
5
6
7
//deteremine rate using complex conditions with 
	//logical operators to identify ranges
	if(weight<=2&&distance<=500) 	
	{rate=1.10;}				
	else if (weight<=2&&distance>500&&distance<=3000)
	{rate=2.20;}
	else if (weight<=2&&distance>50



do I need to do if/else statements for every increment of 500 (distance) to and add the cost for each? that just seems like it will a whole lot more than what is needed

and on the //deteremine rate using if/else if with simple conditions
//using order to correctly separate the options

How do this again without using logical ops?????
Last edited on
Well, first, I don't understand why you'd need to check your distance in those 'if' statements. You've already ascertained that the entered distance is valid and the distance doesn't affect the rates, so it's not needed here.

The spec is a little ambiguous. For example, the 'per 500 miles' rule. Are we to assume that 1 - 499 is the first lot then as soon as another mile is clocked we're charging for another set of 500? If so, you need to be careful of your maths up there:

1
2
3
4
5
6
7
8
9
// Say I'm the user.  I enter 100 miles.
distanceUnits = distance/DISTANCE_UNIT;

// Let's write that out in comments.
// distanceUnits = 100 / 500.
// 100/500 = 0.2
// Data type is an integer, so this is truncated to 0.

shippingCost = distanceUnits * rate;   // Whoops! (Or hooray, free shipping for me) 


You'd have to always add one to that math to make it work how you want it to.

Generally, for larger statements I'd use a switch statement. However, this isn't that big and your assignment specifies to use if and that must be respected. So, all you'd need is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if (weight <= 2)
{
   rate = 1.10;
}
else if (weight >2 && weight <=6)
{
   rate = 2.20;
}
else if (weight > 6 && weight <= 10)
{
   rate = 3.70;
}
else
{
   rate = 4.80;
}


You can make the last one an 'else if' if you like but since we've already validated the weight, it's pretty safe to assume that the else statement will only be hit if weight > 10 and <=20.

Hope this helps.
Last edited on
closed account (2EURX9L8)
Helped a bunch thank you!
Topic archived. No new replies allowed.