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
|
#include <iostream>
using namespace std;
int choose_item()
{
int choice;
do
{
cout << "\nMenu: " << endl;
cout << "1. Print Daily Temperature for the whole month" << endl;
cout << "2. Print Daily Temperature of a choosen day" << endl;
cout << "3. Change Temperature of a day" << endl;
cout << "4. Calculate Average Temperature" << endl;
cout << "5. Find The Hottest and Coldest Day of the Month" << endl;
cout << "6. Exit Program" << endl;
cout << "What do you want to do (Choose 1 to 6)? ";
cin >> choice;
cin.ignore();
}while(choice < 1 || choice > 6);
return choice;
}
void apply_choice(int choice, double (&temperature)[31])
{
switch(choice)
{
case 1:
{
cout << "Daily Temperature This Month: " << endl;
for(int i = 0; i < 31; i++)
{
cout << "Day " << i+1 << ": " << temperature[i] << " Celsius" << endl;
}
break;
}
case 2:
{
int day;
do
{
cout << "Choose the day (1 to 31): ";
cin >> day;
cin.ignore();
} while(day < 1 || day > 31);
cout << "Day " << day << ": " << temperature[day-1] << " Celsius" << endl;
break;
}
case 3:
{
int day;
do
{
cout << "Choose the day (1 to 31): ";
cin >> day;
cin.ignore();
} while(day < 1 || day > 31);
cout << "The existing temperature is " << temperature[day-1] << " Celsius. Enter the new temperature: ";
cin >> temperature[day-1];
cin.ignore();
break;
}
case 4:
{
double total = 0.0;
for(int i = 0; i < 31; i++)
{
total += temperature[i];
}
cout << "The average temperature this month is " << total/31.0 << " Celsius" << endl;
break;
}
case 5:
{
int highestDay = 0, lowestDay = 0;
for(int i = 1; i < 31; i++)
{
if(temperature[i] > temperature[highestDay])
{
highestDay = i;
}
if(temperature[i] < temperature[lowestDay])
{
lowestDay = i;
}
}
cout << "The hottest day (" << temperature[highestDay] << " Celsius) is on day " << highestDay+1 << " of the month" << endl;
cout << "The coldest day (" << temperature[lowestDay] << " Celsius) is on day " << lowestDay+1 << " of the month" << endl;
break;
}
default:
cout << "\nThank you for using the Monthly Temperature Analysis Program. Have a nice day!" << endl;
break;
}
}
int main()
{
double temperature[31] = {22.9, 19.7, 18.0, 23.3, 27.1, 18.7, 22.0, 28.1, 25.2, 21.3, 28.8, 27.5, 31.3, 19.2, 21.5, 30.5, 28.6, 19.5, 20.6, 31.3, 21.5, 29.7, 21.0, 20.2, 21.5, 31.9, 30.6, 22.0, 22.2, 20.0, 31.4};
cout << "***************************************************" << endl;
cout << "Welcome to the Monthly Temperature Analysis Program" << endl;
cout << "***************************************************" << endl;
int choice;
do
{
choice = choose_item();
apply_choice(choice, temperature);
} while(choice != 6);
system("pause");
return 0;
}
|