Parallel Resistance
i am trying to input and calculate this for any given number of resistors and any value of the resistors.
the formula is

1/R= 1/R1+1/R2+1/R3.........+!/Rn

Could someone tell me how to input this into C++
could u explain a little on how to do this aswell.

Thank you

Ps it would be very helpful if anybody knows how to input this in to MATLAB 7 aswell. Thank you
I know its asking alot but man was made to help man
its in our nature

Recommended Answers

All 21 Replies

I'm no math whiz but isn't that formula the same as
R =1 / (r1 + r2 + ... rn)

so first you need the values for r1, r2 .. rn. where do those come from? put them into an array then you can easily calculate the formula (this has not been compiled or tested).

float foo(vector<int> array)
{
   int sz = array.size();
  float R = 0;
  for(int i = 0; i < sz; i++)
     R += array[i];
  return 1.0/R;
}

I'm no math whiz but isn't that formula the same as
R =1 / (r1 + r2 + ... rn)

No it isn't.
Take for example two resistances. That is the simplest case. 1/R = 1/R1 + 1/R2 => R = (R1)(R2)/(R1 + R2 )

Member Avatar for iamthwee

No it isn't.
Take for example two resistances. That is the simplest case. 1/R = 1/R1 + 1/R2 => R = (R1)(R2)/(R1 + R2 )

awww, when you get to his age... heh heh (joke sorrie) ;)

No it isn't.
Take for example two resistances. That is the simplest case. 1/R = 1/R1 + 1/R2 => R = (R1)(R2)/(R1 + R2 )

Ok so my math was wrong. But that is still pretty simple to program once the values of R1 ... Rn are known and in an array or file. The danger, of course, is with math overflow -- R1 * R2 * ... Rn can become a very very huge number that no computer on this planet has enough memory to hold when n approaches infinity. So using computer programs is only feasible for pretty small values of n.

I suggest that you try taking a float variable, initialize it with 0, and implement a while loop which would terminate on pressing a pre-defined key, in each iteration of the loop tell the usr to enter a number(i.e. the resistance value) and find its inverse and add it to the float variabe.
When you exit the loop invert that float variable to get the effective parallel resistance.
it would look something like this.

float res_eff=0.0,res_new;
do
{

cout<<"\n Enter the resistance value ";
cin>>res_new;
res_eff+=1/res_new;
cout<<"\n Want to enter more? ";
cin>>ch;
}
while(ch='a'||ch='A');
res_eff=1/res_eff;
cout<<"\n Net value of resistance = "<<res_eff;

that is not the right formula either, but it has the right idea. Entering the numbers from the keyboard will get very very timesome if you have to run the program quite a few times for debugging and testing. -- better to just hard-code the numbers in the program or save them in a file, makes debugging a whole lot easier.

you need two float variable -- numerator and denominator.

float numerator = 0, denominator = 0;
vector<float> array;
for(i = 0; i < array.size(); i++)
{
   numerator *= array[i];
   denominator += array[i];
}

float result = numberator / denominator;

I would set up a vector of doubles, load it with the resistance values using push_back(). This will take care of size too. You can load from a file or user input.

Now loop through the vector and invert all the resistance values. You can sum up the inverted resistance values with accumulate(). The result needs to be inverted to get the parallel resistance.

that is not the right formula either, but it has the right idea. Entering the numbers from the keyboard will get very very timesome if you have to run the program quite a few times for debugging and testing. -- better to just hard-code the numbers in the program or save them in a file, makes debugging a whole lot easier.

you need two float variable -- numerator and denominator.

float numerator = 0, denominator = 0;
vector<float> array;
for(i = 0; i < array.size(); i++)
{
   numerator *= array[i];
   denominator += array[i];
}

float result = numberator / denominator;

No I guess it is correct. I should have been a bit generic when explaining my simplification of the formula.
It is only when there is two resistances that R becomes R=(R1*R2)/(R1 + R2 )= ( Product / Addition ) For 3 resistances R=(R1)*(R2)*(R3)/(R1*R2+R2*R3+R3*R1 ) For 4 Resistances R=(R1)*(R2)*(R3)*(R4)/(R1*R2*R3+R2*R3*R4+R3*R4*R1 + R4*R1*R2 ) ...
As you can see this gets tedious ( but not impossible; just the use of some permutations and combinations) to simplify when the number of resistances involved get larger. So the easiest way is to add the reciprocals and take the overall reciprocal as the final answer.
But as Dragon pointed out, it should be checked for division from zero and overflow, and possibly underflow. A more refined approach of ani_manit would do, with the values entered from a text file.

Wow! what a screwed-up mess that can become! I would be tempted to change careers very quickly.

Wow! what a screwed-up mess that can become! I would be tempted to change careers every quickly.

Yep. I know I did. :D

This program works...
I don´t know if it's like this that youi want:

#include "stdafx.h"
#include <iostream.h>
#include <stdio.h>


int main(int argc, char* argv[])
{
	int no_values;
	
	float res_eff = 0.0,
		  res_new,
		  denominator = 0.0;

	printf("\nHow many input resistance do you want? ");
	scanf("%d", &no_values);

	int k=0;
	do
	{
		cout << "\n Enter the resistance value: ";
		cin >> res_new;
		denominator = denominator + 1 / res_new;
		k++;
	}
	while ( k < no_values);
	
	res_eff = 1 / denominator;
	
	cout<<"\n Net value of resistance = " << res_eff << "\n";
			
	return 0;
}

regards

This program works...
I don´t know if it's like this that youi want:
//snip

Most probably not! choose a language, either C or C++, and stick to it.. mixing the two like that is really a very very bad idea.

Most probably not! choose a language, either C or C++, and stick to it.. mixing the two like that is really a very very bad idea.

Yeh, but it works...
but I can put like this:

int no_values;
	
	float res_eff = 0.0,
		  res_new,
		  denominator = 0.0;

	cout << "\nHow many input resistance do you want? ";
	cin << no_values;

	int k=0;
	do
	{
		cout << "\n Enter the resistance value: ";
		cin >> res_new;
		denominator = denominator + 1 / res_new;
		k++;
	}
	while ( k < no_values);
	
	res_eff = 1 / denominator;
	
	cout<<"\n Net value of resistance = " << res_eff << "\n";
			
	return 0;
}

Do you agree now?

;)

Member Avatar for iamthwee

>Wow! what a screwed-up mess that can become! I would be tempted to change careers very quickly

What career can one change to when they're in an old folks home?

;)

>Wow! what a screwed-up mess that can become! I would be tempted to change careers very quickly

What career can one change to when they're in an old folks home?

;)

Fish'n :cheesy:

You might be surprised by the amount of grey hair at the casino's! (And 't aint all from stress of loosing neither!)

none of the programs have worked
could someone please write a program and test in and post it on here plz
this looks like its going to make me fail
i dont think anything has pissed me off soooo much
thnak you people

Member Avatar for iamthwee

none of the programs have worked
could someone please write a program and test in and post it on here plz
this looks like its going to make me fail
i dont think anything has pissed me off soooo much
thnak you people

I tell you what..why don't you just write it yourself...

none of the programs have worked
could someone please write a program and test in and post it on here plz
this looks like its going to make me fail
i dont think anything has pissed me off soooo much
thnak you people

I already gave you the answer!

If the program doesn't works, it's because you're doing it wrong...

Try to do something on your own.... and right!

dude thanks for the question because im a huge electronics geek an i had no idea your could use programing with ohms law and other electronic equasions hey i got an idea im going to use a program to calculate electromagnetic induction um to help you out you first need to do the math on paper with a scmatic of a circuit example that u could use for this and then convert your processes leading to your answer, but in c++ santax of your knowledge

// TITLE: resistors in series

#include <stdio.h>

main ()
{

float Resistor1 ;
float Resistor2 ;
float Resistor3 ;
float TotalResistance ;
printf ("\n\n PROGRAM TO CALCULATE SERIES & PARALLEL RESISTANCE ");
printf ("\n________________________________________________________\n\n") ;

printf (" Enter the value of first resistor :");
scantf ("%f", &Resistor1) ;

printf ("\n Enter The value of second resistor :");
scantf ("%f", &Resistor2) ;

printf ("\n Enter the value of third resistor :");
scantf ("%f", &Resistor3) ;

TotalResistance = (Resistor1)+(Resistor2)+(Resistor3) ;
printf ("%n The series resistance is #.2f ohm",TotalResistance);

TotalResistance = (1)/(1/Resistor1)+(1)/(2/Resistor2)+(1)/(3/Resistor3);
printf ("\n The Parellel resistance is #.2f ohm",TotalResistance);
printf("\n")
printf("\n")
printf("CALCULATION FOR SERIES RESISTANCE\n");
printf("INSERT RESISTANCE VALUE\n";
return 0;
}


pls help me..

commented: respond to 6 year old thread -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.