# need help in how to setup a for loop for this scenario

Page 1 of 1

## 3 Replies - 1223 Views - Last Post: 04 October 2009 - 03:29 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=129868&amp;s=76bed750b881d6b686ca278796ba960c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 putha-nee

• D.I.C Head

Reputation: 0
• Posts: 168
• Joined: 04-October 09

# need help in how to setup a for loop for this scenario

Posted 04 October 2009 - 02:55 PM

```double cost1 = 0, cost2= 0;

for( weight = 0; weight<=free_weight; weigh++)
{
cost 1 = getMinimumCost();
}
for( weight = 11; weight<=weight_limit; weigh++)
{
cost2 +=weight * getRate20();
}

```

Here is what I am trying to do. I am given a certain weight for a shipping carrier. i need to throw that weight through three ranges and it needs to individually calculate the cost for each range as long as the weight is in that range and then calculates the total of any applicable ranges.

For ex. the ranges are for zone1, zone2, zone3 are 0-10, 11-20 , 21-75, respectively. The rates for each zone are 0, \$2, \$3 / extra pound respectively.

So for instance, for a 25 pound object, it would be the minimum cost, \$9.25 + (10 * 2) + (5 * 3).

i am trying to setup a for loop for each of those ranges but i don't know where I am going wrong here as free_weight and weight_limit are predefined variables so using those in the for loop would only mean i'd get the maximum cost for that range rather then the actual cost given a specific weight

Is This A Good Question/Topic? 0

## Replies To: need help in how to setup a for loop for this scenario

### #2 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

## Re: need help in how to setup a for loop for this scenario

Posted 04 October 2009 - 03:05 PM

I don't really know why you're using a for loop for this, it sounds like it's an if statement problem.
Especially in the first for loop, you assign cost1 a single value, if the for loop runs 0 times, then it will be 0, otherwise it will be the value getMinimumCost() returns.

I also don't quite understand how you are coming up with the formula of cost, eg: \$9.25 + (10 * 2) + (5 * 3), it seems like the numbers are arbitrarily chosen. Perhaps if you explained what the zones mean? Or explain how you are getting this formula.

As for the code syntax itself:
In your for loops, you use weigh++ and I believe you want weight++ instead. Also your cost1 variable has a space between the cost and 1.
Was This Post Helpful? 0

### #3 putha-nee

• D.I.C Head

Reputation: 0
• Posts: 168
• Joined: 04-October 09

## Re: need help in how to setup a for loop for this scenario

Posted 04 October 2009 - 03:10 PM

William_Wilson, on 4 Oct, 2009 - 02:05 PM, said:

I don't really know why you're using a for loop for this, it sounds like it's an if statement problem.
I also don't quite understand how you are coming up with the formula of cost, eg: \$9.25 + (10 * 2) + (5 * 3), it seems like the numbers are arbitrarily chosen. Perhaps if you explained what the zones mean? Or explain how you are getting this formula.

As for the code syntax itself:
In your for loops, you use weigh++ and I believe you want weight++ instead. Also your cost1 variable has a space between the cost and 1.

The cost is calculated individually for each of the 3 zones and the total is then added in order to compute the total cost of shipping of the parcel.

For a box 10 pounds or less, the 9.25 is charged. For a box 11-20 pounds, you are charged 0.18cents/pound above 11 plus 9.25 which is the cost of the first 10 pounts. for a box 21-75 pounds, you are charged 0.17cents/pound for each pound above 21 pounds, plus the \$1.80 (for pounds 11-20 @ rate of 0.18cents/pound) plus \$9.25.

That should clarify the question about the zones and costs.

As far as the if statements or for loops, I am guessing I would need both but i just can't get my hands around how i would put it together and apply it to any amount of weight.
Was This Post Helpful? 0

### #4 putha-nee

• D.I.C Head

Reputation: 0
• Posts: 168
• Joined: 04-October 09

## Re: need help in how to setup a for loop for this scenario

Posted 04 October 2009 - 03:29 PM

putha-nee, on 4 Oct, 2009 - 02:10 PM, said:

William_Wilson, on 4 Oct, 2009 - 02:05 PM, said:

I don't really know why you're using a for loop for this, it sounds like it's an if statement problem.
I also don't quite understand how you are coming up with the formula of cost, eg: \$9.25 + (10 * 2) + (5 * 3), it seems like the numbers are arbitrarily chosen. Perhaps if you explained what the zones mean? Or explain how you are getting this formula.

As for the code syntax itself:
In your for loops, you use weigh++ and I believe you want weight++ instead. Also your cost1 variable has a space between the cost and 1.

The cost is calculated individually for each of the 3 zones and the total is then added in order to compute the total cost of shipping of the parcel.

For a box 10 pounds or less, the 9.25 is charged. For a box 11-20 pounds, you are charged 0.18cents/pound above 11 plus 9.25 which is the cost of the first 10 pounts. for a box 21-75 pounds, you are charged 0.17cents/pound for each pound above 21 pounds, plus the \$1.80 (for pounds 11-20 @ rate of 0.18cents/pound) plus \$9.25.

That should clarify the question about the zones and costs.

As far as the if statements or for loops, I am guessing I would need both but i just can't get my hands around how i would put it together and apply it to any amount of weight.

Any suggestions?
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }