Need help with temperature program that involves arrays and functions

Hello everyone, this is my first time on this site and my first post. First off i am taking a c++ class because i need it to continue with my engineering degree. c++ is sort of fun (no offense) but its not something i am really interested in. Anyway here is the guidlines to the program i need to make.

Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year for a city of your choice. You, as the programmer, must create an input text file that contains the name of the city as well as the high/low temperature data for each month for that city. You can find this information by googling "average monthly high and low temperatures for 'city of your choice'". Using functions as described below, the program must retrieve and process the data from the file. The main program opens the file, calls the functions, and then outputs the name of the city, the average high, the average low, and the month that has the highest and lowest temperatures for the year, in a user friendly way. Don't forget to close the file before ending the main program.

Your program must consist of the following functions:

Function getData: this function reads and stores the city name and reads and stores the temperature data in a two-dimensional array.
Function averageHigh: This function calculates and returns the average high temperature for the year.
Function averageLow: This function calculates and returns the average low temperature for the year.
Function indexHighTemp: This function returns the index of the highest high temperature in the array. (The main program will interpret that index to figure out the name of the month).
Function indexLowTemp: This function returns the index of the lowest low temperature in the array. (The main program will interpret that index to figure out the name of the month).

The problem i am having is that my average high, average low, high index, and low index all display -9.25596e+061. Below is the code i am using. I don't exactly know what all of it means but i think i have a general idea. The arrays confuse me a little bit
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
  #include<iostream>
#include<string>
#include<fstream>

using namespace std;

void getdata(ifstream & citydata, string & name, double temps[][2]);
void averagehigh(double temps[][2], double sum, double & average);
void averagelow(double temps[][2], double sum, double & average);
void indexhightemp(double temps[][2], double highest, double & index);
void indexlowtemp(double temps[][2], double lowest, double & index);

int main()
{

	string city;
	double temperatures[12][2], temp1[12][2], average1, sum1=0, temp2[12][2], average2, sum2=0, temp3[12][2], highest1 = 0 , index1, temp4[12][2], lowest1 = 0, index2;
	ifstream data;
//***************************************

	data.open("averagetemps.txt");
//**************************************
	getdata(data, city, temperatures);
//******************************************
	cout << city << endl;
	for (int i = 0; i<12; i++)
	{
		for (int j = 0; j<2; j++)
			cout << temperatures[i][j] << "  ";
		cout << endl;
	}
//**********************************
	data.close();
//*********************************************	

	averagehigh(temp1, sum1, average1);
		cout << average1 << endl;

	averagelow(temp2, sum2, average2);
		cout << average2 << endl;

	indexhightemp(temp3, highest1, index1);
		cout << index1 << endl;

	indexlowtemp(temp4, lowest1, index2);
		cout << index2 << endl;

//**********************************************

	return 0;
}


void getdata(ifstream & citydata, string & name, double temps[][2])
{
	citydata >> name;
	for (int i = 0; i<12; i++)
	{
		for (int j = 0; j<2; j++)
			citydata >> temps[i][j];
	}
}

void averagehigh(double temps[][2], double sum, double & average)
{
	sum = 0;
	for (int i = 0; i<12; i++)
	{
		sum = sum + temps[i][1];
		average = (sum / 12);
	}
}

void averagelow(double temps[][2], double sum, double & average)
{
	sum = 0;
		for (int i = 0; i < 12; i++)
		{
		sum = sum + temps[i][0];
		average = (sum / 12);
		}
}

void indexhightemp(double temps[][2],double highest, double & index)
{
	for (int i = 0; i < 12; i++)
	{
		if (temps[i][1] > highest)
		{
			highest = temps[i][1];
			index = i;
		}
	}
}

void indexlowtemp(double temps[][2], double lowest, double & index)
{
	for (int i = 0; i < 12; i++)
	{
		if (temps[i][0] > lowest)
		{
			lowest = temps[i][0];
			index = i;
		}
	}
}
What values does "temp1" contain on line 35?
I am not sure what you mean by that. temp 1 is (temp1[12][2]).
Yes, but what does it contain?

Line 23 reads data into the temperatures array. Where is anything stored into temp1? If you don't put anything in the array (temp1), computing it's average won't make any sense.

it takes its info from a text file.

New_York_City
23 36
24 40
32 48
42 58
53 68
63 77
68 83
66 81
58 74
47 63
38 52
28 42
oh. i think i see what you are saying. temp1,2,3,4 have nothing assigned to it. the data is stored in temperatures. I stuck temperatures in place if the temp1,2,3,4 and i am getting back some real numbers.
so my average high and average low temps are coming out perfect, yay. I don't actually understand what this is asking. Maybe someone can clarify.

Function indexHighTemp: This function returns the index of the highest high temperature in the array. (The main program will interpret that index to figure out the name of the month).
Function indexLowTemp: This function returns the index of the lowest low temperature in the array. (The main program will interpret that index to figure out the name of the month).
Think about these:
How does one usually use an index of an array element?
What is the type of an index of an array element usually?

You should have a third array that has the names of the months. The index of the month that was hot should be equal to the index of the element in that name-array that contains the name of the appropriate month.
i dont actually know what an index is. Is it asking for the heat index? does it just want me to display the highest and the lowest temperature from the corresponding columns?
Last edited on
What are the i and j in cout << temperatures[i][j] << " ";
i is 0->12 and j is 0->2
Those are their values. The i and j are indices that are used to access an element of the array. See http://www.cplusplus.com/doc/tutorial/arrays/
so i cant get the last two functions to work. I am trying to get them to display the month of the highest temp and of the lowest temp and then store that number. My coding looks good to me. for the highest temp index i get a 6 outputted which is the proper number but i do not know if the function is working properly. For the lowest index i just get -925596e+061.
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
#include<iostream>
#include<string>
#include<fstream>

using namespace std;

void getdata(ifstream & citydata, string & name, double temps[][2]);
void averagehigh(double temps[][2], double sum, double & average);
void averagelow(double temps[][2], double sum, double & average);
void indexhightemp(double temps[][2], double highest, double & index);
void indexlowtemp(double temps[][2], double lowest, double & index);

int main()
{

	string city;
	double temperatures[12][2], average1, sum1=0, average2, sum2=0, highest1 = 0 , index1, lowest1 = 0, index2;
	ifstream data;
//***************************************

	data.open("averagetemps.txt");
//**************************************
	getdata(data, city, temperatures);
//******************************************
	cout << city << endl;
	for (int i = 0; i<12; i++)
	{
		for (int j = 0; j<2; j++)
			cout << temperatures[i][j] << "  ";
		cout << endl;
	}
		cout << endl;
//**********************************
	data.close();
//*********************************************	

	averagehigh(temperatures, sum1, average1);
		cout << "The average high temperature in NYC for the year is " << average1 << " degrees farenheight." << endl;

	averagelow(temperatures, sum2, average2);
		cout << "The average low temperature in NYC for the year is " << average2 << " degrees farenheight." << endl;

	indexhightemp(temperatures, highest1, index1);
		cout << index1 << endl;

	indexlowtemp(temperatures, lowest1, index2);
		cout << index2 << endl;

//**********************************************

	return 0;
}


void getdata(ifstream & citydata, string & name, double temps[][2])
{
	citydata >> name;
	for (int i = 0; i<12; i++)
	{
		for (int j = 0; j<2; j++)
			citydata >> temps[i][j];
	}
}

void averagehigh(double temps[][2], double sum, double & average)
{
	sum = 0;
	for (int i = 0; i<12; i++)
	{
		sum = sum + temps[i][1];
		average = (sum / 12);
	}
}

void averagelow(double temps[][2], double sum, double & average)
{
	sum = 0;
		for (int i = 0; i < 12; i++)
		{
		sum = sum + temps[i][0];
		average = (sum / 12);
		}
}

void indexhightemp(double temps[][2],double highest, double & index)
{
	highest = 0;
	for (int i = 0; i < 12; i++)
	{
		if (temps[i][1] > highest)
		{
			highest = temps[i][1];
			index = i;
		}
	}
}

void indexlowtemp(double temps[][2], double lowest, double & index)
{
	lowest = 0;
	for (int i = 0; i < 12; i++)
	{
		if (temps[i][0] < lowest)
		{
			lowest = temps[i][0];
			index = i;
		}
	}
}
Last edited on
Thanks everyone i got it.
Topic archived. No new replies allowed.