modify the output function to accept the average as a double parameter

having trouble with this problem, but I know I'm supposed to:
1. create a variable to store the average
2. have the function read the file find the average of the numbers and assign to your variable average
3. modify the output function to accept the average as a double parameter

In the code on line 35 I've commented out the find_max_min as I'm not worried about that part of the problem. I just need to know how to do the 3 things above. here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <fstream> // I/O
#include <iomanip> // For setw()
using namespace std;

ofstream outputfile("output.txt");
const int MAX_FILE_NAME = 35; // Maximum space allocated for file name

void open_input(ifstream& input, char name[]);          // Get file name & Open file
void find_max_min(ifstream& input, int& max, int& min); // Find max & min values
double find_average(ifstream& input);
void output(const char name[], int max, int min, ostream& os = cout); // Print results

int main() 
// Parameters: None
// Returns:    Zero
// Calls:      open_input(), find_max_min(), output()     
{  char again;                        // Does user want to go through loop again?
   char file_name[MAX_FILE_NAME + 1]; // Name of file to be processed
   ifstream input_numbers;            // For working with input file
   
   int max, min;

   double average;

   
   cout << "This program can find the largest and smallest numbers in a file\n"
        << "of integers.\n" << endl;
   system("pause"); // Hold message on screen until key is pressed

   do 
   {  
      system("cls");                           // Clear screen
      open_input(input_numbers, file_name);    // Get file name & open file
      // find_max_min(input_numbers, max, min);   // Find max & min values in file
      average = find_average(input_numbers);

      input_numbers.close();                   // Close file
      output(file_name, max, min);             // Print results on screen
      output(file_name, max, min, outputfile);    // Print results on outputfile

      cout << "\nDo you want to process another file (Y/N)? ";
      cin >> again;
      cin.ignore(256, '\n');  // Remove Enter key from keyboard buffer
   
   } while ( again == 'y' || again == 'Y'); 

   cout << "\nEnd of Program!" << endl;
   outputfile << "\n\nThanks for using MaxMin!\f"; 
   outputfile.close();
   
   return 0; 
}  // End of main()

void open_input(ifstream& input, char name[]) //Open file, exit on error
// Parameters: Variables for input file reference and input file name
// Returns:    None
// Calls:      None
{  int count = 0;             // Count number of tries
   
   do // Continue until we get a valid file name and can open file
   {  
      count++;
      
      if (count != 1)  // Issue error message if we are trying again.
      {  cout << "\n\aInvalid file name or file does not exist. Please try again." 
              << endl;
      }
   
      cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
           << " characters please)\n:> ";
      cin.get(name, MAX_FILE_NAME + 1);// Gets at most MAX_FILE_NAME characters
      cin.ignore(256, '\n');           // Remove Enter key from keyboard buffer
      
      input.clear();                   // Clear all error flags, if any, from prev try
      input.open(name, ios_base::in); // Open only if file exists
   
   } while (input.fail() );            // If can't open file, try again
} // End of open_input()

void find_max_min(ifstream& input, int& max, int& min) // Find max & min values
// Parameters: Variables for file reference and max and min values
// Returns:    None
// Calls:      None
{  
   int value;              // Value from file

   input >> value;         // Read first number
   max = min = value;      // Initialize max & min to first number
   while (input >> value)  // Continue as long as we can read a number from file.
   {  
      if (value > max) max = value;
      if (value < min) min = value;
   }
} // End of find_max_min()

double find_average(ifstream& input)
// Parameters: Variables for file reference and max and min values
// Returns:    None
// Calls:      None
{  
   double value;              // Value from file
   double sum = 0;
   int count = 0;
  
         // Initialize max & min to first number
   while (input >> value)  // Continue as long as we can read a number from file.
   {  
      sum += value;
      count++;
   }
return sum/count;
}


void output(const char name[], int max, int min, ostream& os) // Print results
// Parameters: File name, max & min values from file, output stream
// Returns:    None
// Calls:      None
{  os << "\n\nInput File Name        : " << name << endl;
   os << "Largest Number in File : " << setw(8) << max << endl;
   os << "Smallest Number in File: " << setw(8) << min << endl;
} // End of output() 


any help would be greatly appreciated
Last edited on
Anyone?
I don't really get the problem, it seems to me your function for calculating averages is fine, so what's the question?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double find_average(ifstream& input)
// Parameters: Variables for file reference and max and min values
// Returns:    None
// Calls:      None
{  
   double value;              // Value from file
   double sum = 0;
   int count = 0;
  
   // Initialize max & min to first number
   while (input >> value)  // Continue as long as we can read a number from file.
   {  
      sum += value;
      count++;
   }
   return sum/count;
}

The above seems fine, you need to also 3. modify the output function so it takes average value.
1
2
3
4
5
6
7
8
9
void output(const char name[], int max, int min, ostream& os, double average) // Added average to params
// Parameters: File name, max & min values from file, output stream, + Average
// Returns:    None
// Calls:      None
{  os << "\n\nInput File Name        : " << name << endl;
   os << "Largest Number in File : " << setw(8) << max << endl;
   os << "Smallest Number in File: " << setw(8) << min << endl;
   os << "Average of the numbers are: " << setw(8) << average << endl;
} // End of output()  


back in main when you call output simply add the value for average in the params:
39
40
      output(file_name, max, min);             // Print results on screen
      output(file_name, max, min, outputfile);    // Print results on outputfile 

your going to have trouble here because you have not declared overloaded functions.
all you need to do is perhaps, create one that simply calls the other with a generic ostream& ? or just change line 39.

eg.
1
2
3
4
5
// overloaded function used for ???
void output(const char name[], int max, int min) 
{  
   output(name,max,min, /*something goes here */ );
}  


or line 39 might be changed to: output( name, min, max, cout ); I don't know if that will work, but it might...
Last edited on
Topic archived. No new replies allowed.