Question

In: Computer Science

C++ programming Program 1: Write a program that uses a structure to store the following weather...

C++ programming

Program 1:

Write a program that uses a structure to store the following weather data for a particular month:

Total Rainfall

High Temperature

Low Temperature

Average Temperature

The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month (the avg temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average monthly rainfall, the total rainfall for the year, the highest and lowest temperatures for the year (and the months they occurred in), and the average of all the monthly average temperatures.

Input validation: only accept temperatures within the range between -100 and +140 degrees Fahrenheit.

Program 2:

Modify your program so it defines an enumerated data type with enumerators for the months (JANUARY, FEBRUARY, etc.). The program should use the enumerated type to step through the elements of the array.

User interface is important!

You should post pseudocode

Your code should have appropriate comments

Your code should be well-formatted, with camel case variables

Variables should be self-descriptive

Your code should have functions (besides main!)

Your functions should only do one thing

Solutions

Expert Solution

// C++ code

#include <iostream>
using namespace std;

char monthWeatherArray[12][10] = { "January", "February", "March", "April", "May","June", "July", "August", "September", "October","November", "December" };

struct Weather
{
   double totalRainfall;
   double highTemperature;
   double lowTemperature;  
   double averageTemperature;  
};

double weatherArrayAverage(Weather weatherArray [], char c[])
{
   double average = 0;  


   if(c == "temp")
   {
  
       for(int i = 0; i < 12; i++)
       {
           average = average + weatherArray[i].averageTemperature;
       }
  
       return (average / 12);
   }


   if(c == "rain")
   {
  
       for(int i = 0; i < 12; i++)
       {
           average = average + weatherArray[i].totalRainfall;
       }
  
       return (average / 12);
   }
}

void getUserData(Weather weatherArray[])
{
   for(int i = 0; i < 12; i++)
   {
  
       cout << "From " << monthWeatherArray[i] << ": \n\n";
       cout << "Total rainfall: ";
       cin >> weatherArray[i].totalRainfall;

  
       while(weatherArray[i].totalRainfall < 0)
       {
           cout << "\nInvaid Input\n\n";
           cout << "Total rainfall: ";
           cin >> weatherArray[i].totalRainfall;
       }

       cout << "Highest temperature: ";
       cin >> weatherArray[i].highTemperature;
  
       while((weatherArray[i].highTemperature < -100) or (weatherArray[i].highTemperature > 140))
       {
           cout << "\nInvalid Input\n";
           cout << "Highest temperature: ";
           cin >> weatherArray[i].highTemperature;
       }

       cout << "Lowest temperature: ";
       cin >> weatherArray[i].lowTemperature;
  
       while((weatherArray[i].lowTemperature < -100) or (weatherArray[i].lowTemperature > 140) or (weatherArray[i].lowTemperature > weatherArray[i].highTemperature))
       {
           cout << "\nInvaid Input\n\n";
           cout << "Lowest temperature: ";
           cin >> weatherArray[i].lowTemperature;
       }

       weatherArray[i].averageTemperature = (weatherArray[i].highTemperature + weatherArray[i].lowTemperature)/2;

       cout << endl;
   }
}

int main()
{

   Weather weatherArray[12];

   getUserData(weatherArray);  

   int maximumposition = 0;
   int minimumposition = 0;  

   double minimum = weatherArray[0].lowTemperature;
   double maximum = weatherArray[0].highTemperature;  

   for(int i = 1; i < 12; i++)
   {
  
       if(minimum > weatherArray[i].lowTemperature)
       {
           minimum = weatherArray[i].lowTemperature;
           minimumposition = i;
       }
      
  
       if(maximum < weatherArray[i].highTemperature)
       {
           maximum = weatherArray[i].highTemperature;
           maximumposition = i;
       }
   }


   cout << "Average rainfall: " << weatherArrayAverage(weatherArray, "rain") << endl;
   cout << "Average temperature: " << weatherArrayAverage(weatherArray, "temp") << endl;
   cout << "Highest temperature: " << weatherArray[maximumposition].highTemperature << " (" << monthWeatherArray[maximumposition] << ")" << endl;
   cout << "Lowest temperature: " << weatherArray[minimumposition].lowTemperature << " (" << monthWeatherArray[minimumposition] << ")" << endl;


   return 0;
}

/*
output:

From January:

Total rainfall: 30
Highest temperature: 42
Lowest temperature: 10

From February:

Total rainfall: 34
Highest temperature: 32
Lowest temperature: 10

From March:

Total rainfall: 67
Highest temperature: 44
Lowest temperature: 32

From April:

Total rainfall: 56
Highest temperature: 32
Lowest temperature: 11

From May:

Total rainfall: 67
Highest temperature: 98
Lowest temperature: 65

From June:

Total rainfall: 54
Highest temperature: 33
Lowest temperature: 21

From July:

Total rainfall: 45
Highest temperature: 44
Lowest temperature: 32

From August:

Total rainfall: 76
Highest temperature: 43
Lowest temperature: 22

From September:

Total rainfall: 87
Highest temperature: 66
Lowest temperature: 54

From October:

Total rainfall: 76
Highest temperature: 44
Lowest temperature: 33

From November:

Total rainfall: 34
Highest temperature: 22
Lowest temperature: 11

From December:

Total rainfall: 44
Highest temperature: 33
Lowest temperature: 21

Average rainfall: 55.8333
Average temperature: 35.625
Highest temperature: 98 (May)
Lowest temperature: 10 (January)

*/


Related Solutions

Write a program that uses a structure to store the following weather data for a particular...
Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature. The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average...
C++ 11.7: Customer Accounts Write a program that uses a structure to store the following data...
C++ 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account: Customer name Customer address City State ZIP code Telephone Account balance Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Prompts...
Write a program that uses a structure to store the following data on a company division...
Write a program that uses a structure to store the following data on a company division in c++ Division Name (such as East, West, North, or South) First-Quarter Sales Second-Quarter Sales Third-Quarter Sales Fourth-Quarter Sales Total Annual Sales Average Quarterly Sales The program should use four variables of this structure. Each variable should represent one of the following corporate divisions: East, West, North, and South. The user should be asked for the four quarters’ sales figures for each division. Each...
Write a program that uses a structure to store the following inventory information in a Binary...
Write a program that uses a structure to store the following inventory information in a Binary file and access it using Random Access Method: Item description Quantity on hand Wholesale cost Retail cost Date added to inventory The program should have a menu that allows the user to perform the following tasks: Add new records to the file Display any record in the file Change any record in the file For exact menu option text please see the test cases...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to...
Programming assignment 4 : C++ Write a program to do the following: 1.Define a structure to store a date, which includes day(int), month(int), and year(int). 2.Define a structure to store an address, which includes address(house number and street)(string), city(string), state(string), zip code (string). 3.Define a class to store the following information about a student. It should include private member variables: name(string), ID (int), date of birth (the first structure), address (the second structure), total credit earned (int), and GPA (double)....
Write a program that uses a structure to store the following data: (Remember to use proper...
Write a program that uses a structure to store the following data: (Remember to use proper formatting and code documentation) Member Name Description name student name idnum Student ID number Scores [NUM_TESTS] an array of test scores Average Average test score Grade Course grade Declare a global const directly above the struct declaration const int NUM_TESTS = 4; //a global constant The program should ask the user how many students there are and should then dynamically allocate an array of...
Write a program that uses a structure to store the following data about a customer account:...
Write a program that uses a structure to store the following data about a customer account: Name Address City, State and Zip Telephone number Account balance Date of last payment The program should use an array of at least 5 structures. It should let the user enter data into the array, change the contents of any element and display all the data stored in the array. The program should have a menu-driven interface and use functions as appropriate. Input validation:...
11.7: Customer Accounts Write a program that uses a structure to store the following data about...
11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Prompts And...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT