By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,585 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

C-loops explain plz

P: n/a
Hey, forgive me of my ignorance!

Can someone tell me why this won't work. I have this coded but it
won't compile is this not permitted in C.

kg = kilograms and d = distance // this is not code but a example of
what
I was trying to do. I at the library now and I don't have the code
with me now
but this is what I was trying to do.

if kg => 6kg
if d => 200
count = 1.50
else for (d; d > 200; d--)
count + 1.50

Well that basically how I coded it. I was trying to see if the
distance is 200 or less if so add 1.50 to count. But if it's more
than 200 add 1.50 to count for every 200 in distance. Using a for loop
that how I thought it should be written. The Sams teach yourself
don't mention when one should use different looping statements.
Please explain to me why this would work. I got it to work using only
if statements thou. Explain plz. Thanks in advance :)
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a

"Tweaxor" <ph****@yahoo.com> wrote in message
news:1c**************************@posting.google.c om...
Hey, forgive me of my ignorance!

Can someone tell me why this won't work. I have this coded but it
won't compile is this not permitted in C. if kg => 6kg
if d => 200
count = 1.50
else for (d; d > 200; d--)
count + 1.50


This is not a valid C program. Is this what you mean ?

if (kg >= 6) {
if (d >= 200) {
count = 1.50;
} else {
for ( d ; d > 200 ; d--)
count += 1.50;
}
}

Also, the first 'd' in the for loop is not needed so it can be re-written
as:

for ( ; d > 200 ; d-- )

This is a valid C program. Explain what you are trying to do a little
better.

Nov 13 '05 #2

P: n/a
ph****@yahoo.com (Tweaxor) wrote:
Hey, forgive me of my ignorance!

Can someone tell me why this won't work. I have this coded but it
won't compile is this not permitted in C.

kg = kilograms and d = distance // this is not code but a example of
what
I was trying to do. I at the library now and I don't have the code
with me now
but this is what I was trying to do.

if kg => 6kg
if d => 200
count = 1.50
else for (d; d > 200; d--)
count + 1.50
Uck! Let's see what you really want to do:
Well that basically how I coded it. I was trying to see if the
distance is 200 or less
if ( d <= 200 )
if so add 1.50 to count.
if ( d <= 200 )
count += 1.5;
But if it's more
than 200
if ( d <= 200 )
count += 1.5;
else
add 1.50 to count for every 200 in distance.
if ( d <= 200 )
count += 1.5;
else
count += 1.5 * (d / 200);

Note: we are assuming that d is positive and of integral type!
Using a for loop

OK.

if ( d <= 200 )
count += 1.5;
else
for( ; d > 200; d -= 200 )
count += 1.5;

I'm not sure if it is what you asked for, if not please refine your
request.

HTH
Regards
--
Irrwahn
(ir*******@freenet.de)
Nov 13 '05 #3

P: n/a
This is want I was trying. I was wanting to increment total cost.
cost1 = 1.10 for every 500 in d add 1.10 to it.
printf("\nEnter weight(in Kg): ");
scanf("%i",&wt);
if (wt < 0 || wt > 20) {
scanf("%i",&wt);
}
printf("\nEnter distanc(in mi.): ");
scanf("%i",&d);
if (d < 0 || d >3000) {
scanf("%i",&d);
}
if (wt <= 2 && d <= 500) {
printf("\tTotal cost: %1.2f",cost1);
}
if (wt >= 3 && wt <= 6 && d <= 500) {
printf("\tTotal cost: %1.2f",cost2);
}
if (wt >= 6 && wt <= 10 && d <= 500) {
printf("\tTotal cost: %1.2f",cost3);
}
if (wt >= 10 && wt <= 20 && d <= 500) {
printf("\tTotal cost: %1.2f",cost4);
}
return 0;
}
Nov 13 '05 #4

P: n/a
On 18 Oct 2003 12:50:02 -0700, Tweaxor wrote:
This is want I was trying. I was wanting to increment total cost.
cost1 = 1.10 for every 500 in d add 1.10 to it.
printf("\nEnter weight(in Kg): ");
scanf("%i",&wt);
if (wt < 0 || wt > 20) {
scanf("%i",&wt);
}
printf("\nEnter distanc(in mi.): ");
scanf("%i",&d);
if (d < 0 || d >3000) {
scanf("%i",&d);
}
if (wt <= 2 && d <= 500) {
printf("\tTotal cost: %1.2f",cost1);
}
if (wt >= 3 && wt <= 6 && d <= 500) {
printf("\tTotal cost: %1.2f",cost2);
}
if (wt >= 6 && wt <= 10 && d <= 500) {
printf("\tTotal cost: %1.2f",cost3);
}
if (wt >= 10 && wt <= 20 && d <= 500) {
printf("\tTotal cost: %1.2f",cost4);
}
return 0;
}

Well, some questions/remarks:
1) Don't you understand your assignment?
2) When is it due?
3) Try to explain in plain language what you try to do in this program
4) Post your actual code and not some snippet that's incomplete:
- cost1, cost2 , cost3, cost4 are neither defined nor initialized
- there is no "main" statement
5) Be as precise as possible as nobody in this NG likes guessing
- in your example code aren't any loops at all, so what's
your question about C and loops?

Let me guess - your assignment reads more or less like this:
"Write a calculator for a parcel service. The rates are calcuted from
a weight depend base price and the distance of transports."
(Then there is a neat table giving the 4 weight ranges and the price
including the first 500 miles.)
For longer distances an additional charge of 1.10 per 500 miles has to
be added the the base price." (some examples of correct freight rates
follow)

Good guess, isn't it?
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.