# Thread: Help with making a function

1. ## Help with making a function

Dear friends,

I am trying to make a function to calculate total resistance in of parallel reistors, this function should keep asking for entering the resistors until a negative number is entered.

As you may know formula to calculate total resistance of 2 parallel resistor is

(1/(1/Ra)+(1/Rb))

ofcourse this function should calculate parallel resistance of as many as resistors that are being entered untill a negative number is entered.

I wrote this function but it is not working good, would be nice if you take a look and made some suggestiions or fixitation!

Code:
```	double R_Parallel(void) {
int nHelp, nCount = 1; // Help and Counter
double fPRE; 		 //Pralaller Resistance Equivelant
double fResistor;  //Resistor

while (fResistor >= 0) { //WHILE 1
printf("\nPlease input parallel resistant number %d,
\ninput a negative value to finish the operation: \n", nCount);
scanf("%lf", &fResistor);

//Special case for first resistor
if(nCount == 1) {fPRE = fResistor;};

nCount++;

fPRE = pow(((pow(fPRE, -1))+(pow(fResistor, -1))),-1);
return (fPRE);

}//END WHILE 1
}```

2. Why don't you use nHelp?
instead of pow -1 you can use 1/(1/fPRE + 1/fResistor). A simpler way would be (Ra + Rb) / (Ra * Rb).

And you should drop nCount, the while loop and fPRE and fResistor and ask the user to input two values stored in Ra and Rb then just calculate and return.

3. true, but with this function it will stop after enterin just 1 value, I want it to contine untill a negative value is entered.

4. Originally Posted by Saeid87
I wrote this function but it is not working good, would be nice if you take a look and made some suggestiions or fixitation!
You mean not working at all.

Here's an idea epitomized by the "reading comprehension" tests everyone did in grade school, where they give you a question loaded with irrelevant information and you have to sort it out. Your post, and your code, is mostly full of irrelevant information. This makes it harder for a stranger to analyse, and is probably distracting you too since you consider it important to explain some formula that is (again) irrelevant to your problem with program logic, but you have failed to make any effort to try and separate these issues.

What is important here? Is it the "formula to calculate total resistance of 2 parallel resistor". No, it is not. Is it that "this function should keep asking for [information] until a negative number is entered"? I think so. But you have done this ass backward. Before you try and complicate a basic task with these nitty gritty details (that have almost nothing to do with coding), make sure you can perform and understand the basic task.

Again the basic task that you have completely failed to implement here is collecting input until a negative number is entered. So forget the formula for now. Instead, just collect input in the simplest way and develop the basic structure of the function:
Code:
```double testfunc(void) {
double input = 0.0, total = 0.0;
while (1) {
scanf("%lf",&input);
if (input<0) break;
total += input;
}
}```
Hopefully you can see the structural difference between this and your function and it has nothing to do with calculating resistance. Notice where the return statement is (outside the loop!)*

You can shorten it slightly by using the while (condition) if you want:
Code:
```double testfunc(void) {
double input = 0.0, total = 0.0;
while (input >= 0) {
total += input;
scanf("%lf",&input);
}
}```
If you don't think I'm a jerk and you want my help again, you will have to put some time into intelligently formulating your problem first, or I will not have time to deal with it.

*it could be inside the loop as part of the if (input), but never without some condition -- otherwise there is no loop, since the first time it will just reach return no matter what.

5. Thanks mate, I took a while and read your post carefully. you are absloutly right about my way of questioning.

I tried your first code to get the idea, it works prefectly. Now I try to state the problem as clear as I can.

I need a function to calculate total resistance of as many as resistors in parallel, until a negative value is entered. we already now the formula for calculating total resistance of two resistors in parallel is:

1/((1/Ra)+(1/Rb))

But there is a special case, if only one value is entered, that is only 1 resistor, the total resistance is equal to the value of that resistor. so we have to have a condition to take care of this, that is if first input value is "100" and second is "-1" which second one should break the operation, the function should return "100".

well so far, lets say we have 3 inputs: 10, 15 and 20

according to the formula, total resistance for the first two values would be:
1/((1/10)+(1/15))
and this will be equal to 6, that is now we need to put 6 and third input, 20, to the formula wich will result in 4.615

we have to keep same priciple until a negative value is entered.

I really have idea how to do this, but since it is my first exprience with programming I dont know the right way to do it. I really appriciate any help.

6. Well guys I think I did it
Code:
```double R_Parallel(double fUin, double fRo) {
// Internal variables for function
int nHelp, nCount = 0; // Help and Counter
double fPRE; 		 //Pralaller Resistance Equivelant
double fResistor;  //Resistor
double fUout; //Uout is the final output voltage after the voltage divider

while (1) { //WHILE 1
printf("\nPlease input parallel resistant number %d,\ninput a negative value to finish the operation: \n", nCount);
scanf("%lf", &fResistor);
if(fResistor <= 0) { printf("\nStopping the program since a negative value is entered, the last calulated output voltage is %f Volts.", fUout); break;}
nCount++;

// Special case for first entered resistor, it will be in series with Ro
if(nCount == 1) {fPRE = fResistor;}

// Calculating total resistance for more thatn
if(nCount != 1){
fPRE = 1/((1/fPRE)+(1/fResistor));
}

fUout = (fRo/(fRo+fPRE))*fUin;

printf("\nU out is now %f Volts.\n", fUout);

}//END WHILE 1

return (0);

}```