Thread: Help wanted for my little c-program

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    Help wanted for my little c-program

    Hi everyone!

    I am a beginner in c programming, and I would be very very grateful if someone coule take a look at my little program which I wrote and which unfortunately does not really work.

    The program calculates the running time and the annuity of a credit, after receiving the necessary values.
    However, the formula for the running time does not work. I tried to rewrite this formula, maybe I have made a mistake:

    Help wanted for my little c-program-601e4d6340f55585b0d1f15634a1b842-png

    in my programm n is the running time, R is the annuity, S is the amount of the credit, m is the fixed fee and i is the lending rate in per cent.

    Code:
    #include <stdio.h>
    #include<math.h>
    
    double R,m,S,i;
        
    double annuity()
    {
        double R = 12*m;
        return R;
    }
    
    int term()
    {
        int n = -(log(1-(i*S/R))/log(1+i));
         return n;
    }
    
    
    main()
    {
          printf("\nFill in a value for S: ");
        scanf("%lf",&S);
        printf("\nFill in a value for m: ");
        scanf("%lf",&m);
        printf("\nFill in a value for i: ");
        scanf("%lf",&i);
    
        printf("\n\nThe annuity is %.2lf",annuity());
        printf("\nThe running time is %i\n\n", term());
    }
    Thank you in advance and sorry for my bad english!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The global variable R is never given a valid value.
    Using global variables is considered a bad thing to do in most cases.

    Tim S.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Thank you very much for your answer.
    Where would it be best then to declare the variable R, so that both functions can use it? I have some problems with this topic...
    Thanks in advance

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Remember (mathematicians and computer scientists), ln and log are not the same things.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Just get rid of the double keyword here: double R = 12*m;

    You're effectively creating a new, local variable called R instead of setting the global R variable.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Thank you very much. I changed it.
    However, the result for the running time n is mostly -2147483648
    How should I change the formula for n? What is wrong with it?
    Thanks!

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by hk_mp5kpdw View Post
    Remember (mathematicians and computer scientists), ln and log are not the same things.
    Isn't there a separate log10 function for log to the base 10 ?

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Try changing "-(value)" to "-1 * (value)"; this is a guess, I have no idea what C does with "-(value)".

    In this line below:
    Code:
    int n = -(log(1-(i*S/R))/log(1+i));
    Tim S.

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Need to make a clarification/retraction. The log function call in the language does mean ln (natural logarithm/base e). A common problem with programmers and the log function is that they want log (base 2) but in fact it is e instead and there is always a need to convert from base e to base 2 with the use of computations (a simple one). In this case, the use of log is correct. This tripped me up as well in this case.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by stahta01 View Post
    Try changing "-(value)" to "-1 * (value)"; this is a guess, I have no idea what C does with "-(value)".

    In this line below:
    Code:
    int n = -(log(1-(i*S/R))/log(1+i));
    Tim S.
    This should not matter. The C standard says a unary - is treated as a negation of the operand, so it behaves as expected.

    @Lika:
    What input are you giving? It's hard for us to track down your problem if we can't replicate it. Regardless, I threw in some totally made up numbers.

    I added the following 3 lines to the beginning of term():
    Code:
    printf("i*S/R = %f\n", i*S/R);
    printf("log(1-(i*S/R)) = %f\n", log(1-(i*S/R)));
    printf("log(1+i) = %f\n", log(1+i));
    Here is the output I got:
    Code:
    $ ./credit
    Fill in a value for S: 1
    Fill in a value for m: 3
    Fill in a value for i: 100
    
    The annuity is 36.00
    i*S/R = 2.777778
    log(1-(i*S/R)) = nan
    log(1+i) = 4.615121
    
    The running time is -2147483648
    Notice that log(1-(i*S/R)) comes back as nan (not a number). Take a look at the documentation for log():
    Quote Originally Posted by man log
    RETURN VALUE
    On success, these functions return the natural logarithm of x.

    If x is a NaN, a NaN is returned.

    If x is 1, the result is +0.

    If x is positive infinity, positive infinity is returned.

    If x is zero, then a pole error occurs, and the functions return -HUGE_VAL, -HUGE_VALF, or -HUGE_VALL, respectively.

    If x is negative (including negative infinity), then a domain error occurs, and a NaN (not a number) is returned.
    Since e is a positive constant, e to any power must be a positive number, thus you can't take the natural log of a negative number.

    This is a classic case of GIGO. You need to give appropriate values for S, m and i. I believe that i*S/R should come out to be a value between 0 and 1. Should i be entered/represented as a fractional percent (e.g. .01 = 1%)? Do you need to force conditions on R, m and S such that R is always greater than S or greater than 100*S or some such?

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Lika View Post
    Where would it be best then to declare the variable R, so that both functions can use it?
    I meant to answer this in my other post. Declare all your variables in main, and pass them as needed. If you need some help on using functions in C, check out our tutorial, and Google for some more.

    Also note that the correct declaration of main is int main(void), and you need to return an int at the end, usually 0 for success. Read why it matters here.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Thank you very much for your answers!
    I changed what you had advised me and now the program looks better.
    However, the problem as regards the calculation of i is still there. But I think I will leave it like this, because the definition of task for the program is very short and does not give proper information about this. Whether there need to be forced special conditions on the input variables is also not clear.
    So again, thanks very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help wanted
    By reticulatus in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 11-22-2009, 05:03 PM
  2. Replies: 5
    Last Post: 03-29-2009, 02:32 PM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. I just wanted....
    By ILoveVectors in forum C++ Programming
    Replies: 0
    Last Post: 06-22-2005, 09:29 PM
  5. *Wanted*
    By TeQno in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2003, 03:11 AM