Help reading file into arrays.

This is the assignment:
"Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:
a) Function getData: This function reads and stores data in the two-dimensional array.
b) Function averageHigh: This function calculates and returns the average high temperature for the year.
c) Function averageLow: This function calculates and returns the average low temperature for the year.
d) Function indexHighTemp: This function returns the index of the highest high temperature in the array.
e) Function indexLowTemp: This function returns the index of the lowest low temperature in the array."

My teacher gave us some data and wants us to input it using a file. My question is, how should I write the input file to get each temperature into its correct slot in the array? Also, am I inputting the file correctly? Thanks.

-Puckett

What I have so far.
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
  #include <iostream>
#include <fstream>

using namespace std;

const int months = 12;

void getData(double [][ 2 ], int);
double averageHigh(double [] [ 2 ], int);
double averageLow(double [] [ 2 ], int);
int indexHighTemp(double [] [ 2 ], int);
int indexLowTemp(double [] [ 2 ], int);

int main()
{
   double temperatures[months][2];
   
   getData(temperatures, months);

   cout << "\n\nThe average high temp. for the year: "
	   << averageHigh(temperatures, months) << endl;
   cout << "\n\nThe average low temp. for the year: "
	   << averageLow(temperatures, months) << endl;

   cout << "\n\nIndex of highest temp. for the year: "
	   << indexHighTemp(temperatures, months) << endl;
   cout << "\n\nIndex of lowest temp. for the year: "
	   << indexLowTemp(temperatures, months) << endl;

   system("PAUSE");
    return 0;
}

void getData(double t[][2], int m)
{
	int i;
	ifstream inFile;
	ofstream outFile;

	inFile.open("tempsFile.txt");
	
	if (!inFile)
	{
		cout << "Cannot open input file."
			<< endl;
	}
	
	for (int i=0; i<m; i++)
		inFile >> t[i][0];
		cout << endl;
		inFile >> t[i][1];
		cout << endl;
}

double averageHigh(double t[] [2], int m)
{
	double sum = 0;

	for (int i=0; i<m; i++)
		sum += t[i][0];
	return (sum/m);
}

double averageLow(double t[][2], int m)
{
	double sum = 0;
	
	for (int i=0; i<m; i++)
		sum += t[i][1];
	return (sum/m);
}

int indexHighTemp(double t[][2], int m)
{
	int ind = 0;
	double highest = t[0][0];

	for (int i=1; i<m; i++)
		if (t[i][0] > highest)
		{
			highest = t[i][0];
			ind = i;
		}
		return ind;
}

int indexLowTemp(double t[][2], int m)
{
	int ind = 0;
	double lowest = t[0][1];

	for (int i=1; i<m; i++)
		if (t[i][1] < lowest)
		{
			lowest = t[i][1];
			ind = i;
		}
		return ind;
}
P.s. - It compiles OK. But the output data is incorrect.
So, Im on line 48:

try this:
1
2
3
4
for (int i=0; i<m; i++){ // I would include these statements in brackets, so your 2D array is being filled
		inFile >> t[i][0];
		inFile >> t[i][1];
}

if you're still not getting what you want, let us know, whats going on
My output screen says:
"Cannot open input file.

The average high temp. for the year: 7.1635e+234

The average low temp. for the year: 7.64985e+233

Index of highest temp. for the year: 8

Index of lowest temp. for the year: 5"

My input file is:
"35.5 57.6
38.6 62.4
45.4 70.5
51.2 77.5
60.1 84.6
67.3 90.6
70.9 92.7
70.1 92.2
64.9 87.7
52.2 78.7
43.5 68.7
37.6 60.3"
My output screen says:
"Cannot open input file.
1
2
3
4
5
6
7
	inFile.open("tempsFile.txt");
	
	if (!inFile)
	{
		cout << "Cannot open input file."
			<< endl;
	}


That error message you sensibly put into your code has alerted you to exactly what the problem is. Your output data is nonsense because your program isn't able to open the file to read the input data.

You need to check that the file is in the correct directory, is correctly named, has the correct permissions, etc.
OK. It's not giving me the "Cannot open input file" anymore, but the output is still incorrect.

"The average high temp. for the year: 53.1083

The average low temp. for the year: 76.9583

Index of the highest temp. for the year: 6

Index of the highest temp. for the year: 0"

Figured it out! Thanks guys
Topic archived. No new replies allowed.