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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
#include <iostream>
#include <vector>
#include <array>
#include <limits>
/*
Program to:
Temperature of a month for a given year.
High and low temperatures for a given year.
The average temperature for a given year.
The average temperature of a month for all years.
*/
#define MONS_IN_YR 12
// Get a valid month (1-Jan,... 12-Dec)
unsigned int get_a_valid_month()
{
unsigned int month;
// Keep looping till a valid month is given.
do
{
std::cout << std::endl << "Enter month (1-12): ";
std::cin >> month;
if(month>=1 && month <=MONS_IN_YR)
break;
else
{
std::cout << "Invalid month. Try again.";
exit(0);
}
}while(1);
return month;
}
// Get a valid month (Valid range 2010-2013)
unsigned int get_a_valid_year()
{
unsigned int year;
// Keep looping till a valid year is given.
do
{
std::cout << std::endl << "Enter year (2010-2013): ";
std::cin >> year;
if(year>=2010 && year <=2013)
break;
else
{
std::cout << "Invalid year. Try again.";
exit(0);
}
}while(1);
return year;
}
// Class to contain/implement: Year and its Temperatures
class year_temp_arr
{
private:
// Two dimensional arear using vector.
std::vector< std::array<double,MONS_IN_YR> > two_d_arr;
public:
// Default constructor. Assume no of years as 1. Default 2d array of size 1 X 12. Initialized with zero.
year_temp_arr():two_d_arr(1, { { 0,0,0,0,0,0,0,0,0,0,0,0} } ) {}
// Constructor with dimensions (no of years). Initialized with zero.
year_temp_arr(int row):two_d_arr(row, { { 0,0,0,0,0,0,0,0,0,0,0,0} } ) {}
// Constructor with dimensions (no of years). Initialized with the intializer provided.
year_temp_arr(int row, const double *vals);
// Print the values in two dimensional array.
void print_yr_n_tmp();
// Print the temperature of month, year.
void print_temp_of_a_month();
// Get the minimum and maximum temperature in year.
void get_min_max_temp(int year, double &min, double &max);
// Print the minimum and maximum temperature in year.
void print_hi_n_low_temp_of_a_month();
// Print the average temperature in year.
void print_avg_temp_of_a_year();
// Compute average temperature in year.
double get_avg_temp_of_a_year(int year);
// Print the average temperature in month.
void print_avg_temp_of_a_month();
};
year_temp_arr::year_temp_arr(int row, const double *vals):two_d_arr(row, { { 0,0,0,0,0,0,0,0,0,0,0,0} } )
{
// Fill the two_d_arr with the initializer values in vals.
for(int ri=0; ri<row; ++ri)
for(int ci=0; ci<MONS_IN_YR; ++ci)
two_d_arr[ri][ci]=vals[(ri*MONS_IN_YR)+ci];
}
// Print the temperature of all the years.
void year_temp_arr::print_yr_n_tmp()
{
std::cout << std::endl;
for (std::vector< std::array<double,MONS_IN_YR> >::iterator i = two_d_arr.begin(); i != two_d_arr.end(); ++i)
{
for(int mon_i=0; mon_i <MONS_IN_YR; ++mon_i)
std::cout << "\t" << (*i)[mon_i];
std::cout << std::endl;
}
}
// Print the temperature of month, year (got from user as input).
void year_temp_arr::print_temp_of_a_month()
{
// Get the month.
unsigned int month = get_a_valid_month();
unsigned year = get_a_valid_year();
// Print the temperature of month, year.
std::cout << std::endl << "Temperature of month " << month << " of year "
<< year << " is: " << two_d_arr[year-2010][month-1] << std::endl;
return;
}
// Find the minimum and maximum temperature of a year.
void year_temp_arr::get_min_max_temp(int year, double &min, double &max)
{
double temp;
min=0.0;
max=0.0;
// Find the minimum and maximum temperature in year.
for(int mon_i=0; mon_i <MONS_IN_YR; ++mon_i)
{
temp = two_d_arr[year-2010][mon_i];
if(min > temp) min = temp;
if(max < temp) max = temp;
}
return;
}
// Find the average temperature of a year.
double year_temp_arr::get_avg_temp_of_a_year(int year)
{
double sum=0, avg=0;
// Find the average temperature in year.
for(int mon_i=0; mon_i <MONS_IN_YR; ++mon_i)
sum += two_d_arr[year-2010][mon_i];
avg = sum/MONS_IN_YR;
return avg;
}
// Print the minimum and maximum temperature in year.
void year_temp_arr::print_hi_n_low_temp_of_a_month()
{
double min, max;
// Get the year from user.
unsigned int year = get_a_valid_year();
// Compute the minimum and maximum temperature in year.
get_min_max_temp(year, min, max);
// Print the minimum and maximum temperature in year.
std::cout << std::endl << "High and low temperatures of year " << year
<< " are " << min << " " << max << std::endl;
return;
}
// Print the average temperature in year.
void year_temp_arr::print_avg_temp_of_a_year()
{
double avg;
// Get the year from user.
unsigned int year = get_a_valid_year();
// Compute the average temperature in year.
avg = get_avg_temp_of_a_year(year);
// Print the average temperature in year.
std::cout << std::endl << "The average temperature for year " << year
<< " is " << avg << std::endl;
return;
}
// Print the average temperature of a month (of several years).
void year_temp_arr::print_avg_temp_of_a_month()
{
double avg, sum=0;
// Get the month from user.
unsigned int month = get_a_valid_month();
// Compute the average temperature of a month (of several years).
for (std::vector< std::array<double,MONS_IN_YR> >::iterator i = two_d_arr.begin(); i != two_d_arr.end(); ++i)
sum += (*i)[month-1];
avg = sum/two_d_arr.size();
// Print the average temperature of a month (of several years).
std::cout << std::endl << "The average temperature of month " << month
<< " is " << avg << std::endl;
return;
}
int main()
{
// Monthwise temperature of year 2010, 2011, 2012, 2013
const double temps[]=
{
-16.1, -13.5, 15.9, 28.1, 30.2, 32.7, 35.8, 30.5, 25.3, 18.5, 5.0, -1.5, // 2010
-15.1, -13.9, 17.9, 25.5, 28.5, 31.5, 34.8, 28.9, 26.4, 15.8, 2.8, -2.8, // 2011
-21.1, -15.9, 20.9, 23.4, 29.4, 30.8, 29.6, 24.6, 27.8, 19.8, 3.4, 0.0, // 2012
-22.1, -13.9, 19.9, 21.6, 31.2, 29.5, 33.2, 35.1, 24.6, 14.3, 5.7, -8.0 // 2013
};
year_temp_arr yr_n_tmp(4,temps);
do
{
int option;
std::cout << std::endl
<< "1. Temperature of a month for a given year." << std::endl
<< "2. High and low temperatures for a given year." << std::endl
<< "3. The average temperature for a given year." << std::endl
<< "4. The average temperature of a month for all years." << std::endl
<< "5. Exit program." << std::endl
<< "Enter your option(1-4): ";
std::cin >> option;
switch(option)
{
case 1:
yr_n_tmp.print_temp_of_a_month();
break;
case 2:
yr_n_tmp.print_hi_n_low_temp_of_a_month();
break;
case 3:
yr_n_tmp.print_avg_temp_of_a_year();
break;
case 4:
yr_n_tmp.print_avg_temp_of_a_month();
break;
case 5:
exit(0);
default:
std::cout << std::endl << "Invalid option. Try again." << std::endl;
exit(0);
};
}while(1);
return 0;
}
|