# Thread: Help wanted for my little c-program

1. ## 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:

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());
}```

2. 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.

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...

4. Remember (mathematicians and computer scientists), ln and log are not the same things.

5. 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.

6. 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. Originally Posted by hk_mp5kpdw
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. 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. 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.

10. Originally Posted by stahta01
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():
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. Originally Posted by Lika
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.