Newbie C++ program help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kevo84
    New Member
    • Sep 2006
    • 1

    #1

    Newbie C++ program help

    Hello,

    I am horrible at math and am trying to figure out how to make a program in which you would calculate, and display the answer for the following:

    1/1+1/2+1/3+1/4+1/5............1/999999+1/1000000

    we are suppose to use both floating point and double variables. The problem is i cant logically think of a way to make the denominators change, while doing the addition opperation. Please keep in mind this is a beginer class and we really havnt gone over much besides input, output, if statements, while, do-while and for loops. By the way we are supposed to use while, do-while or for's to do this. I am sorry if this seems like i'm wanting you to do my homework for me, but its kind of hard whne there are no tutor or anything to talk me through this (professor office hours are all durring either work or my other class times)

    thank you all for your time.
  • xpcer
    New Member
    • Jul 2006
    • 51

    #2
    Originally posted by Kevo84
    Hello,

    I am horrible at math and am trying to figure out how to make a program in which you would calculate, and display the answer for the following:

    1/1+1/2+1/3+1/4+1/5............1/999999+1/1000000

    we are suppose to use both floating point and double variables. The problem is i cant logically think of a way to make the denominators change, while doing the addition opperation. Please keep in mind this is a beginer class and we really havnt gone over much besides input, output, if statements, while, do-while and for loops. By the way we are supposed to use while, do-while or for's to do this. I am sorry if this seems like i'm wanting you to do my homework for me, but its kind of hard whne there are no tutor or anything to talk me through this (professor office hours are all durring either work or my other class times)

    thank you all for your time.
    may be like this?

    #include <stdio.h>
    main()
    {
    double result;
    int i;

    i=1;
    result=0;
    do{
    result=result+( 1/i);
    i=i+1;
    }
    while(i<=100000 0);

    print("Result : %f \n",result);
    }

    Comment

    • D_C
      Contributor
      • Jun 2006
      • 293

      #3
      The problem with the above code is that both 1 and i are integers, so when you do result = result + 1/i, it will return zero for all (1/i), i > 1. As a result, you will have 1 + 0 + 0 + ... + 0 = 1.

      Also, the loop could be shortened to
      i = 1;
      while(i < (10^n))
      result += (1/i++);

      However, I don't understand your motivation for doing such a thing. If it's understanding how to program a loop, I suggest you find a better example.

      The series (summation of 1/i, i = 0 to 10^n) can be thought of as the Riemann sum for 1/x, and it turns out that is how we define the natural logarithm. ln(x) = int(du/u) from 1 to x. You may use ln(x) to under-approximation sum(x=1 to 10^n, 1/x). In this case ln(10^n) = n*ln(10), which is much faster than a loop which iterates 10^n times.

      The difference between the two functions as you head towards infinity is The Euler-Mascheroni constant, gamma.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Originally posted by D_C
        Also, the loop could be shortened to
        i = 1;
        while(i < (10^n))
        result += (1/i++);
        The notation 10^n is poor (or wrong even)

        In C/C++ ^ is the xor operator, there is no power operator (or did you really mean xor? or were you pseudo coding?)

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          No, I meant pow(10,n). I was just being lazy.

          Comment

          Working...