codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  simple series problem - help needed  georgethebeatle at 21:09 on Monday, June 05, 2006
 

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.


  Re: simple series problem - help needed  The Graduate at 07:07 on Tuesday, June 13, 2006
 

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.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  form validation(arabic/persian language)
•  Function to check the pattern(i.e 1A-2B-3C)
•  function call from multiple rows
•  OpenFileDialog
•  Re: Sorting in ascending - random characters from user
•  Re: Date validation
•  How to use java in javascript-c
•  SQLException : returned refusing to connect
•  Dissappearing Menus


Recent Articles
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net


© Copyright codetoad.com 2001-2006