# C (Not C++) Homework Help

1. Oct 5, 2008

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. Oct 5, 2008

### mgb_phys

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?

3. Oct 5, 2008

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");

while(1)
{
"Press \"0\" at anytime to terminate the program. \n\n");
scanf("%lf", &r);

if (r == SENTINEL || r < SENTINEL)
{
break;
}

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");

system("pause");
return 0;
}

4. Oct 5, 2008

### Bill_B

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.

5. Oct 5, 2008

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.

6. Oct 5, 2008

### Bill_B

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.

7. Oct 5, 2008

### mgb_phys

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?