### HELP! I can't figure out how to use a sort function

This is a portion of my code where I have tried to put in a sort feature But as you can most likely see I have no idea how to use one. This program reads in "grades" from a file and out puts the grades on the screen then exports them to an output file. all of that works except now My numbers do not show up on the screen or in the output file I get a random assortment of numbers usually something like "-84486585" in the place of where my numbers used to show up.

also it has to all be in arrays.

void output(const char name[], double sarray[], int n, double average, ostream& out)
{
out.setf(ios::fixed);
out.setf(ios::showpoint);
out.precision(2);

out << "\n\nInput File Name: " << name << endl;
out << "\nFile Contents:" << n << endl;

int index = 0;
double next;

for(int i = 0; i < n ; i++)
sort = i;
while ( n > 0 && sarray[n-1] > sarray[n] )
{
swap (sarray[n], sarray[n-1] );
n--;
}

out << "\nAverage of Numbers in File: " << setw(8) << average << endl;

}
Last edited on
This is an algorithm known as the selection sort function. My array is called list.

 ``12345678910111213141516`` `````` int i, j; int min; double temp; for (i = 0; i < length-1; ++i) { min = i; for (j = i + 1; j < length; ++j) if (list[j] < list[min]) min = j; temp = list[min]; list[min] = list[i]; list[i] = temp; }``````
Last edited on
What is the length in this attatched to?
length = the amount of numbers that are in the list. I find it easiest to get the length as you read in the array. Something like this:

 ``123456789`` ``````int i = 0; while(inputFile) { inputFile >> list[i]; i += 1; } length = i-1;``````
Last edited on
Ok so when I try to input something along the lines of what you have I get No numbers and it delete's my finding average part of the problem..... Here Let me just post my entire code on here because maybe I'm missing something some were else that is jacking it all up.

its kinda long....

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143`` `````` using namespace std; ofstream outputfile("output.txt"); const int MAX_FILE_NAME = 35; const int MAX_NUMBER_SCORES = 50; void open_input(ifstream& input, char name[]); void read_values(ifstream& input, double v[], int size, int& used); void find_average(const double v[], int n, double& average); void output(const char name[], double sarray[], int n, double average, ostream &out = cout); int main() { char again; char file_name[MAX_FILE_NAME + 1]; ifstream input_numbers; double scores [MAX_NUMBER_SCORES]; double average; int num_scores; cout << " This program finds the average of numbers in a file and outputs "; cout << " the average to a file" << endl; cout << endl; cout << endl; system("pause"); do { cout << endl; cout << endl; cout << endl; open_input(input_numbers, file_name); read_values(input_numbers, scores, MAX_NUMBER_SCORES, num_scores); input_numbers.close(); if (num_scores > 0) { find_average(scores, num_scores, average); output(file_name, scores, num_scores, average); output(file_name, scores, num_scores, average, outputfile); } else { cout << "\n\n No data in File: " << file_name << endl; } cout << "\nDo you want to process another file (Y/N)? "; cin >> again; cin.ignore(256, '\n'); } while ( again == 'y' || again == 'Y'); cout << "\nThe End!" << endl; outputfile.close(); return 0; } void open_input(ifstream& input, char name[]) { int count = 0; do { count++; if (count != 1) { cout << "\n\aInvalid file name or file does not exist. Please try again." << endl; } cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME << " characters please)\n:> "; cin.get(name, MAX_FILE_NAME + 1); input.open(name, ios_base::in); input.clear(); } while (input.fail() ); } void read_values (ifstream& input, double v[], int size, int& used) { double value; int count = 0; while (count < size && input >> value) { v[count] = value; count ++; } used = count; } void find_average(const double v[], int n, double& average) { int i, sum = 0; for (i = 0; i < n; i++) { sum = sum + v[i]; } average = sum / n; } void output(const char name[], double sarray[], int n, double average, ostream& out) { out.setf(ios::fixed); out.setf(ios::showpoint); out.precision(2); out << "\n\nInput File Name: " << name << endl; out << "\nFile Contents:" << sarray << endl; int sort = 0, index = 0; for(int i = 0; i < n ; i++) sort = i; if ( n > 0 && sarray[n-1] > sarray[n] ) { swap (sarray[n], sarray[n-1] ); n--; } out << "\nAverage of Numbers in File: " << setw(8) << average << endl; } ``````
Do I maybe need to create another void function with all of the information I've put in the output function and then link the two some how?
 This program finds the average of numbers in a file and outputs the average to a file Press any key to continue . . . Enter the input file name (maximum of 35 characters please) :> class3.txt Input File Name: class3.txt File Contents:0030FAA8 Average of Numbers in File: 81.00 Do you want to process another file (Y/N)?

That is the output I get...
I'm unable to figure out what your error is. I rewrote a rough draft of the program since I'm about to go to bed and be unable to help any further. I hope you can find something in mine that helps. Sorry that I'm taking the lazy approach.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114`` ``````#include #include #include using namespace std; double list[50]; void openFile(ifstream&); //function to open designated file void readData(ifstream&, double list[], int&); // reads list into array and finds length of list void findAverage(double list[], double&, int); // finds average... void sortList(double list[], int); // uses the array and length to sort the array in ascending order void outData(ofstream&, double list[], int, double); // outputs the sorted list and average int main() { ifstream inFile; ofstream outFile; int length; double average; openFile(inFile); readData(inFile, list, length); findAverage(list, average, length); sortList(list, length); outData(outFile, list, length, average); system("pause"); return 0; } // end main void openFile(ifstream& inputFile) { string userInputFile; cout << "Enter the name of the file you would like to work with: \t"; cin >> userInputFile; cout << "\n"; inputFile.open(userInputFile.c_str()); } void readData(ifstream& inputFile, double list[], int& theLen) { int i = 0; while(inputFile) { inputFile >> list[i]; ++i; } theLen = i-1; } void findAverage(double list[], double& theAverage, int theLen) { double sum; for(int i = 0; i < theLen; ++i) sum += list[i]; theAverage = sum / theLen; } void sortList(double list[], int theLen) { int i, j; int min; double temp; for (i = 0; i < theLen-1; ++i) { min = i; for (j = i + 1; j < theLen; ++j) if (list[j] < list[min]) min = j; temp = list[min]; list[min] = list[i]; list[i] = temp; } // end for } void outData(ofstream& outputFile, double list[], int theLen, double theAverage) { string userOutputFile; cout << "Name the file you would like to output the data to: \t"; cin >> userOutputFile; outputFile.open(userOutputFile.c_str()); for(int i = 0; i < theLen; ++i) { outputFile << list[i] << endl; cout << list[i] << endl; } cout << "The average is: " << theAverage << endl; outputFile << "The average is: " << theAverage << endl; } ``````
Hey I'm sorry I keep forgetting to tell you THANK YOU! I finally got it worked out with your help!!
Topic archived. No new replies allowed.