4 Replies - 2599 Views - Last Post: 29 January 2013 - 05:44 PM Rate Topic: -----

#1 Drazzminius  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

The question reads:

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  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • 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


Was This Post Helpful? 2
  • +
  • -

#3 Drazzminius  Icon User is offline

  • New D.I.C Head

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

Re: Nested Loop

Posted 26 April 2012 - 04:41 AM

Awesome! Thank you for the quick reply!!!
Was This Post Helpful? 0
  • +
  • -

#4 mtrouard29  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1