#include #include using namespace std; void bubbleSort(double* a, int n); int main() { ifstream inputFile("input.txt"); if (!inputFile.is_open()) { cerr << "Cannot open an input file." << endl; return (-1); } // Input an array int n; // Length of array if (!(inputFile >> n) || n < 0) { cerr << "Incorrect format of input file." << endl; return (-1); } double *a = new double[n]; for (int i = 0; i < n; ++i) { if (!(inputFile >> a[i])) { cerr << "Incorrect format of input file." << endl; delete[] a; return (-1); } } inputFile.close(); bubbleSort(a, n); // Output the result ofstream outputFile("output.txt"); if (!outputFile.is_open()) { cerr << "Cannot open an output file." << endl; return (-1); } for (int i = 0; i < n; ++i) { if (i > 0) { // Write a delimiter if (i%5 == 0) { outputFile << endl; } else { outputFile << ' '; } } outputFile << a[i]; } outputFile << endl; outputFile.close(); delete[] a; return 0; } void bubbleSort(double* a, int n) { bool sorted = false; int k = 0; while (!sorted) { sorted = true; for (int i = 0; i < n-1-k; ++i) { if (a[i] > a[i+1]) { sorted = false; double tmp = a[i]; a[i] = a[i+1]; a[i+1] = tmp; } } ++k; } }