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.

Please tell me if you need any more info. ANY help is greatly appreciated.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    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:

1
2
3
4
5
6
7
8
9
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....



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <fstream>
#include <string>

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.