# Help with looping

• 09-16-2012
john3j
Help with looping
Hello,

I have the following code that works great:

Code:

```#include <stdio.h> int main(void) { int v=1; int dist=1; int i = (500 / 100); float r = 0.05; int val = 20; float R = 0.05 * val; float pwr_loss = (val * r) * (i * i);     printf("\t  Voltage  \t  Distance  \t  Power Loss\n");       printf("\t=============================================\n"); for (dist=1; dist<=9; dist++) {     printf("\t %7d \t %8d  \t %8f\n", v*100, val, pwr_loss);     val = 10 + val;     pwr_loss = (0.05 * val)*(i*i);   }     printf("\n\n");     system("PAUSE");   return 0; }```
How could I get it to run through the loop for a second time with
Code:

`int i = (500 / 100);`
changing to
Code:

`int i = (500 / 200);`
?

The output would give you the voltage, distance, and power loss with a voltage of 100 counting to 100 mile distance. It would then output the voltage, distance, and power loss with a voltage of 200 counting to 100 mile distance.

Any help would be greatly appreciated!

Thanks,
John
• 09-16-2012
rags_to_riches
Here's a hint for you. For loops can start at any number, end at any number, and be incremented by numbers other than 1.
• 09-16-2012
oogabooga
What do you expect i's value to be after i = 500 / 200 ?
It will be exactly 2, not 2.5 if that's what you were expecting.
It you need it to be 2.5, then try
Code:

`float i = 500.0 / 200.0;`
To do what you want you should first rewrite the code as a function that you call from main and give it whatever parameters you need (in particular you should pass in i).

You should also give i a better name. :)
• 09-16-2012
Click_here
Quote:

Originally Posted by john3j
How could I get it to run through the loop for a second time...

Another for loop around that for loop.

Code:

``` for (dist=1; dist<=9; dist++) {   ... } Becomes for (loop_counter=0; loop_counter<=2; loop_counter++) {     for (dist=1; dist<=9; dist++)     {         ...     }     i = ...; }```
Also note what oogabooga said about the data type.
• 09-20-2012
john3j
I actually went through the code and rewrote it. Here is what I have:

Code:

```/****************************************************************/ /*        CS263 Programming in C                                    */ /*        Assignment #3 Page 261                                    */ /*        Written by: John Strange                                */ /*        Date: 9/20/2012                                            */ /*                                                                */ /*                                                                */ /*  Compute the power loss in a transmission line with a      */ /*  resistance of 0.05 ohms/mile. Compute the power loss if    */ /*  500 kW of power is transmitted from a power generating    */ /*  station to cities at a distance of 20, 30, 40, 50,...,    */ /*  100 miles at 100 V and 200 V.                              */ /*                                                                */ /****************************************************************/ /*                                                                */ /*            dist = Distance                                    */ /*                                                                */ /*            page = Page number                                    */ /*                                                                */ /*            voltage = Voltage                                    */ /*                                                                */ /*            i = Current                                            */ /*                                                                */ /*            resist = Initial Resistance                            */ /*                                                                */ /*            R = Total Resistance                                */ /*                                                                */ /*            pwrLoss = Power Loss                                */ /*                                                                */ /****************************************************************/ #include <stdio.h> #include <math.h> int main() { int dist = 20; int page = 0; int voltage; float i; float resist = 0.05; float R; float pwrLoss; for(voltage = 100; voltage <= 200; voltage += 100) {     i = (500 / voltage); page = page + 1; printf("                                        "); printf("                                    %d\n", page); printf("\n                          Voltage: %d\n", voltage); printf("                          ____________ \n"); for(dist = 20; dist <= 100; dist += 10) {     R = (resist * dist); pwrLoss = (R * (i * i)); printf("\n            Distance                    Power Loss"); printf("\n            ________                    __________"); printf("\n              %d", dist); printf("                        %8.2f\n", pwrLoss); } } return 0; }```
Here is the output I get when I run it...

Voltage: 100
____________

Distance Power Loss
________ __________
20 25.00

Distance Power Loss
________ __________
30 37.50

Distance Power Loss
________ __________
40 50.00

Distance Power Loss
________ __________
50 62.50

Distance Power Loss
________ __________
60 75.00

Distance Power Loss
________ __________
70 87.50

Distance Power Loss
________ __________
80 100.00

Distance Power Loss
________ __________
90 112.50

Distance Power Loss
________ __________
100 125.00

Voltage: 200
____________

Distance Power Loss
________ __________
20 4.00

Distance Power Loss
________ __________
30 6.00

Distance Power Loss
________ __________
40 8.00

Distance Power Loss
________ __________
50 10.00

Distance Power Loss
________ __________
60 12.00

Distance Power Loss
________ __________
70 14.00

Distance Power Loss
________ __________
80 16.00

Distance Power Loss
________ __________
90 18.00

Distance Power Loss
________ __________
100 20.00

The problem is that it is still not computing properly when the voltage is 200. Can anyone help me figure out why?

Thanks,
John

Quote:

Originally Posted by Click_here
Another for loop around that for loop.

Code:

``` for (dist=1; dist<=9; dist++) {   ... } Becomes for (loop_counter=0; loop_counter<=2; loop_counter++) {     for (dist=1; dist<=9; dist++)     {         ...     }     i = ...; }```
Also note what oogabooga said about the data type.

• 09-20-2012
rags_to_riches
I'm guessing you're running into integer division here, when voltage is 200:

Code:

`i = (500 / voltage);`
You've declared i as an integer, which means the decimal generated here is lost.
• 09-20-2012
john3j
you are exactly right! thank you very much!