First of all my program needs to be able to calculate the sum of
1/1 + 1/2 + 1/3 + 1/4 + 1/5 + etc. all the way up to 1/100000000.
Later on we have to reverse it, but thats not important now.
I have this so far... but I'm very confused at how exactly to get the loop execution right, because it seems that the sum is counting closer to 0 for some reason.
#include <iostream>
using namespace std;
void main ()
{
int value = 1;
float value2 = 1;
float summation = 0;
while ((value2 <= 100000000) && (value2 > 0))
{
sum += (value / value2);
++value2;
}
cout << "The sum of the first problem is: " << summation << endl;
}
That's what I have so far. I know you have to divide as the first execution, then take that sum and add it to the next sum.
Code tags. Always code tags.
You can type sum = 1 / value2 instead of value=1, sum = value / value2.
The reason is because you are setting it to equal. Every time, sum EQUALS the quotient. That means the previous value is destroyed. You need +=, the compound addition, which will add to it rather than changing its value directly. Think about the meaning of the assignment operator.
Whoops never mind, you seem to have just edited that.
In that case, let's see... (by the way stop using void main, it's stupid and inexplicable, main is supposed to return int and nothing else)
What output are you getting?
I redid it a little. I can't believe i kept setting a value to val1 for some reason. Stupid mistake on my part.
#include <iostream>
using namespace std;
void main ()
{
float val = 1;
float sum = 0;
while (val <= 100000000)
{
sum += (1 / val);
++val;
}
cout << "The sum of the first problem is: " << sum << endl;
}
I'm still not getting a result with the loop, but I moved the line (cout) back up into the brackets, and it's adding now, but its going slowly as its adding smaller and smaller fractions. It will lets say 10.xxxx
10.xxx2
10.2020
10.9823
11.1029
So what do you want? That's normal behavior. 1/10000000 or whatever is an exceptionally small number. You may want to switch to double or long double to maintain any amount of accuracy.
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)."
Hence i was using float variables. I guess I did it correctly after all?
------------------------------------------------------------------------------------------------- (updated)
#include <iostream>
using namespace std;
void main ()
{
{
float val = 1;
float sum = 0;
while (val <= 100000000)
{
sum += (1 / val);
++val;
cout << "The sum of the first problem is: " << sum << endl;
}
}
{
float val = 100000000;
float sum = 0;
while (val >= 1)
{
sum += (1 / val);
--val;
cout << "The sum of the second problem is: " << sum << endl;
}
}
It seems to be perfectly fine. It should be constantly decelerating because every value you add is lower than the one before it.
My only advice is to stop using floats and use double/long double. Floats lack the decimal accuracy to handle the numbers you have in mind.
The only problem I have then is getting the result to appear. While i realize this takes a LONG time to appear, I don't think my teacher is expecting that. I wasn't sure if it was a literal translation from the program sheet. I just assumed they wanted 2 ways using FLOAT and 2 ways using DOUBLE.
The thing about float is that the variable size is not going to be large enough to hold all your decimal spaces so you will end up losing tons of places to truncation. So that's why double is generally better than float.
i appreciate all the help you've given me, and I think I've gotten the program exactly how I want it. I guess the only question that remains to me is... If the teacher expects me to use float variables, and I am using those as you can see above. If the result never comes back how exactly do I get the result then for float variables?
The double result shows up exactly the same number of 18.9979.
The float never returns. Does long double count as a float variable?
I ran your program with float variables which gave me a slight difference in the sum's then I changed the variables to doubles and they came out exactly the same and moved the couts outside of the loop brackets. which Ive seen you have done in your last post :), try declaring the variables as double and see what happens
your right drake, however i was able to get an output with 10000000 as a float variable, which is 8 digits, but after some quick research I realized it can't handle 9 digits as a float
orbit, this was probably the point of the assignment, to realize the float variable limitations
I just moved to a new house this past month and have been away from programming for about 3 weeks,,, jeesh, I cant believe how quickly I forgot many of the basics
Thank you all for the help. I think I've finished the program, however I did email my teacher finding it fishy that float wasnt returning anything, and I updated it accordingly. Apparently, I'm supposed to change the VAL to an INT and a (float) temporarily, but not sure why. Both float's answers are different but doubles are the same.
#include <iostream>
using namespace std;
void main ()
{
{
int val = 1;
float sum = 0;
while (val <= 100000000)
{
sum += (1 / (float) val);
++val;
}
cout << "The sum of the first problem float variables is: " << sum << endl;
}
{
int val = 100000000;
float sum = 0;
while (val >= 1)
{
sum += (1 / (float) val);
--val;
}
cout << "The sum of the second problem using float variables is: " << sum << endl;
}
{
double val = 1;
double sum = 0;
while (val <= 100000000)
{
sum += (1 / val);
++val;
}
cout << "The sum of the third problem using double variables is: " << sum << endl;
}
{
long double val = 100000000;
long double sum = 0;
while (val >= 1)
{
sum += (1 / val);
--val;
}
cout << "The sum of the fourth problem using double variables is: " << sum << endl;