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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#include <iostream>
#include <fstream> // I/O
#include <iomanip> // For setw()
using namespace std;
ofstream outputfile("output.txt");
const int MAX_FILE_NAME = 35; // Maximum file name length
const int MAX_NUMBER_SCORES = 40; // Maximum number of values in array
void open_input(ifstream& input, char name[]); // Get file name, Open file
void read_values(ifstream& input, double v[], int size, int& used); // Read values from file
void find_max_min(const double v[], int n, double& max, double& min);// Find max/min in array
double find_average(ifstream& input);
void output(const char name[], const double v[], int n,
double average, ostream &out = cout); // Print Results
void sort(double a[], int number_used);
//Precondition: number_used <= declared size of the array a.
//The array elements a[0] through a[number_used - 1] have values.
//Postcondition: The values of a[0] through a[number_used - 1] have
//been rearranged so that a[0] <= a[1] <= ... <= a[number_used - 1].
void swap_values(double& v1, double& v2);
//Interchanges the values of v1 and v2.
int index_of_smallest(const double a[], int start_index, int number_used);
//Precondition: 0 <= start_index < number_used.
//Referenced array elements have values.
//Returns the index i such that a[i] is the smallest of the values
//a[start_index], a[star_index + 1], ..., a[number_used - 1].
int main()
// Parameters: None
// Returns: Zero
// Calls: open_input(), read_values(), 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; // Input file of doubles
double scores[MAX_NUMBER_SCORES]; // Array to hold scores processed
//double max, min; // Maximum and minimum numbers from file
double average;
int num_scores; // Number of scores in file
cout << "This program can find the average number in a file\n"
<< "of at most " << MAX_NUMBER_SCORES << " floating-point values.\n" << endl;
system("pause"); // Hold message on screen until key is pressed
do
{ open_input(input_numbers, file_name); // Get file name & open file
read_values(input_numbers, scores,
MAX_NUMBER_SCORES, num_scores); // Read values
input_numbers.close(); // Close file
if (num_scores > 0)
{ find_average(scores, num_scores, average); // Find max & min values in array
sort(scores, num_scores);
output(file_name, scores, num_scores, average); //Print results on screen
output(file_name, scores, num_scores, average, outputfile); // and outputfile
}
else
{ cout << "\n\n\aNo data in file: " << file_name << endl;
}
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
// Parameters: Variables for input file reference nad 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 to read only if file exists
} while (input.fail() ); // If can't open file, try again
} // End of open_input()
void read_values(ifstream& input, double v[], int size, int& used) // Read values
// Parameters: Variables for file reference, variable for array reference,
// value for array size and variable for number of values in array
// Returns: None
// Calls: None
{
double value; // Value from file
int count = 0; // Count number of values in file
while (count < size && input >> value) // Continue as long as there is
// room in the array and we can read
{ v[count] = value; // a number from file.
count ++;
}
used = count;
} // End of read_values()
void find_max_min(const double v[], int n, double& max, double& min) // Find max & min values
// Parameters: Variables for array reference, value for number of values
// and variables for max and min values
// Returns: None
// Calls: None
{
int i; // Array index and loop counter
max = min = v[0];
for (i = 1; i < n; i++) // Start with 1 since max & min initialized to v[0]
{
if (v[i] > max) max = v[i];
if (v[i] < min) min = v[i];
}
} // 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 i; // Value from file
double sum = 0;
int count = 0;
for (i = 0; i < n; i++) // Continue as long as we can read a number from file.
{
sum += v[i];
count++;
}
return sum/count;
}
void output(const char name[], const double v[], int n,
double average, ostream& out)
// Parameters: File name, array reference, number of values and max & min values
// Returns: None
// Calls: None
{ int i; // Array index and loop counter
out.setf(ios::fixed);
out.setf(ios::showpoint);
out.precision(1);
out << "\n\nInput File Name: " << name << endl;
out << "\nFile Contents:" << endl;
for (i = 0; i < n; )
{ out << setw(8) << v[i];
if ( ++i % 5 == 0) out << "\n"; // Print only 5 values per line
}
if ( i % 5 != 0) out << "\n"; // Newline if last line is short
out << "\nAverage Number in File: " << setw(8) << average << endl;
} // End of output()
void sort(double a[], int number_used)
{
int index_of_next_smallest;
for (int index = 0; index < number_used - 1; index++)
{//Place the correct value in a[index]:
index_of_next_smallest =
index_of_smallest(a, index, number_used);
swap_values(a[index], a[index_of_next_smallest]);
//a[0] <= a[1] <=...<= a[index] are the smallest of the original array
//elements. The rest of the elements are in the remaining positions.
}
}
void swap_values(double& v1, double& v2)
{
double temp;
temp = v1;
v1 = v2;
v2 = temp;
}
int index_of_smallest(const double a[], int start_index, int number_used)
{
double min = a[start_index];
int index_of_min = start_index;
for (int index = start_index + 1; index < number_used; index++)
if (a[index] < min)
{
min = a[index];
index_of_min = index;
//min is the smallest of a[start_index] through a[index]
}
return index_of_min;
}
|