Thread: what am i doing wrong?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    what am i doing wrong?

    im trying to calculate the sum of 1/1 + 1/2+...+1/100

    Code:
    #include <stdio.h>
    int main() {
        
        int i;
        
        float res;
        
        res = 0;
        
        for (i=1;i<=100;i++){
    
            res = res + 1/i;
           
            }
    
        printf("%f",res);
        
        getchar();
        
        return 0;
        
    }

    result: 1????????????????????

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're doing integer division, where the remainder is thrown out, so it boils down to 1/1 = 1, and all the other divisions are zero's.

    So the answer is just 1.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    3
    Quote Originally Posted by Adak View Post
    You're doing integer division, where the remainder is thrown out, so it boils down to 1/1 = 1, and all the other divisions are zero's.

    So the answer is just 1.
    thank you i changed to

    Code:
    #include <stdio.h>
    #include <math.h>
    int main() {
        
        int i;
        
        float res;
        
        res = 0;
        
        for (i=1;i<=100;i++){
    
            res = res + 1.0/i;
           
            }
    
        printf("%f",res);
        
        getchar();
        
        return 0;
        
    }
    and i think it works now

    result: 5.187378

    is it correct?

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Usually, it is best to define the types in the code:

    res = res + 1.0f/(float)i;

    Though this is not a requirement.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Only one side needs to be a floating point, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM