#include void swap_if(int & x, int & y) { if (x > y) { int temp = x; x = y; y = temp; } } void main() { int A[1000]; ifstream fin; fin.open("data"); if (fin.fail()) { cout << "Can't find file\n"; exit(1); } /* Useful update operators, simplify common actions, recommended var += x; equivalent to var = var + x; and -= *= /= %= &&= ||= etc... */ /* These update operators are discouraged and not recommended (by me) var ++ sort of the same as var += 1 ++ var sort of the same as var += 1 var -- sort of the same as var -= 1 -- var sort of the same as var -= 1 could say int x = 5; cout << "answer is " << x++ << "\n"; cout << "answer is " << ++x << "\n"; cout << "answer is " << y+=1 << "\n"; */ int num; for (num = 0; num <= 1000; num += 1) { fin >> A[num]; if (fin.fail()) break; } cout << num << " numbers successfully read\n"; fin.close(); for (int line = num - 1; line >= 0; line -= 1) for (int i = 0; i <= line; i += 1) swap_if(A[i], A[i+1]); ofstream fout; fout.open("data"); for (int i = 0; i <= num; i += 1) fout << A[i] << "\n"; fout.close(); }