Sorting Program

Hello, I'm working a programming problem for class. Need help.

Prompt the user for the name of an input file. Open and read the first line of this user-specified input file. Create a double array with a size to match the first line of the input file. So this means that if the first line of the input file is a 100, the array will have 100 slots. If the first line of the input file is a 200, the array will have 200 slots. This means that the array must be dynamically sized according to the first line read from the file. Your program should then read the remaining numbers into the array. Once the array has been filled, sort the array from low to high. After the sort is complete, use a loop to sum the elements in the array, and then calculate the average of all values within the array. Prompt the user for the name of an output file. Open this user-specified output file and write the following information to this file: the number of slots in the array, followed by the sorted list of numbers (one per line), and finally, the average value within the array. Use the provided scores.dat test data file to get your program working.
scores.dat
10
90.5
100
89.9
80.10
79.95
70
69
60.5
59.0
0.0

Sorted list: output.dat
10
0.00
59.00
60.50
69.00
70.00
79.95
80.10
89.90
90.50
100.00
69.89

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
#include <stdafx.h>
#include <iostream>
#include <fstream>
#include <cstdlib> // needed for exit()
#include <string>
using namespace std;

int main()
{
	// program goes here
	string filename;
	ifstream inFile;
	cout << "Please enter the name of the file you wish to open: ";
	cin  >> filename;
	inFile.open(filename.c_str());  // open the file
	if (inFile.fail())  // check for successful open
	{
		cout << "\nThe file named " << filename
			 << " was not successfully opened"
			 << "\n Please check that the file currently exists."
			 << endl;
		cin.ignore();
		exit(1);
	}
	cout << "\nThe file has been successfully opened for reading.\n";

	cin.ignore();
	return 0;
}


void sort(double nums[], int numels)
{
	// sort function

	
}
What exactly do you need help with? The instructions are pretty clear and you can find sorting algorithms online if you need help with that.

But pretty much, read in the size of the array and the elements from your filestream, then sort the array.
Last edited on
Topic archived. No new replies allowed.