# help! calculate arbitrary number of floating points

Printable View

• 02-26-2009
cakestler
help! calculate arbitrary number of floating points
hello i am new to c programming. i need to enter an arbitrary number of floating points and plug these numbers inthe the formula
1/Rn = 1/R1 + 1/R2 + ....+ 1/Rt

i need to do this in ohms so the 1/Rn, so the answer will end up being the reciprocal of the numbers after they are added together. i am using a while, break loop...is this right? here is what i have so far if anyone has any suggestions. THanks!

Code:

``` int main() {   double r1;   double resistance = 0.0;     printf(" This program calculates the total resistance \n");   printf("of n resistors connected in parallel.\n");   printf("\n");   printf("Enter the resistance or EOF to end input:\n");   while ( 1 )  {       scanf("%lf", &r1);       if ( r1 == (EOF || 0) )         break;       resistance += (1/r1);       }   if (resistance == 0)       printf("\nNo values entered.\n\n");   else       printf("\nTotal resistance of the ciruit is %.3f ohms\n", 1/resistance);         system("pause");   return 0; }```
• 02-26-2009
MK27
I think you will get stuck looking for the "EOF" key on this one.

ps. This won't work:
Code:

`if ( r1 == (EOF || 0) )`
you meant
Code:

`if ( (r1 == EOF) || (r1 == 0) )`
...tho as just implied r1 will never really == EOF [edit: yes it will, sorry]
• 02-26-2009
matsp
Code:

`if ( r1 == (EOF || 0) )`
This almost certainly isn't doing what you expect it to do.

First of all, IF the read of the input results in an EOF situation, the EOF is not stored in r1 - it is returned as the result of the scanf() operation.

Second, r1 == (EOF || 0) is not the same as ((r1 == EOF) || (r1 == 0)), which is probably what you meant.

EOF || 0 is probably equal to numerically 1, but some compilers could possibly produce something else, like -1. And that's what r1 is compared to.

--
Mats
• 02-26-2009
MK27
Hey wow, "1" does work here. If that's what the OP wants...
• 02-26-2009
cakestler
THANKS!!!! is there any way that i can get it to give me the result by pressing EOF( ctrl Z)???
• 02-26-2009
matsp
Quote:

Originally Posted by cakestler
THANKS!!!! is there any way that i can get it to give me the result by pressing EOF( ctrl Z)???

Yes, check if scanf() returns EOF.

Code:

```  int ret; ...   ret = scanf(...);   if (ret == EOF) ...```
You have to fill in the details, but it shows the principle.

--
Mats