Like Tree1Likes
  • 1 Post By mintalbes

problem with functions & for loop !

This is a discussion on problem with functions & for loop ! within the C Programming forums, part of the General Programming Boards category; Hi I'm trying to write a code that will a print a table of results the values of first column ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    5

    problem with functions & for loop !

    Hi
    I'm trying to write a code that will a print a table of results
    the values of first column are known so I put them using for loop
    then i tried calling functions inside the loop to calculate the value fot the other columns
    the problem is that the third function depends on the value of the first & second functions but the values can not be sent there !
    here is the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define PI 3.141592653589793238462
     
    void get_values(double *, double*);
    void get_capacitance(double*, int, double);
    void get_inductance(double*, int, double);
    void get_attenuation(double,int, double, double);
    void get_attdb(double);
     
    int main(void)
    {
       double inductor, capacitor;
       int F;
       double capacitance, inductance, attenuation, attdb;
     
            
       get_values(&inductor, &capacitor);
      
       printf("L = %lf, C = %lf\n", inductor, capacitor);
      
       for (F=100;F<=1000;F=F+100)
       {
           printf("%i",F);
           printf("\t");
          
           get_capacitance(&capacitance,F,capacitor);
           printf("%lf", capacitance);
           printf("\t");
          
           get_inductance(&inductance,F,inductor);
           printf("%lf", inductance);
           printf("\t");
          
           get_attenuation(attenuation,F, inductance, capacitance);
           printf("%lf", attenuation);
           printf("\t");
          
           get_attdb(attenuation);
           printf("\t");
          
           printf("\n");
       }   
       system("PAUSE");
       return 0;
    }
     
     
    void get_values(double *L, double *C)
    {
       printf( "Please, enter the required inductor value");
       scanf("%lf", L);
            
       printf("Please, enter the required capacitor value");
       scanf("%lf", C);         
    }
     
     
    void get_capacitance(double *capacitance, int frequency, double C)
    {
        *capacitance =(((1/(2 * PI * frequency * C))));
       
    }
     
    void get_inductance(double *inductance, int frequency, double L)
    {
        *inductance = (2 * PI * frequency * L);
    }
     
    void get_attenuation(double attenuation,int F, double L, double C)
    {
         attenuation = (1/(1-(2*PI*F*2*PI*F*L*C)));
     
    }
     
    void get_attdb(double att)
    {
         printf("%lf",(20 * log10(att)));
     
    }

    thanks in advance
    Last edited by mintalbes; 04-17-2012 at 10:15 PM.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I think you mean to pass a pointer to the attenuation variable, rather than just the variable. Also, you never use attdb.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    thanks for your reply TheBigH
    I tried previously getting the capacitance & inductance values without using pointers but the same problem occured, when i send them to the attenuation functions i don't get the expected values.
    i want to know how can i get the value from the function get_capacitance and send it to get_attenuation ?

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    No, you're right to use pointers to get the capacitance and inductance values. You should also use a pointer for the attenuation.

    Passing just the variable to get_attenuation makes a copy of the variable which is discarded once the function ends. The changes you make to it are lost.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    i tried it again but the same problem
    the forth and fifth column values are not correct
    here is the last code i tried


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define PI 3.141592653589793238462
    
    void get_values(double *, double*);
    void get_capacitance(double*, int, double);
    void get_inductance(double*, int, double);
    void get_attenuation(double*,int, double, double);
    void get_attdb(double*, double);
    
    
    
    int main(void)
    {
       double inductor, capacitor;
       int F;
       double capacitance, inductance, attenuation, attdb;
    
             
       get_values(&inductor, &capacitor);
       
       printf("L = %lf, C = %lf\n", inductor, capacitor);
       
       for (F=100;F<=1000;F=F+100)
       {
           printf("%i",F);
           printf("\t");
           
           get_capacitance(&capacitance,F,capacitor);
           printf("%lf", capacitance);
           printf("\t");
           
           get_inductance(&inductance,F,inductor);
           printf("%lf", inductance);
           printf("\t");
           
           get_attenuation(&attenuation,F, inductance, capacitance);
           printf("%lf", attenuation);
           printf("\t");
           
           get_attdb(&attdb, attenuation);
           printf("%lf", attdb);
           printf("\t");
           
           printf("\n");
       }    
       system("PAUSE");
       return 0;
    }
    
    
    void get_values(double *L, double *C)
    {
       printf( "Please, enter the required inductor value"); 
       scanf("%lf", L);
             
       printf("Please, enter the required capacitor value");
       scanf("%lf", C);          
    }
    
    
    void get_capacitance(double *capacitance, int frequency, double C)
    {
        *capacitance =(((1/(2 * PI * frequency * C))));
        
    }
    
    void get_inductance(double *inductance, int frequency, double L)
    {
        *inductance = (2 * PI * frequency * L);
    }
    
    void get_attenuation(double *attenuation,int F, double L, double C)
    {
         *attenuation = (1/(1-(2*PI*F*2*PI*F*L*C)));
    
    }
    
    void get_attdb(double *attdb, double att)
    {
         *attdb =(20 * log10(att));
    
    }

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    What values do you expect to get, and what are you getting?
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    first the user is asked to enter two values
    the first should be 0.01
    the second 0.000001
    then you get results for five columns

    frequency ____ inductance ____ capacitance ___ attenuation __ attenuation dB
    100 _________ 6.283 _________ 1591.549 _____ 1.004 _______ 0.034
    200 _________12.566 _________ 795.775 ______ 1.016 _______ 0.138
    300 ________ 18.850__________ 530.517_______ 1.037_______ 0.314
    etc

    i'm getting for attenuation -0.000000 for all rows
    and for attdB 1.#IND00
    Last edited by mintalbes; 04-17-2012 at 10:45 PM.

  8. #8
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    I think your formula for attenuation is wrong.

    If I work it out by hand, using the values of inductance and capacitance from the program output, I get very small negative values for the attenuation.
    Code:
    while(!asleep) {
       sheep++;
    }

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    5
    it seems that was indded the problem because i treied simple formula and it worked
    thank you very much for your help
    TheBigH likes this.

  10. #10
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    No problem.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using for loop and functions to create a basic menu
    By sunandstars in forum C Programming
    Replies: 2
    Last Post: 02-19-2011, 02:12 PM
  2. Replies: 2
    Last Post: 12-18-2010, 02:34 PM
  3. for loop that calls multiple functions
    By elsparko in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2009, 07:10 AM
  4. Replies: 10
    Last Post: 12-16-2008, 10:28 AM
  5. functions stops loop from running
    By a1pro in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 03:02 PM

Tags for this Thread


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