424,987 Members | 997 Online Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

Newbie C++ program help

 P: 1 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. Sep 18 '06 #1
4 Replies

 P: 51 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 main() { double result; int i; i=1; result=0; do{ result=result+(1/i); i=i+1; } while(i<=1000000); print("Result : %f \n",result); } Sep 18 '06 #2

 100+ P: 293 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. Sep 18 '06 #3

 Expert Mod 5K+ P: 8,916 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?) Sep 18 '06 #4

 100+ P: 293 No, I meant pow(10,n). I was just being lazy. Sep 19 '06 #5 