#include <iostream>
#include <ctime>
#include <vector>
#include <iomanip>
using namespace std;
const unsigned int n = 7; //days
const unsigned int m = 8; //temperature measured every three hours within a day
/**The function takes in input a matrix and calculate the daily averages of temperatures, storing them in a vector.*/
vector<double> dailyAveragesOfTemperature(double matrix[n][m]) {
vector<double> d;
double sum;
int i, j;
for (i = 0; i < n; i++) {
sum = 0;
for (j = 0; j < m; j++) {
sum += matrix[i][j];
}
d.push_back(sum / m);
}
return d;
}
int main()
{
double matrix[n][m];
srand(static_cast<unsigned int>(time(0)));
int i, j;
/**The Matrix is filled with random values from -40 to +40.*/
/**At the same time, the matrix is displayed on the screen.*/
cout << " ";
for (i = 0; i < m; i++) {
cout << fixed << setw(5) << setprecision(2) << setfill('0') << i*3.0 << " ";
}
cout << endl;
cout << " ";
for (i = 0; i < m; i++) {
cout << "--------";
}
cout << endl;
for (i = 0; i < n; i++) {
cout << fixed << "day " << i+1 << " |";
for (j = 0; j < m; j++) {
matrix[i][j] = (double)(rand()) / RAND_MAX * 80.0 - 40.0;
cout << fixed << setw(7) << setprecision(2) << setfill(' ') << matrix[i][j] << " ";
}
cout << endl;
}
cout << endl;
/**The vector is filled with average temperature values.*/
vector<double> dAOT = dailyAveragesOfTemperature(matrix);
cout << "The daily averages of temperatures : " << endl;
for (i = 0; i < n; i++) {
cout << fixed << "day " << i + 1 << " |";
cout << fixed << setw(7) << setprecision(2) << setfill(' ') << dAOT.at(i);
cout << endl;
}
cout << endl;
double minT = dAOT.at(0), maxT = dAOT.at(0);
int minDay = 0, maxDay = 0;
for (i = 1; i < n; i++) {
if (dAOT.at(i) > maxT) {
maxT = dAOT.at(i);
maxDay = i;
}
if (dAOT.at(i) < minT) {
minT = dAOT.at(i);
minDay = i;
}
}
cout << fixed << setw(7) << setprecision(2) << setfill(' ')
<< "On the " << minDay+1 << " day, there was a minimum average daily temperature " << minT << endl;
cout << "On the " << maxDay + 1 << " day, there was a maximum average daily temperature " << maxT << endl;
}
Comments
Leave a comment