Help with looping

This is a discussion on Help with looping within the C Programming forums, part of the General Programming Boards category; Hello, I have the following code that works great: Code: #include <stdio.h> int main(void) { int v=1; int dist=1; int ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    9

    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

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    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.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,390
    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.
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    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 View Post
    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.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    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.

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    9
    you are exactly right! thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with looping
    By Sshakey6791 in forum C++ Programming
    Replies: 6
    Last Post: 01-13-2009, 08:49 PM
  2. Need help with Looping
    By Harryt123 in forum C Programming
    Replies: 11
    Last Post: 05-21-2006, 01:16 PM
  3. help with looping.
    By tdoctasuess in forum C Programming
    Replies: 1
    Last Post: 05-13-2004, 01:00 PM
  4. Looping
    By hmr_smpsn_57 in forum C++ Programming
    Replies: 15
    Last Post: 02-24-2004, 04:08 PM
  5. Looping
    By Vorkosigan in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2004, 06:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21