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

This C++ code does the following: *opens a file with random numbers *counts how

ID: 3680670 • Letter: T

Question

This C++ code does the following:

*opens a file with random numbers

*counts how many numbers there are

*gets the average

*gets the largest value

* gets the smallest value

I am having trouble finding the smallest value, I get a wierd (wrong) number when the program executes.

What needs to be corrected or addded to display the correct smallest value?

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main ()
{
   int number,
   numbers,
       max,
           min = max;
   double sum = 0.0;
double average = 0.0;

  
   ifstream inputFile;
   inputFile.open("Numbers.txt");
      
  
while(inputFile >> number)
{
           if(number > max)
               max = number;
          
           if(number < min)
               min = number;
      
   numbers++;
   sum += number;      
       }
      
       average = sum / numbers;
      
cout << " Count = " << numbers << endl;
       cout<<setprecision(2)<<fixed;
cout << "Average = " << average << endl;
       cout<<setprecision(0)<<fixed;
       cout << "Largest Value = " << max << endl;
       cout << "Smallest Value =" << min << endl;

   inputFile.close();
   return 0;
}

Explanation / Answer

1. There is a small mistake in this program with data type.

2. If you're reading min, max values in int's, then largest/smallest should be of type int too.

3. But, here you have to change min & max variables data type into double then you will get correct output.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote