Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C (Not C++) Homework Help

  1. Oct 5, 2008 #1
    Hey guys, our homework requires us to write the code to calculate the total resistance of a circuit for "n" number of resistors in parallel. My question is what kind of coding scheme is required for the "n" number of resistors which basically means "infinite" number of resistors?

    I know there is a while loop involved, however, I cannot seem to figure out how to make the program accept an infinite number of resistor values?

    Your help is greatly appreciated. Thanks.
  2. jcsd
  3. Oct 5, 2008 #2


    User Avatar
    Science Advisor
    Homework Helper

    They probably don't want an infinite number, it would take rather a long time to enter!
    Look at the formula for resistors in parallel, do you need to store all the individual values to calculate th etotal resistance so far?
  4. Oct 5, 2008 #3
    Yes, the program needs to be able to store the values for it to calculate the total resistance for a given "n" number of resistors. This is what I have coded so far in my C program,

    #include <stdio.h>

    #define SENTINEL 0

    int main()

    double r;
    double value;
    double total = 0.0;
    double resistanceTotal;

    printf("Welcome, this program is designed to calculate the \n"
    "total equivalent resistance of a circuit for a given \n"
    "\"n\" number of parallel resistors. \n\n");

    printf("\nPlease enter the value of your resistor(s) now! \n"
    "Press \"0\" at anytime to terminate the program. \n\n");
    scanf("%lf", &r);

    if (r == SENTINEL || r < SENTINEL)

    else if (r > SENTINEL)
    value = 1/r;
    total += value;
    resistanceTotal = 1/total;
    printf("%2.2lf ohms", resistanceTotal);


    printf("\nProgram Author: Vladimir S. Lolinco \n\n");

    return 0;
  5. Oct 5, 2008 #4


    User Avatar
    Gold Member

    This can be done in a number of ways. Here's two -

    1. Ask the user how many resistors they have, then use that input to set up the loop.

    2. Choose a value to terminate the loop (q for quit, or 0, or another similar invalid input) and then just keep looping until the user enters the "end" value.
  6. Oct 5, 2008 #5
    So, I know I would declare the number of resistors the user has as a double, but where would I place that "value" in my while loop? Would that be part of a calculation in the while loop or outside of it? Am I even on the right track for my calculations? Thanks.
  7. Oct 5, 2008 #6


    User Avatar
    Gold Member

    Since you're using a while loop, option 2 is probably a better bet. Plus, that seems to be what you're doing anyway.

    If you were using a for loop, then option 1 might be a more natural fit. You CAN use option1 with a while loop (by using a counter variable inside the loop) but IMO it's not as clean.

    Really, you're almost there with what you have. Think some more about what mgb_phys posted, and consider what should be inside the loop and what should be outside.
  8. Oct 5, 2008 #7


    User Avatar
    Science Advisor
    Homework Helper

    Isn't the resistance of a bunch of resistors in parallel
    1/R =1/R1 + 1/R2 + .... 1/Rn
    If you are given the resistances one at a time, do you need to store all of them?
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: C (Not C++) Homework Help
  1. C++ homework (Replies: 14)

  2. C++ Homework (Replies: 12)

  3. Help (Not Homework) (Replies: 5)