Show_Your_Skills

closed account (L8bqko23)
The average monthly temperatures for years 2010, 2011, 2012 and 2013 are shown in the table below.
You have been asked to write a program that will store these average temperature values for the months of
January to December for the year 2010, 2011, 2012 and 2013 into a two dimensional array. This information
will then be used to find the highest, lowest and the average temperatures.

JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
2010 -16.1 -13.5 15.9 28.1 30.2 32.7 35.8 30.5 25.3 18.5 5.0 -1.5
2011 -15.1 -13.9 17.9 25.5 28.5 31.5 34.8 28.9 26.4 15.8 2.8 -2.8
2012 -21.1 -15.9 20.9 23.4 29.4 30.8 29.6 24.6 27.8 19.8 3.4 0
2013 -22.1 -13.9 19.9 21.6 31.2 29.5 33.2 35.1 24.6 14.3 5.7 -8

Instructions:
Create a 4-by-12-two-dimensional array to store double precision temperature values for each month of the
year for years 2010, 2011, 2012 and 2013. The values entered must then be used to calculate the high and low
and average temperatures for each year.
Your program should then allow the user to request for any of the following information:
• temperature for a month for a given year: e.g. temperature for the month of June for the year 2012 = 30.8
• High and low temperatures for any year: e.g. for the year 2011- the highest and lowest temperatures are -
34.8 and -15.1 respectively
• The average temperature for any year: e.g. the average temperature for year 2013 is 14.2
• The average monthly temperature: e.g. the average monthly temperature for the month of March is 18.65

Note:
• Validate all data entry.
• All numerical output must be formatted to display to two decimal places.
• Have appropriate commenting in your program and practice good programming skills. Your program
should be well documented and user friendly. The output format for the program can be of your own.
sure i'll do it!

...wait a minute.....
closed account (L8bqko23)
lets see your skills
We do not participate in activities on this site that would result in the expulsion of university students.
closed account (L8bqko23)
iam not a university student
i post this question so that we can atleast learn something about c++ programming especially on arrays and functions.
it will be big helpful to beginners
#share your skills and gain more...
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <vector>
#include <limits>

/* 
Program to:
  Temperature of a month for a given year.
  High and low temperatures for a given year.
  The average temperature for a given year.
  The average temperature of a month for all years.
*/

#define MONS_IN_YR 12

// Get a valid month (1-Jan,... 12-Dec)
unsigned int get_a_valid_month()
{
  unsigned int month;
  // Keep looping till a valid month is given.
  do
  {
    std::cout << std::endl << "Enter month (1-12): ";
    std::cin >> month;
    if(month>=1 && month <=MONS_IN_YR) 
      break;    
    else
    {
      std::cout << "Invalid month. Try again.";
      exit(0);
    }
  }while(1);
  return month;
}

// Get a valid month (Valid range 2010-2013)
unsigned int get_a_valid_year()
{
  unsigned int year;
  // Keep looping till a valid year is given.
  do
  {
    std::cout << std::endl << "Enter year (2010-2013): ";
    std::cin >> year;
    if(year>=2010 && year <=2013)
      break;    
    else
    {
      std::cout << "Invalid year. Try again.";
      exit(0);
    }
  }while(1);
  return year;
}

// Class to contain/implement: Year and its Temperatures
class year_temp_arr
{
  private:
    // Two dimensional arear using vector.
    std::vector< std::vector<double> > two_d_arr;
  public:
    // Default constructor. Assume no of years as 1. Default 2d array of size 1 X 12. Initialized with zero.
    year_temp_arr():two_d_arr(1, std::vector<double>(MONS_IN_YR,0) ) {}
    // Constructor with dimensions (no of years). Initialized with zero.
    year_temp_arr(int row):two_d_arr(row, std::vector<double>(MONS_IN_YR,0) ) {}
    // Constructor with dimensions (no of years). Initialized with the intializer provided.
    year_temp_arr(int row, const double *vals);    
    // Print the values in two dimensional array.
    void print_yr_n_tmp();
    // Print the temperature of month, year.
    void print_temp_of_a_month();
    // Get the minimum and maximum temperature in year.
    void get_min_max_temp(int year, double &min, double &max);
    // Print the minimum and maximum temperature in year.
    void print_hi_n_low_temp_of_a_month();
    // Print the average temperature in year.
    void print_avg_temp_of_a_year();
    // Compute average temperature in year.
    double get_avg_temp_of_a_year(int year);
    // Print the average temperature in month.
    void print_avg_temp_of_a_month();
};

year_temp_arr::year_temp_arr(int row, const double *vals):two_d_arr(row, std::vector<double>(MONS_IN_YR,0) )
{
  // Fill the two_d_arr with the initializer values in vals.
  for(int ri=0; ri<row; ++ri)
    for(int ci=0; ci<MONS_IN_YR; ++ci)
      two_d_arr[ri][ci]=vals[(ri*MONS_IN_YR)+ci];
}

// Print the temperature of all the years.
void year_temp_arr::print_yr_n_tmp()
{    
  std::cout << std::endl;
  for (std::vector< std::vector<double> >::iterator i = two_d_arr.begin(); i != two_d_arr.end(); ++i)
  {
    for(int mon_i=0; mon_i <MONS_IN_YR; ++mon_i)
      std::cout << "\t" << (*i)[mon_i];
    std::cout << std::endl;
  }
}

// Print the temperature of month, year (got from user as input).
void year_temp_arr::print_temp_of_a_month()
{
  // Get the month.
  unsigned int month = get_a_valid_month();
  unsigned year = get_a_valid_year();
  // Print the temperature of month, year.
  std::cout << std::endl << "Temperature of month " << month << " of year " 
            << year << " is: " << two_d_arr[year-2010][month-1] << std::endl;
  return;
}

// Find the minimum and maximum temperature of a year.
void year_temp_arr::get_min_max_temp(int year, double &min, double &max)
{
  double temp;
  min=0.0;
  max=0.0;  
  // Find the minimum and maximum temperature in year.
  for(int mon_i=0; mon_i <MONS_IN_YR; ++mon_i)
  {
    temp = two_d_arr[year-2010][mon_i];
    if(min > temp) min = temp;
    if(max < temp) max = temp;
  }
  return;
}

// Find the average temperature of a year.
double year_temp_arr::get_avg_temp_of_a_year(int year)
{
  double sum=0, avg=0;
  // Find the average temperature in year.
  for(int mon_i=0; mon_i <MONS_IN_YR; ++mon_i)  
    sum += two_d_arr[year-2010][mon_i];
  avg = sum/MONS_IN_YR;
  return avg;
}

// Print the minimum and maximum temperature in year.
void year_temp_arr::print_hi_n_low_temp_of_a_month()
{
  double min, max;
  // Get the year from user.
  unsigned int year = get_a_valid_year();
  // Compute the minimum and maximum temperature in year.
  get_min_max_temp(year, min, max);
  // Print the minimum and maximum temperature in year.
  std::cout << std::endl << "High and low temperatures of year " << year 
            << " are " << min << " " << max << std::endl;
  return;
}

// Print the average temperature in year.
void year_temp_arr::print_avg_temp_of_a_year()
{
  double avg;
  // Get the year from user.
  unsigned int year = get_a_valid_year();
  // Compute the average temperature in year.
  avg = get_avg_temp_of_a_year(year);
  // Print the average temperature in year.
  std::cout << std::endl << "The average temperature for year " << year 
            << " is " << avg  << std::endl;
  return;
}

// Print the average temperature of a month (of several years).
void year_temp_arr::print_avg_temp_of_a_month()
{
  double avg, sum=0;
  // Get the month from user.
  unsigned int month = get_a_valid_month();
  // Compute the average temperature of a month (of several years).
  for (std::vector< std::vector<double> >::iterator i = two_d_arr.begin(); i != two_d_arr.end(); ++i)  
    sum += (*i)[month-1];
  avg = sum/two_d_arr.size();
  // Print the average temperature of a month (of several years).
  std::cout << std::endl << "The average temperature of month " << month 
            << " is " << avg  << std::endl;
  return;
}

int main()
{
  // Monthwise temperature of year 2010, 2011, 2012, 2013
  const double temps[]=
  {
    -16.1, -13.5, 15.9, 28.1, 30.2, 32.7, 35.8, 30.5, 25.3, 18.5, 5.0, -1.5, // 2010
    -15.1, -13.9, 17.9, 25.5, 28.5, 31.5, 34.8, 28.9, 26.4, 15.8, 2.8, -2.8, // 2011
    -21.1, -15.9, 20.9, 23.4, 29.4, 30.8, 29.6, 24.6, 27.8, 19.8, 3.4,  0.0, // 2012
    -22.1, -13.9, 19.9, 21.6, 31.2, 29.5, 33.2, 35.1, 24.6, 14.3, 5.7, -8.0  // 2013
  };
  
  year_temp_arr yr_n_tmp(4,temps);

  do
  {
    int option;
    std::cout << std::endl
              << "1. Temperature of a month for a given year." << std::endl
              << "2. High and low temperatures for a given year." << std::endl
              << "3. The average temperature for a given year." << std::endl
              << "4. The average temperature of a month for all years." << std::endl
              << "5. Exit program." << std::endl
              << "Enter your option(1-4): ";
    std::cin >> option;

    switch(option)
    {
      case 1:
        yr_n_tmp.print_temp_of_a_month();
        break;
      case 2:
        yr_n_tmp.print_hi_n_low_temp_of_a_month();
        break;
      case 3:
        yr_n_tmp.print_avg_temp_of_a_year();
        break;
      case 4:
        yr_n_tmp.print_avg_temp_of_a_month();
        break;
      case 5:
        exit(0);
      default:        
        std::cout << std::endl << "Invalid option. Try again." << std::endl;
        exit(0);
    };   
  }while(1);
  
  return 0;
}

With C++11 "array container" to hold temperature of 12 months of a year:

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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <iostream>
#include <vector>
#include <array>
#include <limits>

/* 
Program to:
  Temperature of a month for a given year.
  High and low temperatures for a given year.
  The average temperature for a given year.
  The average temperature of a month for all years.
*/

#define MONS_IN_YR 12

// Get a valid month (1-Jan,... 12-Dec)
unsigned int get_a_valid_month()
{
  unsigned int month;
  // Keep looping till a valid month is given.
  do
  {
    std::cout << std::endl << "Enter month (1-12): ";
    std::cin >> month;
    if(month>=1 && month <=MONS_IN_YR) 
      break;    
    else
    {
      std::cout << "Invalid month. Try again.";
      exit(0);
    }
  }while(1);
  return month;
}

// Get a valid month (Valid range 2010-2013)
unsigned int get_a_valid_year()
{
  unsigned int year;
  // Keep looping till a valid year is given.
  do
  {
    std::cout << std::endl << "Enter year (2010-2013): ";
    std::cin >> year;
    if(year>=2010 && year <=2013)
      break;    
    else
    {
      std::cout << "Invalid year. Try again.";
      exit(0);
    }
  }while(1);
  return year;
}

// Class to contain/implement: Year and its Temperatures
class year_temp_arr
{
  private:
    // Two dimensional arear using vector.
    std::vector< std::array<double,MONS_IN_YR> > two_d_arr;
  public:
    // Default constructor. Assume no of years as 1. Default 2d array of size 1 X 12. Initialized with zero.
    year_temp_arr():two_d_arr(1, { { 0,0,0,0,0,0,0,0,0,0,0,0} } ) {}
    // Constructor with dimensions (no of years). Initialized with zero.
    year_temp_arr(int row):two_d_arr(row, { { 0,0,0,0,0,0,0,0,0,0,0,0} } ) {}
    // Constructor with dimensions (no of years). Initialized with the intializer provided.
    year_temp_arr(int row, const double *vals);    
    // Print the values in two dimensional array.
    void print_yr_n_tmp();
    // Print the temperature of month, year.
    void print_temp_of_a_month();
    // Get the minimum and maximum temperature in year.
    void get_min_max_temp(int year, double &min, double &max);
    // Print the minimum and maximum temperature in year.
    void print_hi_n_low_temp_of_a_month();
    // Print the average temperature in year.
    void print_avg_temp_of_a_year();
    // Compute average temperature in year.
    double get_avg_temp_of_a_year(int year);
    // Print the average temperature in month.
    void print_avg_temp_of_a_month();
};

year_temp_arr::year_temp_arr(int row, const double *vals):two_d_arr(row, { { 0,0,0,0,0,0,0,0,0,0,0,0} } )
{
  // Fill the two_d_arr with the initializer values in vals.
  for(int ri=0; ri<row; ++ri)
    for(int ci=0; ci<MONS_IN_YR; ++ci)
      two_d_arr[ri][ci]=vals[(ri*MONS_IN_YR)+ci];
}

// Print the temperature of all the years.
void year_temp_arr::print_yr_n_tmp()
{    
  std::cout << std::endl;
  for (std::vector< std::array<double,MONS_IN_YR> >::iterator i = two_d_arr.begin(); i != two_d_arr.end(); ++i)
  {
    for(int mon_i=0; mon_i <MONS_IN_YR; ++mon_i)
      std::cout << "\t" << (*i)[mon_i];
    std::cout << std::endl;
  }
}

// Print the temperature of month, year (got from user as input).
void year_temp_arr::print_temp_of_a_month()
{
  // Get the month.
  unsigned int month = get_a_valid_month();
  unsigned year = get_a_valid_year();
  // Print the temperature of month, year.
  std::cout << std::endl << "Temperature of month " << month << " of year " 
            << year << " is: " << two_d_arr[year-2010][month-1] << std::endl;
  return;
}

// Find the minimum and maximum temperature of a year.
void year_temp_arr::get_min_max_temp(int year, double &min, double &max)
{
  double temp;
  min=0.0;
  max=0.0;  
  // Find the minimum and maximum temperature in year.
  for(int mon_i=0; mon_i <MONS_IN_YR; ++mon_i)
  {
    temp = two_d_arr[year-2010][mon_i];
    if(min > temp) min = temp;
    if(max < temp) max = temp;
  }
  return;
}

// Find the average temperature of a year.
double year_temp_arr::get_avg_temp_of_a_year(int year)
{
  double sum=0, avg=0;
  // Find the average temperature in year.
  for(int mon_i=0; mon_i <MONS_IN_YR; ++mon_i)  
    sum += two_d_arr[year-2010][mon_i];
  avg = sum/MONS_IN_YR;
  return avg;
}

// Print the minimum and maximum temperature in year.
void year_temp_arr::print_hi_n_low_temp_of_a_month()
{
  double min, max;
  // Get the year from user.
  unsigned int year = get_a_valid_year();
  // Compute the minimum and maximum temperature in year.
  get_min_max_temp(year, min, max);
  // Print the minimum and maximum temperature in year.
  std::cout << std::endl << "High and low temperatures of year " << year 
            << " are " << min << " " << max << std::endl;
  return;
}

// Print the average temperature in year.
void year_temp_arr::print_avg_temp_of_a_year()
{
  double avg;
  // Get the year from user.
  unsigned int year = get_a_valid_year();
  // Compute the average temperature in year.
  avg = get_avg_temp_of_a_year(year);
  // Print the average temperature in year.
  std::cout << std::endl << "The average temperature for year " << year 
            << " is " << avg  << std::endl;
  return;
}

// Print the average temperature of a month (of several years).
void year_temp_arr::print_avg_temp_of_a_month()
{
  double avg, sum=0;
  // Get the month from user.
  unsigned int month = get_a_valid_month();
  // Compute the average temperature of a month (of several years).
  for (std::vector< std::array<double,MONS_IN_YR> >::iterator i = two_d_arr.begin(); i != two_d_arr.end(); ++i)  
    sum += (*i)[month-1];
  avg = sum/two_d_arr.size();
  // Print the average temperature of a month (of several years).
  std::cout << std::endl << "The average temperature of month " << month 
            << " is " << avg  << std::endl;
  return;
}

int main()
{
  // Monthwise temperature of year 2010, 2011, 2012, 2013
  const double temps[]=
  {
    -16.1, -13.5, 15.9, 28.1, 30.2, 32.7, 35.8, 30.5, 25.3, 18.5, 5.0, -1.5, // 2010
    -15.1, -13.9, 17.9, 25.5, 28.5, 31.5, 34.8, 28.9, 26.4, 15.8, 2.8, -2.8, // 2011
    -21.1, -15.9, 20.9, 23.4, 29.4, 30.8, 29.6, 24.6, 27.8, 19.8, 3.4,  0.0, // 2012
    -22.1, -13.9, 19.9, 21.6, 31.2, 29.5, 33.2, 35.1, 24.6, 14.3, 5.7, -8.0  // 2013
  };
  
  year_temp_arr yr_n_tmp(4,temps);

  do
  {
    int option;
    std::cout << std::endl
              << "1. Temperature of a month for a given year." << std::endl
              << "2. High and low temperatures for a given year." << std::endl
              << "3. The average temperature for a given year." << std::endl
              << "4. The average temperature of a month for all years." << std::endl
              << "5. Exit program." << std::endl
              << "Enter your option(1-4): ";
    std::cin >> option;

    switch(option)
    {
      case 1:
        yr_n_tmp.print_temp_of_a_month();
        break;
      case 2:
        yr_n_tmp.print_hi_n_low_temp_of_a_month();
        break;
      case 3:
        yr_n_tmp.print_avg_temp_of_a_year();
        break;
      case 4:
        yr_n_tmp.print_avg_temp_of_a_month();
        break;
      case 5:
        exit(0);
      default:        
        std::cout << std::endl << "Invalid option. Try again." << std::endl;
        exit(0);
    };   
  }while(1);
  
  return 0;
}
stfgts619 wrote:
#share your skills and gain more...
...and your homework is done...

No beginner will ever take look at this
closed account (L8bqko23)
i made some changes in the question
how about doing same program in functions
Look at all the people resolving this guy's homeworks.
He cannot even write functions, like wtf, don't do it, are you dumb or something?
how about doing same program in functions

How about you have a go at writing it? I get paid to write C++ for a living, why would I want to do your homework for you?
Last edited on
stfgts619 wrote:
Show_Your_Skills
stfgts619 wrote:
lets see your skills
stfgts619 wrote:
i did not ask u 2 write coding,
This is why people are angry at you.

EDIT: It appears that posts are disappearing :\
Last edited on
Nothing wrong with us. You are mentally unstable. Or 5 years old.
Topic archived. No new replies allowed.