# Thread: Why does my program not start?

1. ## Why does my program not start?

Hi!
First of all, I know that my code in regard to solving the task (logic) is still wrong and I will deal with it afterwards.
For now, I am stuck at the technical details, since my program is even not starting and apparently not entering the main function and I do wonder why not?
Has it something to do with the fact that the return type of my function shall be a double value, whereas the return type of the main function shall be an integer value?

Code:
```#include <stdio.h>

double ln2iter(int number_subtotals) {
int i, j;
double base = -1, numerator = 1, sum = 0;
for (i = 1; i < (number_subtotals + 1); i++) {
numerator *= base;
} for (j = 1; j <= number_subtotals; j++) {
sum = sum + ((numerator) / j);
}
return sum;
}

int main() {
int n;
double result;

printf("How many subtotals do you want to have calculated?\n");
scanf("%d", &n);
result = ln2iter(n);
printf("The natural logarithm of 2 with %d subtotals is %lf.\n", n, result);

return 0;
}```

2. > numerator *= base;
Huh?
base is -1, and numerator is 1

All you're going to end up with is 1 or -1.

3. As a program, there are no errors, except the %lf format at printf... %f will always deal with double... and to deal with long double you have to use %Lf (uppercase L). This is not this case. How are you compiling and linking your program?

As the intention of the program, your function is completely wrong... Use log(), from libm, to get the ln 2: log(2.0) and compare the results.

PS: Get the glibc source code and take a look at sysdeps/ieee754/flt-32/e_logf.c to see the complexity of log(x).

4. Originally Posted by flp1969
As a program, there are no errors, except the %lf format at printf... %f will always deal with double... and to deal with long double you have to use %Lf (uppercase L). This is not this case. How are you compiling and linking your program?

I just use for testing the codeblocks workspace where all happens automatically.

As the intention of the program, your function is completely wrong... Use log(), from libm, to get the ln 2: log(2.0) and compare the results.

PS: Get the glibc source code and take a look at sysdeps/ieee754/flt-32/e_logf.c to see the complexity of log(x).
Thanks for the suggestions - I will take a look and try to understand the sourcecode myself.

Popular pages Recent additions