/* 7. Write a program to read a file containing 50,000 records of three columns of numbers. The first two columns are type int and the third is type double. Store all the doubles in an array. Sort the array from low to high and print into a new file. Makeup your own file names. */ #include #include #include using namespace std; void sort(double array[], int size); void swap(double&, double&); const int Size = 50000; int main() { double array[Size]; ifstream fin("input.txt"); if (!fin) { cerr << "Cannot open input.txt" << endl; exit(1); } ofstream fout("output.txt"); if (!fin) { cerr << "Cannot open output.txt" << endl; exit(2); } int dummy; for (int i = 0; i < Size; i++) { fin >> dummy >> dummy >> array[i]; } sort(array,Size); for (int i = 0; i < Size; i++) { fout << array[i] << endl; } } // selection sort void sort(double array[], int size) { int minIndex; for (int i = 0; i < size - 1; i++) { minIndex = i; for (int j = i + 1; j < size; j++) { if (array[j] < array[minIndex]) minIndex = j; } if (minIndex != i) swap(array[i],array[minIndex]); } } void swap(double& a, double& b) { double temp; temp = a; a = b; b = temp; }