# Nested Loop

Page 1 of 1

## 4 Replies - 4500 Views - Last Post: 29 January 2013 - 05:44 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=276880&amp;s=e264b8fa2aa3ed8c3639b3ff843e9597&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Drazzminius

Reputation: 0
• Posts: 10
• Joined: 07-December 09

# Nested Loop

Posted 25 April 2012 - 10:40 PM

I'm trying to get a calculation to work for a nested loop, but I'm having a problem with the initial calculation.

Quote

Write a program to 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 distances of 20, 30,
50, 50, ..., 100 miles at 100V and 200V.

power transmitted in watts
i = ------------------------------
volts transmitted

The total resistance R is computed from the equation:

R = r x miles

where r is the resistance per mile.

The power loss is computed from the equation:

Power loss = i^2 x R.

Here is my code:
```#include <stdio.h>
#include <iostream>

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 %8d\n", v*100, val, pwr_loss);
val = 10 + val;
pwr_loss = (0.05 * val)*(i*i);
}
printf("\n\n");
system("PAUSE");
return 0;

}
```
I don't want exact answers necessarily, I'm trying to figure out why I can't get the code to run. I keep getting zero's for the power loss.

Any nudges in the right direction are appreciated.

Is This A Good Question/Topic? 0

## Replies To: Nested Loop

### #2 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Nested Loop

Posted 25 April 2012 - 10:45 PM

The error resides in your print statement, make sure you use the correct format when printing floating point values (%f)
```printf("\t %7d \t %8d  \t %8f\n", v*100, val, pwr_loss); //changed %8d to %8f

```

### #3 Drazzminius

Reputation: 0
• Posts: 10
• Joined: 07-December 09

## Re: Nested Loop

Posted 26 April 2012 - 04:41 AM

Awesome! Thank you for the quick reply!!!

### #4 mtrouard29

Reputation: 0
• Posts: 1
• Joined: 29-January 13

## Re: Nested Loop

Posted 29 January 2013 - 01:23 PM

how do you add the nested loop for voltage of 200?

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: Nested Loop

Posted 29 January 2013 - 05:44 PM

It wouldn't be a nested loop, it would be a second for loop - very similar to the first one, but using the higher voltage, and using changed variables (like the ones listed above the for loop, here). Change them appropriately for the higher voltage, etc., and then add the second for loop for these new equations.