Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 8 of 8
Thread: Need help on a program for class

09162009, 06:34 AM #1
 Join Date
 Sep 2009
 Posts
 16
 Thanks
 3
 Thanked 0 Times in 0 Posts
Need help on a program for class
This is what I have so far:
Code:#include <iostream> using namespace std; void main () { float first_num,sum; long double second_num,sum2; float third_num,sum3; long double fourth_num,sum4; sum = 0; first_num = 1; while (first_num <= 1000000) { sum = sum + 1/first_num; first_num++; } cout << "1.) The sum of the numbers 1/1 + ... + 1/100000000 (Float) : " << sum << endl; sum2 = 0; second_num = 1; while (second_num <= 1000000) { sum2 = sum2 + 1/second_num; second_num++; } cout << "2.) The sum of the numbers 1/1 + ... + 1/100000000 (Double) : " << sum2 << endl; sum3 = 0; third_num = 1000000; while (third_num >= 1) { sum3 = sum3 + 1/third_num; third_num; } cout << "3.) The sum of the numbers 1/100000000 + ... + 1/1 (Float) : " << sum3 << endl; sum4 = 0; fourth_num = 1000000; while (fourth_num >= 1) { sum4 = sum4 + 1/fourth_num; fourth_num; } cout << "4.) The sum of the numbers 1/100000000 + ... + 1/1 (Double) : " << sum4 << endl; }
Write a program to calculate and print the result of the following two operations:
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + ... + 1/99999999 + 1/100000000
and
1/100000000 + 1/99999999 + 1/99999998 + 1/99999997 + ... + 1/3 + 1/2 + 1/1
Results should be totaled and printed once using float variables for each calculation and once using double variables for each calculation. There should be a total of four answers printed. Make sure that the answers are adequately labeled. You may use any of the three loop mechanisms (while, dowhile, or for).
Here's the problem:
Can't put 100,000,000 without the program halting when I run it (1,000,000 works).
Why is that?
Also wondering why the three last sums are different from the first when I do put 1,000,000?
Final question , how can i take it another step and put it all into one loop?
Thanks.Last edited by oracleguy; 09162009 at 05:11 PM. Reason: Please use code tags
09162009, 08:47 AM
#2
 Join Date
 Sep 2009
 Posts
 16
 Thanks
 3
 Thanked 0 Times in 0 Posts
any input?
09162009, 05:17 PM
#3
 Join Date
 Jun 2002
 Location
 USA
 Posts
 9,074
 Thanks
 1
 Thanked 328 Times in 324 Posts
Welcome to CodingForums, please keep our thread bumping rule in mind for the future:
As for your question:Do not bump your thread repeatedly when you don't get a response. Sometimes when you post for help, you may not get a response in a timely matter, if at all. Forums aren't wishing wells, and some questions will fall through the cracks. That's a fact of life. It's ok to occasionally bump a thread, but only when done after an ample amount of time (ie: 23 days) have passed without a response, and never more than once, . Your thread is no more important than another member's when it comes to the amount of attention it should receive.
How do you know the program is "halted" (I'm guessing you mean frozen?)? Most likely it is just taking a very long time to complete at 100 million. It would take 100x longer than for 1 million to complete.
OracleGuy
09162009, 06:39 PM
#4
One thing that can help is if you were to combine your while loops...
i.e. >
Are similar loops (save the variable types and you are using a float/double as your control variable), I would rewrite as something like this to see if your processing time might be sped up:Code:sum = 0; first_num = 1; while (first_num <= 1000000) { sum = sum + 1/first_num; first_num++; } cout << "1.) The sum of the numbers 1/1 + ... + 1/100000000 (Float) : " << sum << endl; sum2 = 0; second_num = 1; while (second_num <= 1000000) { sum2 = sum2 + 1/second_num; second_num++; } cout << "2.) The sum of the numbers 1/1 + ... + 1/100000000 (Double) : " << sum2 << endl;
saigeCode:#include <iostream> using namespace std; void main () { float sum; long double sum2,count; sum = 0; sum2 = 0; count = 1; while (count <= 100000000) { sum+=1/count; sum2+=1/count; count++; } cout << "1.) The sum of the numbers 1/1 + ... + 1/100000000 (Float) : " << sum << endl; cout << "2.) The sum of the numbers 1/1 + ... + 1/100000000 (Double) : " << sum2 << endl; sum = 0; sum2 = 0; count = 100000000; while (count >= 1) { sum+=1/count; sum2+=1/count; count; } cout << "3.) The sum of the numbers 1/100000000 + ... + 1/1 (Float) : " << sum << endl; cout << "4.) The sum of the numbers 1/100000000 + ... + 1/1 (Double) : " << sum2 << endl; }
HTML & CSS Forum Moderator
"If you don't know what you think you know, then what do you know."
R.I.P. Derrick Thomas #58
1/1/1967  2/8/2000
09172009, 01:46 AM
#5
 Join Date
 Sep 2009
 Posts
 16
 Thanks
 3
 Thanked 0 Times in 0 Posts
#include <iostream>
using namespace std;
void main ()
{
float Sum1 = 0;
double Sum2 = 0;
float Sum3 = 0;
double Sum4 = 0;
for (int i = 1; i <= 100000000; i++)
{
Sum1 += 1.0f/i;
Sum2 += 1.0f/i;
Sum3 += 1.0f/(100000001i);
Sum4 += 1.0f/(100000001i);
}
cout << "1.) The sum of the numbers 1/1 + ... + 1/100000000 (Float) = " << Sum1 << endl;
cout << "2.) The sum of the numbers 1/1 + ... + 1/100000000 (Double) = " << Sum2 << endl;
cout << "3.) The sum of the numbers 1/100000000 + ... + 1/1 (Float) = " << Sum3 << endl;
cout << "4.) The sum of the numbers 1/100000000 + ... + 1/1 (Double) = " << Sum4 << endl;
}
Is there any other way to make this even cleaner?
Also why is there differences in the answers between the 4 sums?
Can't get the grasp of it.
Any feedback would be great.
Thanks.
09172009, 02:30 AM
#6
 Join Date
 Jun 2002
 Location
 USA
 Posts
 9,074
 Thanks
 1
 Thanked 328 Times in 324 Posts
It has to do with the data types, a float is a single precision floating point number and a double is a double precision. The double precision has a larger range of values aka more precision in this case.
See:
http://en.wikipedia.org/wiki/Double_precision
http://en.wikipedia.org/wiki/Single_...gpoint_format
Also
Your main function is incorrect for C++. In C++ the main function must return int, not void. Returning void is what you do in C. I would highly recommend fixing that now and getting in the habit of doing that.
OracleGuy
09172009, 03:07 AM
#7
 Join Date
 Sep 2009
 Posts
 16
 Thanks
 3
 Thanked 0 Times in 0 Posts
Thanks for the feedback oracleguy.
"In C++ the main function must return int, not void"
^^^^
That's how my professor has been writing it out, so I've just followed along.
But thanks for the advice
09182009, 07:41 PM
#8
 Join Date
 Sep 2009
 Posts
 16
 Thanks
 3
 Thanked 0 Times in 0 Posts
Everything worked out perfectly
Just saying thanks again