Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please code in C++: Write a program that will search a file of numbers of type i

ID: 3828827 • Letter: P

Question

Please code in C++:

Write a program that will search a file of numbers of type int and write the largest and the smallest numbers to the screen. The file contains nothing but numbers of type int separated by blanks or line breaks. If this is being done as a class assignment, obtain the file name from your instructor.

Additional Notes(from my class):

-No global variables may be used in any of our assignments. If I see a global variable being declared, a zero points will be assigned.
Clarification:

-While variables may not be global, all functions and constants should be declared global.

- Beginning from Chapter 4, all assignment must contain functions, even if it is possible to do without one.

-If the assignment calls for just a function, you must code a driver to test the function. The driver and the function both must be submitted.

-(C6 PC1)

Thank you :)

Explanation / Answer

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <stdlib.h>
using namespace std;

void makes_neat(ifstream& messy_file, ofstream& neat_file,
int number_after_decimalpoint, int fields_width);

int main ()
{
using namespace std;
ifstream in_streams;
ofstream out_streams;

in_streams.open("infile.txt");
out_streams.open("outfile.txt");

if (in_streams.fail( ))
{
cout << "Input file opening may be failed. ";
exit(1);
}
if (out_streams.fail( ))
{
cout << "Output file opening may befailed. ";
exit(1);
}

makes_neat(in_streams, out_streams, 5, 1);

int largest, smallest;
in_streams >> largest >> smallest;
out_streams << "largest = "
<< largest << endl;
out_streams << "smallest = "
<< smallest << endl;

in_streams.close( );
out_streams.close( );

return 0;
}

void makes_neat(ifstream& messy_file, ofstream& neat_file,
int number_after_decimalpoint, int fields_width)
{
neat_file.precision(number_after_decimalpoint);

cout.precision(number_after_decimalpoint);

double next;
while (messy_file >> next)
{
cout << setw(fields_width) << next << endl;
neat_file << setw(fields_width) << next << endl;
}
}