I think it would be much smarter to use the built-in function from header file -- random_shuffle():
Run a loop and insert to the array all the numbers between 1 and 10,000, then call random_shuffle() to shuffle them.
An example code:
#include<iostream>
#include<fstream>
#include<algorithm>
using namespace std;
int main(void)
{
unsigned short int Numbers[10000],x;
for(x=0;x<10000;x++)
{
Numbers[x]=x+1;
}
// shuffle all the Numbers[] array - from element [0] to [9999]
random_shuffle(Numbers, Numbers+10000);
ofstream fout;
fout.open("Numbers1to10000.txt");
for(x=0;x<10000;x++)
{
fout<<Numbers[x]<<"\n"; // put in file
cout<<Numbers[x]<<"\n"; // show to console screen
}
fout.close();
cout<<"Done\n";
cin.get();
return 0;
}
unbeatable0
Junior Poster in Training
90 posts since Sep 2008
Reputation Points: 42
Solved Threads: 13
1. You have made a very bad style functional decomposition. Why an array is global? Define all processing functions with processed array and array size parameters. Never burden your functions with redundant external dependencies. You CreateArray function does not create an array (it was created by a declaration statement), it fills or initialize it!
2. Never use file_stream << ... << endl for bulk file output operations. The std::endl means "write new line and flush buffers". No need in file buffer flush operation in that case (it's too slow). Use file_stream << ... << '\n' pattern.
3. About palindromes. You may get every digit via n%10 ... n /= 10 expressions or make a text representation of the number via std::stringstream output or even old good sprintf function. Try to invent some approaches to palindromic test yourself then (may be) ask more help ;)...
4. Print 10000 numbers to the console... Oh, my God! WHAT FOR?! ;)
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
I would think it would be a lot easier to work the the digits as strings instead of integers. For any given number:
1) Is it a single digit -- yes, then its a Palindromic number
2) If two digit number, are both digits the same: -- yes, then its a Palindromic number
3) for larger numbers, check if the first and last digits are the same, if yes, then check the second and next-to-last digits. Keep checking inwards until either no more digits or just a single digit left. If the algorithm gets that far, then its a Palindromic number.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343