help! calculate arbitrary number of floating points

This is a discussion on help! calculate arbitrary number of floating points within the C Programming forums, part of the General Programming Boards category; hello i am new to c programming. i need to enter an arbitrary number of floating points and plug these ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    36

    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;
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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]
    Last edited by MK27; 02-26-2009 at 01:33 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hey wow, "1" does work here. If that's what the OP wants...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    THANKS!!!! is there any way that i can get it to give me the result by pressing EOF( ctrl Z)???

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cakestler View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21