As a form of quality control, the Pancake Kitchen has recorded, on a Pancake file, two measurements for each of its pancakes made in a certain month: the thickness in mm (millimetres) and the diameter in cm (centimetres). Each record on the file contains the two measurements for a pancake, thickness followed by diameter. The last record in the file contains values of 99 for each measurement. Design a program that will read the Pancake file, calculate the minimum, the maximum and the average for both dimensions, and print these values on a report.
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
int n;
int min_th, max_th, thickness, sum_th;
int min_d, max_d, diameter, sum_d;
ifstream ifs("pancake.txt");
n = 0;
min_th = min_d = 999;
max_th = max_d = 0;
sum_th = sum_d = 0;
while (ifs) {
ifs >> thickness >> diameter;
if (thickness == 99 && diameter == 99) {
break;
}
sum_th += thickness;
if (min_th > thickness) {
min_th = thickness;
}
if (max_th < thickness) {
max_th = thickness;
}
sum_d += diameter;
if (min_d > diameter) {
min_d = diameter;
}
if (max_d < diameter) {
max_d = diameter;
}
n++;
}
double avg_th, avg_d;
avg_th = static_cast<double>(sum_th) / n;
avg_d = static_cast<double>(sum_d) / n;
cout << "Thickness (mm): min = " << min_th << ", max = " << max_th
<< fixed << setprecision(2) << ", average = " << avg_th << endl;
cout << "Diameter (cm): min = " << min_d << ", max = " << max_d
<< fixed << setprecision(2) << ", average = " << avg_d << endl;
return 0;
}
Comments
Leave a comment