This program is about weather statistics.

Having trouble writing program for this This program is about weather statistics. c++

Write a program that uses a structure to store the following data for the purpose of a research in meteorology:

Year (2017, 2018, 2019, 2020)
First-Quarter Rain Amount
Second-Quarter Rain Amount
Third-Quarter Rain Amount
Fourth-Quarter Rain Amount
Total Annual Rain Amount
Average Quarterly Rain Amount
Requirements:

The program should make use of 4 variable of the structure. Each variable represents one of the years (2017, 2018, etc.)
The user should be asked to enter the four quarter's rain amount for each year.
Each year's total and average rain amount should be calculated and stored in the appropriate member of each structure variable.
The results should then be displayed on the screen.
Input validation: Negative numbers for the rain amount should not be accepted

Have not been able to run program not sure exactly what i need to fix or am missing. online learning is not helping right now so self teaching is a little frustrating. using a template to help adjust for my needed information to help guide me to what I correctly need to do. any help is appreciated


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
  Put// Weather Statistics
#include <iostream>
using namespace std;

struct WeatherData {
    int year = 2017;
    double rain;
    double totalAnnual;
    double averageQuarterly;

};

// Function Prototypes
void getYearData(WeatherData&);
double totalRain(WeatherData[], int);
double totalAnnual(WeatherData[], int);
double averageQuarterly(WeatherData[], int);



int main()
{
    //Constant for the number of months
    const int NUM_YEAR = 4;
    int year;

        



    //Get the weather data for each month.
    cout << "Enter the following information:\n";
    for (int index = 0; index < NUM_YEAR; index++)
    {
        //Get the rainfall.
        cout << "Quarter " << (index + 1) << endl;
        getYearData(year[index]);
    }

    //Display the total rainfall.
    cout << "\nTotal Rainfall: " << totalRain(year, NUM_YEAR) << endl;

    //Display the average monthly rain.
    cout << "Average Quarterly Rain: " << averageQuarterly (year, NUM_YEAR) << endl;

    

    return 0;
}

//The getMonthData function accents a WeatherData
//variable by reference. It prompts the user for
//weather data and stores the input in the argument.

void getYearData(WeatherData& data)
{
    //Get the total rainfall for the month.
    cout << "\tTotal Rainfall: ";
    cin >> data.rain;

    

    //Validate the high temperature.
    while (data.totalAnnual < -100 || data.totalAnnual > 140)
    {
        cout << "ERROR: Rainfall must be in the range of -100 throught 140.\n";
        cout << "\tRainfall: ";
        cin >> data.rain;
    }


    //Validate the low temperature.
    while (data.averageQuarterly < -100 || data.totalAnnual > 140)
    {
        cout << "ERROR: Rainfall must be in the range of -100 throught 140.\n";
        cout << "\taverageQuarterly: ";
        cin >> data.rain;
    }

    //Calculate the average temperature.
    data.totalAnnual = (data.rain + data.rain) / 2.0;

}

double totalRain(WeatherData data[], int size)
{
    double totalRain = 0; //Accumulator

    //Get the total of the rain members.
    for (int index = 0; index < size; index++)
        totalRain += data[index].rain;

    //Return the total.
    return totalRain;
}

double averageYearRainfall(WeatherData data[], int size)
{
    return totalRain(data, size) / size;
}

//The averageAverageTemp function accepts an array of 
//WeatherData structures and returns the average of all
//the monthly average temperatures.

double averageAverageQuaerterly(WeatherData data[], int size)
{
    //Calculate the average monthly monthly average teamperature
    double aveTotal = 0; //Accumulator for average temperatures
    double aveAve; //Average of the averages

    //Get the total of the average temperatures.
    for (int index = 1; index < size; index++)
        aveTotal += data[index].averageQuarterly;

    //Calculate the average of the average temperatures.
    aveAve = aveTotal / size;

    //Return the average of the averages.
    return aveAve;
}

//The highestTemp function accepts:
//(1) a WeatherData array
//(2) an int indicating the size of the array
//(3) an int by reference to hold the month with the 
//    highest temperature.
//The function returns the highest temperature and sets the
//month parameter to the number of the month with the highest temp.

double totalAnnual(WeatherData data[], int size, int& year)
{
    //Set the highest to the first month.
    double totalAnnual = data[0].totalAnnual;

    //Step through the array looking for the highest.
    for (int index = 1; index < size; index++)
    {
        if (data[index].totalAnnual > totalAnnual)
        {
            //Save this value. It is the highest so far.
            totalAnnual = data[index].totalAnnual;

            //Save this month's number. (1 = January, etc.)
            totalAnnual = index + 1;
        }
    }

    //Return the highest temperature.
    return totalAnnual;


}

double lowestTemp(WeatherData data[], int size, int& month)
{
    //Set the lowest to the first month.
    double averageQuarterly = data[0].averageQuarterly;

    //Step through the array looking for the lowest.
    for (int index = 1; index < size; index++)
    {
        if (data[index].averageQuarterly < averageQuarterly)
        {
            //Save this value. It is the lowest so far.
            averageQuarterly = data[index].averageQuarterly;

            //Save this month's number. (1 = January, etc.)
            month = index + 1;
        }
    }

    //Return the lowest temperature.
    return averageQuarterly (data, size) / size ;
} the code you need help with here.
Last edited on
Your code doesn't do what it's supposed to do.
You don't have to calc the lowest temp.
Each year's total and average rain amount should be calculated and stored in the appropriate member of each structure variable.

You only have to calc the total and average for each year.

Did you maybe copy the wrong code?
 
getYearData(year[index]);


year is not an array - just an int. The desc asks to input the four quarter's rain amount for each of the 4 years. Therefore the struct should have at least these fields as the requirement is to also the data as well as perform calculations.
Last edited on
Hello Evelynmkm27,

It may not seem important to you, but it does help if you mention what IDE/compiler and operating system you are using.

I am not sure what your original instructions look like, but this is easier to follow:

Write a program that uses a structure to store the following data for the purpose of a research in meteorology:

Year (2017, 2018, 2019, 2020)  // <--- Not necessary to be in the struct. Can easily be dealt with in a for loop.
First-Quarter Rain Amount      // <--- Required.
Second-Quarter Rain Amount     // <--- Required.
Third-Quarter Rain Amount      // <--- Required.
Fourth-Quarter Rain Amount     // <--- Required.
Total Annual Rain Amount       // <--- Required.
Average Quarterly Rain Amount  // <--- Required.


Requirements:

The program should make use of 4 variable of the structure. Each variable represents one of the years (2017, 2018, etc.)

The user should be asked to enter the four quarter's rain amount for each year.

Each year's total and average rain amount should be calculated and stored in the appropriate member of each structure variable.

The results should then be displayed on the screen.

Input validation: Negative numbers for the rain amount should not be accepted



I would start with:

Write a program that uses a structure to store the following data for the purpose of a research in meteorology:

Year (2017, 2018, 2019, 2020)  // <--- Not necessary to be in the struct. Can easily be dealt with in a for loop.
First-Quarter Rain Amount      // <--- Required.
Second-Quarter Rain Amount     // <--- Required.
Third-Quarter Rain Amount      // <--- Required.
Fourth-Quarter Rain Amount     // <--- Required.
Total Annual Rain Amount       // <--- Required.
Average Quarterly Rain Amount  // <--- Required.


Requirements:

The program should make use of 4 variable of the structure. Each variable represents one of the years (2017, 2018, etc.)


Once you have the "struct" correct you can go from there. And do initialize your variables in the "struct" it is so easy, e.g., double totalAnnual{};, the empty {}s will allow the compiler choose what type of (0)zero is needed for the variable. If you need something other than (0)zero you can put this value between the {}s.

When the "struct" is correct the rest of your code tends to suggest that you will need to define an array in "main" to be passed to the functions.

You are not quite there yet, so we can deal with passing the array to the functions by reference later.

Next I would work on:

The user should be asked to enter the four quarter's rain amount for each year.


By the time you can get this part fixed I will have had some time to go over the getYearData function.

Here is a tip: When you use a prototype all that is required is the variable type between the (). Also the correct order must match tha function definition along with the correct variable name and order in the function call. I find it much easier to just copy the function definition and use it for the prototype. Having the variable names in the prototype does not matter, but can be useful when writing your code.

It is always best to work in small pieces and not try to fix the whole program at once. It is also good advice when first writing a program. Work small, compile and test often. Fix any errors as they come up.

Trying to write and fix the whole program at once is just wasting your time. Because at some point a change in 1 part may require a total rewrite of a function because the original may no longer apply.

Andy
ok thank you so much this is helpful!
updated fixes it runs now problem is how to get seperate lines and enter an amount for each and average quarterly and total annual amount tried to use cin to do this but gives me error (c++)



//Weather statistics
#include <iostream>
#include <iomanip>
using namespace std;
// Declaration of WeatherStatistics
struct WeatherStatistics
{
const int NUM_YEAR = (2017, 2018, 2019, 2020);
double First_Quarter_Rain_Amount;
double Second_Quarter_Rain_Amount;
double Third_Quarter_Rain_Amount;
double Fourth_Quarter_Rain_Amount;
double Total_Annual_Rain_Amount;
double Average_Quarterly_Rain_Amount;
};
// Function Prototypes
void getYearData(WeatherStatistics&);

double First_Quarter_Rain_Amount(WeatherStatistics[], int);
double Second_Quarter_Rain_Amount(WeatherStatistics[], int);
double Third_Quarter_Rain_Amount(WeatherStatistics[], int);
double Fourth_Quarter_Rain_Amount(WeatherStatistics[], int);
double Total_Annual_Rain_Amount(WeatherStatistics[], int, int &);
double Average_Quarterly_Rain_Amount(WeatherStatistics[], int, int&);

int main()
{
//constant for year
const int NUM_YEAR = (2017, 2018, 2019, 2020);

string Year_2017, Year_2018, Year_2019, Year_2020;

float
First_Quarter_Rain_Amount_Year,
Second_Quarter_Rain_Amount_Year,
Third_Quarter_Rain_Amount_Year,
Fourth_Quarter_Rain_Amount_Year,
Total_Annual_Rain_Amount_Year,
Average_Quarterly_Rain_Amount_year,
total,
average;

//ask user to enter rain amounts

cout << endl;
cout << "\nThis program calculates the average";
cout << " rainfall for each quarter and year." << endl;
cout << "\n\nPress Enter to continue." << endl;
cin.get();



// information
cout << "Enter the year for Weather statistics : ";
cin >> Year_2017, Year_2018, Year_2019, Year_2020;
cout << "Enter first quarter rain amount ";
cout<< ":";

cout << "Enter second quarter rain amount";
cout << ": ";
cout << " Enter third quarter rain amount";
cout << ": ";
cout << "Enter fourth quarter rain amount";
cout << ": ";
cin >> Total_Annual_Rain_Amount_Year;



//calculate total and average
total= Total_Annual_Rain_Amount_Year;
average = Total_Annual_Rain_Amount_Year,
First_Quarter_Rain_Amount_Year,
Second_Quarter_Rain_Amount_Year,
Third_Quarter_Rain_Amount_Year,
Fourth_Quarter_Rain_Amount_Year;
cout<< endl;
return 0;





}
Hello Evelynmkm27,

Going over your revision it looks like you are going backwards.

Your struct:
1
2
3
4
5
6
7
8
9
10
struct WeatherStatistics
{
    const int NUM_YEAR = (2017, 2018, 2019, 2020);
    double First_Quarter_Rain_Amount;
    double Second_Quarter_Rain_Amount;
    double Third_Quarter_Rain_Amount;
    double Fourth_Quarter_Rain_Amount;
    double Total_Annual_Rain_Amount;
    double Average_Quarterly_Rain_Amount;
};

Line 3 is not required and the way you have written it will only store "2020" in the variable. The way that you initialized the variable one might think that you are trying to initialize an array that is not defined as an array. The comma here does not do anything that you may be thinking except to store the last number in the variable. After that I do not understand how you intend to use this.

The rest of the lines are the variables required for the "struct".

Your 1st prototype is good although I did add a 2nd parameter in my version.

Your next 4 prototypes look like they will be the same function with only a slight difference inside the function.

In "main" the 1st line of code duplicates what you defined in the "struct". This may not be a problem of defining a variable with the same name, but you may have a problem later on. One of the definitions may never be used.

The next part you do not have is an actual object of the "struct" defined in "main" to use. Because you have a "struct" for each of 4 different years an array of "structs" seems the most logical choice here.

When I wrote the code for the "getYearData" function the function does 1 thing only. Well 1 thing in 2 parts. First a while loop get the input from the keyboard and then checks if the input is in the proper range. When you get that far I will go over it with you.

Next I used a switch to determine which variable in the struct needs the input. This way the nested for loop in "main", (hint, hint, nudge, nudge), that calls this function only has to deal with 1 piece of data at a time. A better choice would be to create an array in the struct with a size of 4, 1 element for each quarter, but this does not go along with the directions.

In your second code:
1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Enter first quarter rain amount ";
cout << ":";

cout << "Enter second quarter rain amount";
cout << ": ";

cout << " Enter third quarter rain amount";
cout << ": ";

cout << "Enter fourth quarter rain amount";
cout << ": ";

cin >> Total_Annual_Rain_Amount_Year;

You have 8 lines of code that prompt for input, but you never enter anything. The "cin" statement is asking for the user to do the math when the directions call for a function to do this calculation. It does not make any sense and you never store the input into the "struct", so what do you expect the "struct to do?

Your 2 "cout" statements can be combined into just 1 "cout" as:
cout << "Enter first quarter rain amount: ";. You do not need a separate "cout" just for the (:) and if you notice after the (:) here there is a space and no (\n) or "endl". This put the "cin" on the same line as the prompt and looks much nicer on the screen.

With what I have done so far my input looks like this:

 Enter the following information:

  Enter data for year 1:

   Enter rain fall for quarter 1: 1.1
   Enter rain fall for quarter 2: 2.2
   Enter rain fall for quarter 3: 3.3
   Enter rain fall for quarter 4: 4.4

  Enter data for year 2:

   Enter rain fall for quarter 1:


If you want to get fancy you could define an array of "std::string"s and have the prompt say something like: Enter rain fall for 1st quarter: or even spell out "1st" if you want. Not necessary, but a thought.

At the end of "main" under the comment "calculate total and average" everything is wrong. First off all that meaningless code should be 2 function calls. And what is there does not work.

Andy
Topic archived. No new replies allowed.