Calculating resistance

Reply

Join Date: Nov 2008
Posts: 5
Reputation: kbpszs is an unknown quantity at this point 
Solved Threads: 0
kbpszs kbpszs is offline Offline
Newbie Poster

Calculating resistance

 
0
  #1
Nov 20th, 2008
Ok well my assignment is to write a program to gives me the total resistance in a circuit.
The user is prompted for the resistance type and resistor value such as <1 200> 1 meaning in series, and 200 being the value of the resistor. When the user imputs -1 for the type the program stops and gives the total resistance. 0 goes for the starting resistor, then 1 is for series, and 2 for parallel. Here is what i have so far, I know its not calculating correctly and I can understand what to do in my head, I just cant translate it to code :/ any help or suggestions would be great, thanks

An example of what a user would input would be
"Input resistance type(0 starting: 1 series: 2 parallel) and resistor value"
0 100
1 100
1 100
2 200
2 200
1 25
2 200
1 50
-1
"total resistance is" 100

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int code=0; //This is the connectivity type
  5. double rv=0;//This is the resistor value
  6. double total=0;
  7. double sumD=0; //Sum of the denominator
  8.  
  9.  
  10. while (code!=-1){
  11. cout<<"Input connectivity type(0 starting: 1 series: 2 parallel) and Resistor Value:";
  12. cin>>code>>rv;
  13.  
  14. if (code==1){
  15. total += rv;}
  16. else {
  17. sumD += 1.0/rv;}
  18. }
  19. if (code==2){
  20. total += 1.0/(sumD);}
  21. cout<<total<<endl;
  22. return 0;}
Last edited by kbpszs; Nov 20th, 2008 at 3:04 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,747
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Calculating resistance

 
0
  #2
Nov 20th, 2008
	if (code==1){
		total += rv;}
	else {
		sumD += 1.0/rv;}
	}
if (code==2){
	total += 1.0/(sumD);}

Both red lines of code execute when code == 2. Is this what you want?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: kbpszs is an unknown quantity at this point 
Solved Threads: 0
kbpszs kbpszs is offline Offline
Newbie Poster

Re: Calculating resistance

 
0
  #3
Nov 20th, 2008
yes they are both needed to calculate the resistance for when the user inputs 2 for parallel.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,747
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Calculating resistance

 
0
  #4
Nov 20th, 2008
It might be helpful to give us some exact sample input, along with the exact output that should occur for that input.

Edit: Whoops. Just saw some in the first post. Disregard.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: kbpszs is an unknown quantity at this point 
Solved Threads: 0
kbpszs kbpszs is offline Offline
Newbie Poster

Re: Calculating resistance

 
0
  #5
Nov 20th, 2008
I added it to my original post but ill add it here as well:

An example of what a user would input would be
"Input resistance type(0 starting: 1 series: 2 parallel) and resistor value"
0 100
1 100
1 100
2 200
2 200
1 25
2 200
1 50
-1
"total resistance is:" 100
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,747
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Calculating resistance

 
0
  #6
Nov 20th, 2008
Originally Posted by kbpszs View Post
Ok well my assignment is to write a program to gives me the total resistance in a circuit.
The user is prompted for the resistance type and resistor value such as <1 200> 1 meaning in series, and 200 being the value of the resistor. When the user imputs -1 for the type the program stops and gives the total resistance. 0 goes for the starting resistor, then 1 is for series, and 2 for parallel. Here is what i have so far, I know its not calculating correctly and I can understand what to do in my head, I just cant translate it to code :/ any help or suggestions would be great, thanks

An example of what a user would input would be
0 100
1 100
1 100
2 200
2 200
1 25
2 200
1 50
-1
And the total resistance would be 100.
Maybe I am misunderstanding the input. I don't understand the (0, 100) pair. "Starting resistor"? Regarding connecting resistors in series, you have 100, 100, 25, 50 in series, so at minimum, you need to add those up, right, before you even start with the parallel, which puts you way over 100 ohms total. Can you post the mathematical formula that derives 100 from those numbers? Perhaps upload a schematic? I don't think I'm picturing the circuit.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: kbpszs is an unknown quantity at this point 
Solved Threads: 0
kbpszs kbpszs is offline Offline
Newbie Poster

Re: Calculating resistance

 
0
  #7
Nov 20th, 2008
I drew up a picture of the circuit, its supposed to be calculated from right to left, and yes 0 implies its the starting resistor. The picture should be attached.
Attached Images
File Type: jpg rr.JPG (8.5 KB, 7 views)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,747
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 491
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: Calculating resistance

 
2
  #8
Nov 20th, 2008
I see how you got 100. I would say that you are assuming a lot if you are assuming that people will know that that is the circuit you are intending with that input. You should spell out the rules a little more, in my opinion, but if everyone knows how to enter the circuit (I THINK I understand the rules and the assumptions you are making regarding how a circuit is created from the input), your calculation boils down to this:

100 + 100 + 100 = 300 (first three resistors are in series). Replace with a single 300 ohm resistor.

1 / (1/300 + 1/200 + 1/200) = 75. Replace these parallel resistors with a single 75 ohm resistor.

75 + 25 = 100 (replace these two resistors in series with a single 100 ohm resistor.

1 / (1/100 + 1/100) = 50

50 + 50 = 100, which is the answer.

So make two functions. One that takes an array of resistors in series and calculates their resistance, one that takes an array of resistors in parallel and calculates their resistance. I'd change the "starting resistor" to simply a "series" resistor, since it's the same thing in your schematic. You'll need to set up an array to read the input into, then when it changes from series to parallel or vice versa, call the appropriate function. But start with writing these functions:

  1. double SeriesResistance (int resistorValues[], int numResistors)
  2. {
  3. // returns resistance of the resistors in series
  4. }
  5.  
  6. double ParallelResistance (int resistorValues[], int numResistors)
  7. {
  8. // returns resistance of the resistors in parallel
  9. }

Then go from there. You may want to set up a struct for input since you are dealing with paired data.


Yee-ha! Post number 2000!
Last edited by VernonDozier; Nov 20th, 2008 at 3:53 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 5
Reputation: kbpszs is an unknown quantity at this point 
Solved Threads: 0
kbpszs kbpszs is offline Offline
Newbie Poster

Re: Calculating resistance

 
0
  #9
Nov 20th, 2008
ok cool, thanks alot for all your help, it is much appreciated
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC