C-loops explain plz

T

Tweaxor

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 :)
 
K

Kris Wempa

Tweaxor said:
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.
 
I

Irrwahn Grausewitz

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
 
T

Tweaxor

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;
}
 
C

Chiron Paixos

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top