Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following program is supposed to store the highest and lowest temperature of each month and days requested by the user...But I don't know how to add daily record. if any one can modify my code using 3d array I will be thankful

What I have tried:

C++
// A c++ program that records the high and low temperatures for each day of all months
#include <iostream>
#include <string>

using namespace std;

//declare struct WeatherStats
/*struct is an user-defined data type available in C++.
It allows a user to combine data items of (possibly) different data types under a single name.*/
struct Weather{
    
    int highTemp;
    int lowTemp;
    double avgTemp;
};

//function Declaration
void Data(Weather&);
void Highest(const Weather[], const int);
void Lowest(const Weather[], const int);
double YearlyAvgTemp(const Weather[], const int);

// constant declaration
const int NUM_MONTHS = 12;

//a string array to get month names
string monthNames[NUM_MONTHS] = {"September", "October",
                                 "November", "December", "January", "February", "March",
                                 "April", "May", "June", "July",
                                 "August" };

int main()
{
    //define array of 12 WeatherStats structure variables
    Weather data[NUM_MONTHS];

    //get data for all months
    for(int i = 0; i < NUM_MONTHS; i++){
        cout << "Enter data for " << monthNames[i];
        cout << endl;
        Data(data[i]);
        cout << endl << endl;
    }

    //print the output

    Highest(data, NUM_MONTHS);
    Lowest(data, NUM_MONTHS);
    cout << "Yearly average temperature: ";
    cout << YearlyAvgTemp(data, NUM_MONTHS);


    return 0;
}

//function to read data into structure variables
//for a month
void Data(Weather& w){


    cout << "Enter high temperature: ";
    cin >> w.highTemp;

    //validate input using while loop
    while(w.highTemp < -100 || w.highTemp > 140){
        cout << "Enter a temperature within the range";
        cout << "[-100 to +140]: ";
        cin >> w.highTemp;
    }

    cout << "Enter low temperature: ";
    cin >> w.lowTemp;

    //validate input using while loop
    while(w.lowTemp < -100 || w.lowTemp > 140){
        cout << "Enter a temperature within the range";
        cout << "[-100 to +140]: ";
        cin >> w.lowTemp;
    }

    //calculate monthly average
    w.avgTemp = (w.lowTemp + w.highTemp) / 2.0;
}


//function to get yearly highest temperature
//and the month in which it occurred
void Highest(const Weather w[], const int numElements){
    //variables to aid the search
    int highIndex = 0, high = w[highIndex].highTemp;

    for(int i = 0; i < numElements; i++){
        if(w[i].highTemp > high){
            high = w[i].highTemp;
            highIndex = i;
        }
    }

    cout << "\nThe yearly highest temperature was " << high;
    cout << ", and it occurred in " << monthNames[highIndex] << endl;
}

//function to get yearly lowest temperature
//and the month in which it occurred
void Lowest(const Weather w[], const int numElements){
    //variables to aid the search
    int lowIndex = 0, low = w[lowIndex].lowTemp;

    for(int i = 0; i < numElements; i++){
        if(w[i].lowTemp < low){
            low = w[i].lowTemp;
            lowIndex = i;
        }
    }

    cout << "\nThe yearly lowest temperature was " << low;
    cout << ", and it occurred in " << monthNames[lowIndex] << endl;
}

double YearlyAvgTemp(const Weather w[], const int numElements){
    double total = 0, avg;

    for(int i = 0; i < numElements; i++)
        total += w[i].avgTemp;

    avg = (double)total/numElements;

    return avg;
}
Posted
Updated 27-Jun-21 12:50pm
v2

First, you need to decide how you are going to store it: there are many, many ways such as a DataBase, through a spreadsheet, config file, XML file, JSON data, CSV, right down to a flat text file.
And what you decide there will affect the code you have to write.
When you have decided that, you can work on loading data from it when your app starts, and adding items to it when you read a temperature.

But we can't do that for you - we have no idea what you know about! So start with your course notes and work out what you do know (or should know) about storing values between runs of your application. Then take it from there.
 
Share this answer
 
To save the weather data with month and day, it would be the easiest to expand the structure.
C++
typedef struct {
	int highTemp;
	int lowTemp;
	double avgTemp;
	// int day;
	int month;
	// int year;
} WeatherStats;

In order to use the advantages of C ++ and to improve the handling, it would also be advantageous to manage the weather data with the methods together in one object.
C++
class Weather {
public:
	Weather() {};		// constructor
	//function Declaration
	void SetData(WeatherStats& w);
	void GetData(int i, WeatherStats& w);
	int Highest();
	int Lowest();		
	double YearlyAvgTemp();
private:
	vector <WeatherStats> m_w;
};

//function to set data for a month (or day)
void Weather::SetData(WeatherStats& w) 
{
	//validate input here ?

	//calculate monthly average
	w.avgTemp = (double)(w.lowTemp + w.highTemp) / 2;
	m_w.push_back(w);
}

void Weather::GetData(int i, WeatherStats& w)
{
	//validate input here ?

	// get data for index
	w = m_w.at(i);
}

//function to get highest temperature
int Weather::Highest() {
	//variables to aid the search
	int highIndex = 0, high = m_w.at(highIndex).highTemp;

	for (size_t i = 0; i < m_w.size(); i++) {
		if (m_w.at(i).highTemp > high) {
			high = m_w.at(i).highTemp;
			highIndex = i;
		}
	}
	return highIndex;
}

You could call it that way
C++
int main()
{
	//define class with array of x WeatherStats
	Weather weather;
	WeatherStats w;

	//get data for all months
	for (int i = 0; i < NUM_MONTHS; i++) {
		cout << "Enter data for " << monthNames[i] << endl;
		w.month = i;
		do {
			//validate input using while loop
			cout << "Enter high temperature within the range ";
			cout << "[-100 to +140]: ";
			cin >> w.highTemp;
		} while (w.highTemp < -100 || w.highTemp > 140);

		do {
			cout << "Enter low temperature within the range";
			cout << "[-100 to +140]: ";
			cin >> w.lowTemp;
		} while (w.lowTemp < -100 || w.lowTemp > 140);

		weather.SetData(w);
		cout << endl << endl;
	}

	//print the output

	int hi = weather.Highest();
	weather.GetData(hi, w);
	int highTemp = w.highTemp;

	cout << "\nThe yearly highest temperature was " << highTemp;
	cout << ", and it occurred in " << monthNames[hi] << endl;

	int lo = weather.Lowest();
	weather.GetData(lo, w);
	int  lowTemp = w.lowTemp;

	cout << "\nThe yearly lowest temperature was " << lowTemp;
	cout << ", and it occurred in " << monthNames[lo] << endl;

	cout << "Yearly average temperature: ";
	cout << weather.YearlyAvgTemp() << endl;

	return 0;
}

As Original Griff has already indicated, with so many data records it should be possible to save and load them.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900