Hi, I have to create a program that takes 20 random numbers between 0 and 100 and sorts them in order from smallest to largest. Then, I have to used File I/O to output the list into a .txt file.My code is compiling, and I'm getting the random numbers, but they are not in order. Also, I'm not sure if my File I/O is correct as I don't see a .txt file in my documents. Any help would be great! Thanks!
#include <iostream>
#include<time.h>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
void sortNumbers(int [], int);
void fileInOut();
int main()
{
const int numArraySize = 20;
int numberArray[numArraySize];
srand(time(0));
for(int i = 0; i < numArraySize; i++)
{
numberArray[i] = rand() % 101;
cout << numberArray[i] << endl;
}
sortNumbers(numberArray, numArraySize);
system("pause");
return 0;
}
void sortNumbers(int sortArray[], int arraySize)
{
int temp = 0, lowestValueIndex = 0;
for(int i = 0; i < arraySize; i++)
{
lowestValueIndex = i;
for(int j = i + 1; j < arraySize; j++)
{
if(sortArray[j] < sortArray[lowestValueIndex])
lowestValueIndex = j;
}
if(i != lowestValueIndex)
{
temp = sortArray[i];
sortArray[i] = sortArray[lowestValueIndex];
sortArray[lowestValueIndex] = temp;
}
}
return;
}
void fileInOut()
{
ofstream outFile;
outFile.open("data.txt");
outFile << sortNumbers << endl;
outFile.close();
ifstream inFile;
inFile.open("data.txt");
string myString;
getline(inFile, myString);
inFile.close();
cout << myString << endl;
system("pause");
return;
}