Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (christos):

C++ Help needed! http://screencast.com/t/m9ddkmpqH I dont understand why if I put the "counter++;" line below the " term = term + (1.0/(counter*(counter + 1.0)));" line it gives me infinite? And when I place it above it it doesn't give me infinite? Why does this happen ?

OpenStudy (christos):

#include <iostream> using namespace std; int main() { int n, counter=0; double term=0; cin >> n; while (counter <= n) { counter++; term = term + (1.0/(counter*(counter + 1.0))); } cout << term; return 0; }

ganeshie8 (ganeshie8):

initial value of counter = 0, you're dividing the counter. thats the problem

ganeshie8 (ganeshie8):

change the initial value of counter to 1, it will work...

OpenStudy (christos):

I see, thank you!

ganeshie8 (ganeshie8):

or put the increment later... as you already did...

ganeshie8 (ganeshie8):

np :)

OpenStudy (christos):

=)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!