Hi everyone,

I'm new to this site and programming, and i was wondering if i could get some help with a programming problem of mine.

I have to print 10000 random numbers ranging from 0 to 10000.
The numbers should be put in the console screen and a txt. file.

I have these two steps done, but then i don't know what to do afterward.

The program should then read each number from the file and check to see if it is palindromic.

Then the program should count the number of palindromic numbers in the txt. file, and create a new txt. file containing only the palindromic numbers.

Then the palindromic numbers should be displayed in the console window along with the number of palindromic numbers there are.

//This is the code i have so far
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

const int arraysize = 10000;
int numbers[arraysize];
int i;


void CreateArray();
void DisplayArray();


int main()
{
	
	CreateArray();
	DisplayArray();
	system("Pause");


}

void CreateArray()
// Creates the inital random number array
{
	
srand( (unsigned int )time(0) );

	for (i=0;i<arraysize;i++)
	{
		numbers[i] = rand() % 10000;
	}
}


void DisplayArray()
// displays the array in the console window
{
	int i;
	for (i=0;i<arraysize;i++)
	{
		cout << numbers[i] << endl;
	}

// displays the array in the txt. file
ofstream marksoutput;

marksoutput.open("C:/Users/Bart/Desktop/RandomNumbers.txt");


for (i=0;i<arraysize;i++)
{
	marksoutput << numbers[i] << endl;
}

marksoutput.close();

}
Ancient Dragon commented: Thanks for using code tags on first try :) +36

I think it would be much smarter to use the built-in function from <algorithm> 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;
}

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?! ;)

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.

Wow thanks everyone for all the help so far.

I've just started C++ and some of these threads go over my head.
It's nobody's fault but my own. I've written the code the way it is because its easier for me to understand. Also im working on file input and outputs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.