Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please revise this program as following: When user input high temperature and lo

ID: 3573665 • Letter: P

Question

Please revise this program as following: When user input high temperature and low temperature for each month, the high temperature should be higher or equal to low temperature, if not, the program will display “ERROR: high temperature should be higher or equal to the low temperature” and asks user to input high temperature and low temperature again, if the new high temperature is still lower than the low temperature, the program will display “ERROR: high temperature should be higher or equal to the low temperature” and asks user to input high temperature and low temperature again, until the current high temperature is higher or equal to the low temperature, the program will continue to execute the next statement. Note: the temperature should be in the range [-100, 140] Here is the original program:

// Weather Statistics #include using namespace std; // Constant for the number of months const int NUM_MONTHS = 12; // Declaration of the WeatherInfo structure struct WeatherInfo { double rain; // Total rainfall double high; // High temperature double low; // Low temperature double averageTemp; // Average temperature }; int main() { // Create an array of WeatherInfo structures WeatherInfo year[NUM_MONTHS]; int index = 0; // Loop counter // Get the weather data for each month. cout << "Enter the following information: "; for (index = 0; index < NUM_MONTHS; index++) { // Get the rainfall. cout << "Month " << (index + 1) << endl; cout << " Total Rainfall: "; cin >> year[index].rain; // Get the high temperature. cout << " High Temperature: "; cin >> year[index].high; // Validate the high temperature. while (year[index].high < -100 || year[index].high > 140) { cout << "ERROR: Temperature must be in the range " << "of -100 through 140. "; cout << " High Temperature: "; cin >> year[index].high; } // Get the low temperature. cout << " Low Temperature: "; cin >> year[index].low; // Validate the high temperature. while (year[index].low < -100 || year[index].low > 140) { cout << "ERROR: Temperature must be in the range " << "of -100 through 140. "; cout << " Low Temperature: "; cin >> year[index].low; } // Calculate the average temperature. year[index].averageTemp = (year[index].high + year[index].low) / 2.0; } // Calculate total annual rainfall double totalRain = 0; for (index = 0; index < NUM_MONTHS; index++) totalRain += year[index].rain; // Calculate average monthly rainfall double aveMonthRain = totalRain / NUM_MONTHS; // Calculate the average monthly average temperature double aveTotal = 0, aveAve; for (index = 1; index < NUM_MONTHS; index++) aveTotal += year[index].averageTemp; aveAve = aveTotal / NUM_MONTHS; // Find the highest & lowest temperatures double highest, lowest, highMonth = 0, lowMonth = 0; highest = year[0].high; lowest = year[0].low; for (index = 1; index < NUM_MONTHS; index++) { if (year[index].high > highest) { highest = year[index].high; highMonth = index; } if (year[index].low < lowest) { lowest = year[index].low; lowMonth = index; } } // Display findings. cout << " Total Rainfall: " << totalRain << endl; cout << "Average Monthly Rain: " << aveMonthRain << endl; cout << "Average Monthly Average Temperature: " << aveAve << endl; cout << "Highest Temperature: " << highest; cout << " (Month " << (highMonth + 1) << ") "; cout << "Lowest Temperature: " << lowest; cout << " (Month " << (lowMonth + 1) << ") "; return 0; }

Explanation / Answer

// Weather Statistics
#include <iostream>
using namespace std;
// Constant for the number of months const
int NUM_MONTHS = 12;
// Declaration of the WeatherInfo structure
struct WeatherInfo {
   double rain;
   // Total rainfall
   double high;
   // High temperature
   double low;
   // Low temperature
   double averageTemp;
   // Average temperature
};

int main() {
   // Create an array of WeatherInfo structures
   WeatherInfo year[NUM_MONTHS];
   int index = 0;
   // Loop counter
   // Get the weather data for each month.
   cout << "Enter the following information: ";
   for (index = 0; index < NUM_MONTHS; index++) {
       // Get the rainfall.
       cout << "Month " << (index + 1) << endl;
       cout << " Total Rainfall: ";
       cin >> year[index].rain;

       while(true){
           // Get the high temperature.
           cout << " High Temperature: ";
           cin >> year[index].high;
           // Validate the high temperature.
           while (year[index].high < -100 || year[index].high > 140) {
               cout << "ERROR: Temperature must be in the range " << "of -100 through 140. ";
               cout << " High Temperature: ";
               cin >> year[index].high;
           }
           // Get the low temperature.
           cout << " Low Temperature: ";
           cin >> year[index].low;
           // Validate the high temperature.
           while (year[index].low < -100 || year[index].low > 140) {
               cout << "ERROR: Temperature must be in the range " << "of -100 through 140. ";
               cout << " Low Temperature: ";
               cin >> year[index].low;
           }

           if(year[index].low > year[index].high) cout << "ERROR: high temperature should be higher or equal to the low temperature ";
           else break;
       }
       // Calculate the average temperature.
       year[index].averageTemp = (year[index].high + year[index].low) / 2.0;
   }
   // Calculate total annual rainfall
   double totalRain = 0;
   for (index = 0; index < NUM_MONTHS; index++)
       totalRain += year[index].rain;
   // Calculate average monthly rainfall
   double aveMonthRain = totalRain / NUM_MONTHS;
   // Calculate the average monthly average temperature
   double aveTotal = 0, aveAve;
   for (index = 1; index < NUM_MONTHS; index++) aveTotal += year[index].averageTemp;
   aveAve = aveTotal / NUM_MONTHS;
   // Find the highest & lowest temperatures
   double highest, lowest, highMonth = 0, lowMonth = 0;
   highest = year[0].high;
   lowest = year[0].low;
   for (index = 1; index < NUM_MONTHS; index++) {
       if (year[index].high > highest) {
           highest = year[index].high;
           highMonth = index;
       }
       if (year[index].low < lowest) {
           lowest = year[index].low; lowMonth = index;
       }
   }
   // Display findings.
   cout << " Total Rainfall: " << totalRain << endl;
   cout << "Average Monthly Rain: " << aveMonthRain << endl;
   cout << "Average Monthly Average Temperature: " << aveAve << endl;
   cout << "Highest Temperature: " << highest;
   cout << " (Month " << (highMonth + 1) << ") ";
   cout << "Lowest Temperature: " << lowest;
   cout << " (Month " << (lowMonth + 1) << ") ";
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote