57.7k views
5 votes
Write a fragment of code that opens a file named numbers.txt, reads in pairs of number until end of file and sends the smaller of the two to a file named min.txt and the larger of the two to a file named max.txt (one number per line). You can assume there are no ties. (basic file input/output)

1 Answer

5 votes

Answer:

See explaination for code

Step-by-step explanation:

int x, y;

ifstream numbers;

numbers.open ("numbers.txt");

ofstream min;

min.open ("min.txt");

ofstream max;

max.open ("max.txt");

while(numbers>>x>>y )

{

if(x>y)

{

max<<x<<endl;

min<<y<<endl;

}

else

{

max<<y<<endl;

min<<x<<endl;

}

}

numbers.close;

min.close;

max.close;

User Connor Fuhrman
by
4.6k points