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

For this exercise, you will repeatedly read numbers from a text file. The number

ID: 3869606 • Letter: F

Question

For this exercise, you will repeatedly read numbers from a text file. The numbers in the file represent daily rainfall amounts. The list may contain the number -999, which is an indicator (or sentinel value) that the end of the data we are interested in has been reached and you can stop reading the file. The program will produce the average of the non-negative values in the list up to the -999 (if it shows up). There may be negative numbers other than -999 in the list. The text file to be used with this application is attached: rainfall.txt Furthermore, you will read these positive values and store them in an array having a type appropriate for the data in the file. You will then calculate the average, not while reading the file, but rather by iterating the array. In addition to calculating the average (by iterating through the array) I also want you to find the minimum and maximum values. Your application will output three values only: average, minimum value, and maximum value. Each should be labeled properly in the output. One of the issues in constructing an array is that you must first know how many items you are going to place onto the array. Reading rows from a file is not conducive to this as you rarely know how many rows you will read before you actually read them. Therefore, programmers generally follow one of two strategies for this issue: 1.Declare the array with a maximum value that is high enough to ensure you will never need that many elements. 2.Read the file twice: the first time to determine how many rows there are and the second time to read the data into the array. Neither of the above solutions are ideal. In the first solution you sacrifice memory because you have to initialize the array to be large enough to more than accommodate the file that you will read. In the second solution you waste processing time and disk I/O because you read the same rows from the file twice. You can develop your solution using a custom class object, but this is not required. If you do use a custom class object I will count it as extra credit. In your code, I should see the use of functions keeping as much of the code out of the main() method as possible.

Explanation / Answer

#include<iostream>
#include<fstream>

using namespace std;

int main(){
  
   ifstream fin;
   int input;
   int count;
   int max, min, sum;
   double avg;
   int data[500]; //Assuming the number of data is less than 500

   fin.open("rainfall.txt");

   if (fin){
         count= 0;
         while (fin >> input){
               if (input == -999)
                  break;
               if (input > 0){
                  data[count] = input;
                  count++;
               }
         }
         fin.close();
         max = data[0];
         min = data[0];
         sum =0;
         for (int i = 0; i<count; i++){
             sum = sum + data[i];
             if (max < data[i])
                 max = data[i];
             if (min > data[i])
                 min = data[i];
         }
         cout << "Maximum:" << max << endl;
         cout << "Minimum:" << min << endl;
         cout << "Average:" << sum/count << endl;
        
   }
   else {
      cout << "Error in opening file";
   }
   return 0;
}

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