Thread: Why does my program not start?

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    36

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > numerator *= base;
    Huh?
    base is -1, and numerator is 1

    All you're going to end up with is 1 or -1.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    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).
    Last edited by flp1969; 04-10-2019 at 02:37 PM.

  4. #4
    Registered User
    Join Date
    Dec 2018
    Posts
    36
    Quote Originally Posted by flp1969 View Post
    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 subscribe to a feed

Similar Threads

  1. Having trouble start to start this C program
    By cool1995 in forum C Programming
    Replies: 1
    Last Post: 10-18-2016, 12:51 AM
  2. Replies: 1
    Last Post: 01-21-2013, 07:15 AM
  3. Start over a program?
    By glasvegas in forum C Programming
    Replies: 2
    Last Post: 06-21-2011, 07:49 AM
  4. Get program to copy itself into program files, then start on startup.
    By guitarist809 in forum Windows Programming
    Replies: 6
    Last Post: 03-03-2008, 09:42 AM
  5. start another program
    By lshome in forum C Programming
    Replies: 3
    Last Post: 06-24-2002, 01:48 PM

Tags for this Thread