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 ?
#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; }
initial value of counter = 0, you're dividing the counter. thats the problem
change the initial value of counter to 1, it will work...
I see, thank you!
or put the increment later... as you already did...
np :)
=)
Join our real-time social learning platform and learn together with your friends!