Trouble with arrays in my code.

The problem in my code I'm trying to fix is the last two for statements.My program always messes up the either the low/high value I check and display because they change for some reason. I'm fairly new to this so I'm not sure where I've messing up. The temp.txt values are irrelevant as long as there are 12 float values. Anyone see where I went wrong?

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

#include <string> 
#include <iomanip>
#include <fstream> 
#include <iostream>

using namespace std;

int main()

{

	const int SIZE = 12;
	float tempArray[SIZE]; 
	string months[SIZE] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
	float temperature;
	float lowest = tempArray[0];
	float highest = tempArray[0]; 
	ifstream tempchart;
	tempchart.open("temp.txt"); 
	cout << "This program will display the average temperature of each month of a given year in ferenheit and celcius." << endl; 
	cout << "It will also display the higest and lowest temperature of the year." << endl; 
	
	for( int i = 0;  i < SIZE; i++)
	{
		tempchart >> tempArray[i]; 
	}
	cout << "Month" << setw(30) << setfill('.') ; 
	cout << "Avg temp farenheit" << setw(30) ; 
	cout << "Avg temp celsius" << endl; 
	
	for(int i = 0; i < SIZE; i++) 
	{
		
		cout << months[i] << ": " << setw(16) << tempArray[i] << setw(44) << setprecision(4) <<(5*(tempArray[i]-32)) / 9 << endl;
		
	}

	for (int i = 0; i < SIZE; i++)
	{
		if (tempArray[i] > highest)
			highest = tempArray[i];
	}
	cout << "The highest temperature in farenheit is: " << highest << endl; 
	
	for( int i = 0; i < SIZE; i++)
	{
		if (tempArray[i] < lowest)
			lowest = tempArray[i];
		
	}
	cout << "The lowest temperature in farenheit is: " << lowest << endl;


return 0;

}
Last edited on
The initial values of lowest and highest are undertemined because the array was not entered yet.

float lowest = tempArray[0];
float highest = tempArray[0];
In C++, it is valid to define your variables anywhere in a function.
There are two schools of thought regarding this practice, and the common advice given by instructors is to place all of your variable definitions at the beginning of a function so that they may be easily located.

Others advocate defining your variables immediately before you first use them and only after all initialization data is available. It can be said that code written this way is easier to read because the definition and usage of a variable are close together. It is also sometimes claimed that this reduces the potential for introducing errors into your program by minimizing the chances that you will accidentally alter a variable's value between the lines where you initialize it and the lines that you use it.

I would relocate the definitions of highest and lowest like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        float highest = tempArray[0];
	for (int i = 1; i < SIZE; i++)
	{
		if (tempArray[i] > highest)
			highest = tempArray[i];
	}
	cout << "The highest temperature in farenheit is: " << highest << endl; 
	
        float lowest = tempArray[0];
	for( int i = 1; i < SIZE; i++)
	{
		if (tempArray[i] < lowest)
			lowest = tempArray[i];
		
	}
        cout << "The lowest temperature in farenheit is: " << lowest << endl;

You may also notice that I've changed the initial value of i in each for statement to skip over offset 0, since we don't need to compare the first value to itself.

As a compromise, you could leave your definitions where they are, and reinitialize them after you have read in your temperature data.
Now I get it. Thanks for insight.
Topic archived. No new replies allowed.