Then you could do something like: int* A=newint[100]; this will be a pointer to an array on the heap. And don't forget the delete aspect of it.
Better still, you could use a vector of integers.
#include <iostream>
#include <fstream>
#include <string.h>
#include <math.h>
usingnamespace std;
unsignedint PRNG()
{
// our initial starting seed is 5323
staticunsignedint nSeed = 5323;
// Take the current seed and generate a new value from it
// Due to our use of large constants and overflow, it would be
// very hard for someone to predict what the next number is
// going to be from the previous one.
nSeed = (8253729 * nSeed + 2396403);
// Take the seed and return a value between 0 and 100
return nSeed % 100;
}
int main()
{
int *A = newint[100]; // pointer to an array on the heap
int i;
//Writing Random Numbers to input file
ofstream randomNumbers("input.txt");
for(i=1; i<100; i++){
randomNumbers << PRNG() <<" ";
}
//Reading Numbers From Input File
ifstream in("input.txt");
i=0;
while(in >> *(A+i)) // reading all elements
{
i++;
}
int n=i; // number of numbers
in.close();
//Sorting the numbers
for(i = 0; i<= n-1; i++)
for(int j=i+1; j <= n; j++)
if(*(A+i) > *(A+j))
{
int temp = *(A+i);
*(A+i) = *(A+j);
*(A+j) = temp;
}
// Writing to output file
ofstream out("output.txt");
for(i=1; i<=n; i++)
out << *(A+i) <<" ";
out.close();
delete[] A; // delete the memory allocated on the heap
return 0;
}
I have updated my code using pointers for sorting, and writing random numbers in the input file.
But I have one small problem, now it doesn't write anything in my output file after sorting.