Ask Question
4 January, 12:20

This program will read integers from a file and find results from these integers. Open the file, test to make sure the file opened. Use a loop to read the integers and process each integer as it is read. When End Of File is reached, close the file. After all of the integers have been read and processed, print the results for the following output: The integer count: The sum of the integers: The smallest integer: The largest integer: The average of the integers: Use notepad, vim, or other simple text editor to create a file named file1. txt containing the following, with one integer per line. Test the program two times. Use the following data in the first test: 11 9 18 22 27 33 21 For the second test add an additional line containing the number 40.

+2
Answers (1)
  1. 4 January, 15:26
    0
    this is in cpp

    #include

    #include

    using namespace std;

    int main ()

    {

    ifstream myfile;

    myfile. open ("file1. txt");

    / / Check if file is opened

    if (! myfile. is_open ())

    {

    cout<<"Error: File could not be opened"<
    }

    else{

    / / variable to stor count, sum, minimum and maximum

    int ct=0, sm=0, mn=INT_MAX, mx=INT_MIN;

    / / variable for taking input

    int x;

    / / reading and processing in loop until there are more integers

    while (myfile>>x) {

    ct++;

    sm+=x;

    mn = min (mn, x);

    mx=max (mx, x);

    }

    / / close the file when End of file is reached

    myfile. close ();

    //printing results

    cout<<"The integer count: "<
    cout<<"The sum of the integers: "<
    cout<<"The smallest integer: "<
    cout<<"The largest integer: "<
    cout<<"The average of the integers: "<< ((sm*1.0) / ct) <
    }

    return 0;

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “This program will read integers from a file and find results from these integers. Open the file, test to make sure the file opened. Use a ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers