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.

Edited 6 Years Ago by Ancient Dragon: n/a

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

Edited 6 Years Ago by WaltP: Did you forget something this time? PREVIEW button perhaps?

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?

This article has been dead for over six months. Start a new discussion instead.