why its not working?

This is a discussion on why its not working? within the C Programming forums, part of the General Programming Boards category; hi having an irritating problem. i've tried everything i know, still, cant figue why its not working.. the program should ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    9

    why its not working?

    hi
    having an irritating problem. i've tried everything i know, still, cant figue why its not working..

    the program should get from you a "n" value, and calculate the deniz function as clearly be seen...

    Need a little help please..

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    long deniz ( double x);
    
    int main(void)
    {
      int i;
      int n;
      long total = 0;
      printf (" n value ?? \n");
      
      scanf ( "%d", &n );
      
       for ( i = 1;i <= n; ++i )
       {  total = total + deniz(i);
       
    }
    printf( "output %f", total);
      system("PAUSE");	
      return 0;
    }
    long deniz (double x)
    {
    
    return log(x)-x ;
    }
    Last edited by Dulus; 11-01-2010 at 08:10 AM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >for ( i = 1; 1 <= i <= n; ++i )
    That's not meaningful. I suspect you meant:
    Code:
    for (i = 1; i <= n; ++i)
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    ah. figures...

    yep, thanks. i've corrected it. i dont know why i wrote that...

    but still :S

    when i run the program, says

    "output 0.000000"

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    You're using %f to print a long integer. Try %ld instead, or change total to either float or double.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    Check you format specifier for the following printf:

    Code:
    printf( "output %f", total);
    Is total a double?

    Jim

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    9
    right...
    very very thank you again.
    i got to study a little more i guess...

    gentleman,
    i still got a problem.
    the program still doesent give expected output.

    for example, for n=3 output has to be -4,208240...

    but it gives me -3...

    its sad...

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    You are mixing longs with doubles. Long like int will truncate the result to an int value.

    You should change to using double and not long.

    Jim

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    One possibility... Your i variable is an integer but the deniz function is asking for a double.

    You should use a typecast when calling the function...
    Code:
    total = total + deniz((double) i);
    Also... please don't go back and edit your code. Leave the old one there and post a new copy so that others with the same problem can follow how you solved it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 07:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 07:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM

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