Thread: help with functions!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    12

    help with functions!

    have created a code for solving time capacitor takes to charge up at certain percentages but have run into difficulties.

    1.answers i get for time seem to be off ( have worked out examples on paper mathemtically to test this ) wondering if anyone can see where ive gone wrong!

    2. for extra marks would like to make use of functions in my code any help would be greatly appreciated. ( as far as i can tell there is only need for one function with three variables in but ive been wrong before)

    Code:
    #include <math.h>#include <stdio.h>
    void main()
    {
        float r;//declaration of resistance variable
        float c;//declaration of capacitor varablle
        float pv;//declaration of percentage variable as a raio of 100%=1.0
        float t;// declaration of the time variable
        
    
    
        printf("please enter the value of the resistor in the circuit\n");
        scanf("%f",&r);
        printf("please now input the value of the capacitor in the circuit\n");
        scanf("%f", &c);
        printf("please now enter the percentage value you would like to investigate as a ratio of 0-1 ( 100%=1.0 )\n");
        scanf("%f", &pv);
    
    
    t=-r*c*log(1-pv);
     printf(" the amount of time passed for this given percentage is %f",t);
    
    
    
    
    
    
    
    
     getch();
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... first thing... only post plain text in code tags. The forum highlights it for you.

    You might want to change your floats to doubles for added precision. But do keep in mind that floating point math on PCs is an approximation at best. (Not every decimal value can be accurately represented in the binary storage used by computers)

    For the bonus points you could make the calculation into a function... It's about your only real candidate.

    Also I don't know if it's a copy-paste error or if it's a problem in your code, but you can't put two preprosessor commands on the same line.

    Finally... void main() is wrong... Windows and Linux (and probably Mac too) expect a return value from programs they run. The correct program skeleton is...
    Code:
    int main (void)
      {
    
         // your code here
    
         return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    12
    okay thanks, so how do i go about putting the calculation into a function? ( sorry if its a pretty basic question )

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... widen your field of view just a little...

    What is main() ... it's a function, yes?
    You know how that works.
    So you're already half way there.

    Fix the current problems, get it working *at all* then give it your best shot.
    Post your code if it gives you trouble...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  2. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  3. calling functions from functions
    By algi in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2004, 06:16 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread