Help with Temperature Program

I need help writing a C++ program which reads temperatures from a data file called temperature.txt, stores the temperatures into an array and computes and prints:

The mean temperature, the maximum temperature, the minimum temperature, and the range.
Last edited on
What sort of help?

The first step is as always, a simple loop to make sure you can read the file successfully.
1
2
3
4
ifstream fin("temperature.txt");
while ( fin >> value ) {
  cout << "Value=" << value << endl;
}


Then think about
- putting all the values in an array / vector

Then functions to compute
- the mean temperature,
- the maximum temperature,
- the minimum temperature,
- the range.
Honestly, I have no idea how to set this up.
The current code I have now is this:
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>
using namespace std;
#include <fstream>
void load_array(int [], int & );
//void range_array (int [], int , int & );
void max_value (int [], int , int & );
void loc_min_value (int [], int , int &);
void print_array( int [], int );
void range_array (int, int);



int main()
{

	int num;
	int sum;
	int max;
	int x[20];
	int min;
	load_array (x, num);
//	print_array(x, num);
//	sum_array(x, num, sum);
	loc_min_value (x, num , min);
	max_value (x, num, max);
	range_array (min, max);
	system("pause");
	return 0;
}
//This function loads values into the array from a data file called "aray.dat"
void load_array(int intarray[], int & size)
{
	ifstream infile;
	infile.open("temp.txt");
	size = 0;
	while (!infile.eof() )
	{
		infile >> intarray[size];
		size ++;
	}
	infile.close();
}


//This function computes the sum of the elements in the array
/*void range_array (int intarray[], int size, int & array_range)
{
	cout << "Printing the range of the array " << endl;
	array_sum =0;
	for (int i =0;  i < size; i++)
		array_range = min -;
	cout << array_range << endl;
}
*/

//This function prints the largest value in the array
void max_value (int intarray[20], int size, int & array_max)
{
	cout << "Printing the largest value " <<endl;

	array_max = intarray[0];
	for (int i = 1; i < size; i++)
		if (intarray[i] > array_max)
			array_max = intarray[i];
	cout << array_max << endl;
}

//This function prints the position of the smallest value in the array
void loc_min_value (int x[20], int num, int &min)
{
	cout << "Printing the location of the minimum value " <<endl;

	min = x[0];
	int loc = 0;
	for (int i = 1; i < num; i++)
		if (x[i] < min)
		{
			loc = i;
			min = x[i];
		}
	cout << loc << endl;
}

void range_array (min_val, max_value)
{
	range_array = max_value - min_val;
	cout << "The range is " << range_array;
	
	
}

//This function prints the contents of the array to the screen
/*void print_array(int x[], int num)
{
	cout << "printing the array " <<endl;
	for (int i = 0; i < num; i++)
		cout << x[i] <<endl;
}
*/
Does it work?

I would suggest changing
1
2
3
4
5
	while (!infile.eof() )
	{
		infile >> intarray[size];
		size ++;
	}

to
1
2
3
4
	while (size < 20 && infile >> intarray[size] )
	{
		size ++;
	}

- eof doesn't do what you think it does.
- always check your arrays for possible overflow.
- better yet, use a std::vector.


> //This function prints the position of the smallest value in the array
> void loc_min_value (int x[20], int num, int &min)
I saw no mention of such a thing in your original post.
You should have 4 functions named specifically after the 4 computations you have listed.


Everything seems to work except that I get an error at line 82 of my code:
void range_array (min_val, max_value)
Well you list no types for those parameters.
I realize that and so I changed it from void range_array (min_val, max_value) to void range_array (min, max_value)
That still isn't giving types to the function parameters.

On line 69, for example, look at how each of the function parameters has types.
You want int for the two parameters for range_array.

Second, you are then trying to do an assignment range_array = max_value - min_val;
This is wrong, you're trying to assign a value to the name of the function (technically a function pointer).
do instead:
1
2
int range = max_value - min_value;
cout << range; // etc.  
Last edited on
I have everything working. Now I just in to create a void function for the mean/average.

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
#include <iostream>
#include <fstream>
using namespace std;

//Declaring the voids
void load_array(int [], int & );
void max_value (int [], int , int & );
void loc_min_value (int [], int , int &);
void print_array( int [], int );
void range_array (int, int);
void mean_array (int, int)



int main()
{
	//Declaring the variables
	int num;
	int sum;
	int max;
	int x[20];
	int min;
	
	load_array (x, num);
//	print_array(x, num);
//	sum_array(x, num, sum);
	loc_min_value (x, num , min);
	max_value (x, num, max);
	range_array(min, max);
	system("pause");
	return 0;
}
//This function loads values into the array from a data file called "temperature.dat"
void load_array(int intarray[], int & size)
{
	ifstream infile;
	infile.open("temperature.dat");
	size = 0;
	while (!infile.eof() )
	{
		infile >> intarray[size];
		size ++;
	}
	infile.close();
}


//This function computes the sum of the elements in the array
/*void range_array (int intarray[], int size, int & array_range)
{
	cout << "Printing the range of the array " << endl;
	array_sum =0;
	for (int i =0;  i < size; i++)
		array_range = min -;
	cout << array_range << endl;
}
*/

//This function prints the largest value in the array
void max_value (int intarray[20], int size, int & array_max)
{
	cout << "Printing the largest value " <<endl;

	array_max = intarray[0];
	for (int i = 1; i < size; i++)
		if (intarray[i] > array_max)
			array_max = intarray[i];
	cout << array_max << endl;
}

//This function prints the position of the smallest value in the array
void loc_min_value (int x[20], int num, int &min)
{
	cout << "Printing the location of the minimum value " <<endl;

	min = x[0];
	int loc = 0;
	for (int i = 1; i < num; i++)
		if (x[i] < min)
		{
			loc = i;
			min = x[i];
		}
	cout << loc << endl;
}

//This function prints the range between the smallest and the largest values in the array
void range_array(int min, int max_value)
{
	int array_range = max_value - min;
	cout << "The range is: " << array_range << endl;
}






//This function prints the contents of the array to the screen
/*void print_array(int x[], int num)
{
	cout << "printing the array " <<endl;
	for (int i = 0; i < num; i++)
		cout << x[i] <<endl;
}
*/
It's a shame you ignored all the advice about array range checking and eof().
Thanks for playing.
> Now I just in to create a void function for the mean/average.
¿ever heard of return a value?
¿why a function `max_value()' is doing any printing?

except for `load_array()', whose purpose is specifically to read from a file, the other functions shouldn't be doing any input/output operation
you end with unnecessary complex function that end up being useless
Topic archived. No new replies allowed.