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;
}

The problem is how it is displaying the data. Line 23 prints the array before it has been sorted.

Thanks. I switched the order of the cout statement and sort call, and now I get 10 -numbers and 10 random numbers sorted. Not sure why the first ten are messed up though?! Thoughts?

void sortNumbers(int [], int);
void fileInOut();

int main()
{  
	srand(time(NULL));
	const int numArraySize = 20;
	int numberArray[numArraySize];

	for(int i = 0; i < numArraySize; i++)
	{	 
			numberArray[i] = rand() % 100; 
			sortNumbers(numberArray, numArraySize);
			cout << numberArray[i] << endl;
	}
	
	system("pause");
	return 0;
}

void sortNumbers(int numberArray[], int numArraySize)
{
	int temp = 0, lowestValueIndex = 0;
	
	for(int i = 0; i < numArraySize; i++) 
	{
		lowestValueIndex = i;
		for(int j = i + 1; j < numArraySize; j++)
		{
			if(numberArray[j] < numberArray[lowestValueIndex])
				lowestValueIndex = j;
		}
		
		if(i != lowestValueIndex)
		{
			temp = numberArray[i];
			numberArray[i] = numberArray[lowestValueIndex];
			numberArray[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;
}

Thanks. I switched the order of the cout statement and sort call, and now I get 10 -numbers and 10 random numbers sorted. Not sure why the first ten are messed up though?! Thoughts?

My thought is "why the first ten are messed up" explains nothing? No one but you know what that means...

You now are sorting the numbers as you get them. In other words

Get number 1
Sort it
Output it
Get number 2
Sort them
Output #2
...

Don't you want to
-- Get the numbers
-- Sort them all
-- Output them all?

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.