C/C++ Calculating Total Resistance in an Infinite Parallel Circuit

  • Thread starter Thread starter vladittude0583
  • Start date Start date
  • Tags Tags
    Homework
AI Thread Summary
The discussion revolves around coding a program to calculate the total resistance of "n" resistors in parallel. The main focus is on how to handle an unspecified number of resistor values, with suggestions leaning towards using a while loop that continues until the user inputs a termination value, such as zero. The current code structure includes a loop that accepts resistor values and calculates the total resistance using the formula for resistors in parallel. Participants suggest that while the user could specify the number of resistors, a more flexible approach is to allow continuous input until a sentinel value is entered. There's a consensus that the program does not need to store all individual resistor values, as the calculation can be performed incrementally. The conversation emphasizes refining the loop structure and ensuring calculations are correctly placed within or outside the loop for optimal functionality.
vladittude0583
Messages
40
Reaction score
0
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.
 
Technology news on Phys.org
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?
 
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)
{
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)
{
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;
}
 
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.
 
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.
 
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.
 
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?
 

Similar threads

Replies
8
Views
4K
Replies
14
Views
1K
Replies
3
Views
2K
Replies
29
Views
10K
Replies
3
Views
2K
Replies
10
Views
2K
Back
Top