|
The problem is very simple, but I'm a novice to programming and I got quite confused by this program's output.
The main idea is that I wanted to print the following series:
1
11
111
1111
11111
111111
........
and so on
everything went well until the 17th line when i got
11111111111111110.
from here up to the end i got some zeros and 2s i didn't want.
Here's my code:
#include<math.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
int main ()
{
cout.precision(16);
int k, n (0);
double a(0);
cout <<"Due to memory limitations this program can print up to 16 lines.";
for (n;n<k;n++){
a = a + pow(10,n);
cout << a << endl;
}
system ("pause");
return 0;
}
Please help me! Thanks.
|
|
|
That's just wierd.
It's could be a number of things, but I would only be guessing at this point.
It could be a problem with how the computer handles floating point numbers (change your doubles to floats and the errors begin at line 9) or with the pow function itself.
Whatever the reason, I don't see a problem with your code.
|
|
|
|
|
|
|
|