...

View Full Version : Need help on a program for class



Recklein3
09-16-2009, 05:34 AM
This is what I have so far:



#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;

}


This is what the program is suppose to do:


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, do-while, 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.

Recklein3
09-16-2009, 07:47 AM
any input?

oracleguy
09-16-2009, 04:17 PM
Welcome to CodingForums, please keep our thread bumping rule in mind for the future:


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: 2-3 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.

As for your question:

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.

sage45
09-16-2009, 05:39 PM
One thing that can help is if you were to combine your while loops...

i.e. -->


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;
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:

#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;
}

-saige-

Recklein3
09-17-2009, 12:46 AM
#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/(100000001-i);

Sum4 += 1.0f/(100000001-i);

}

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. :thumbsup:

oracleguy
09-17-2009, 01:30 AM
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_precision_floating-point_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.

Recklein3
09-17-2009, 02:07 AM
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 :thumbsup:

Recklein3
09-18-2009, 06:41 PM
Everything worked out perfectly:D

Just saying thanks again :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum