Trouble with finding lowest value in Array!

Hello, I have a project due very soon. It involves reading data from a file, putting the data into an array, finding the lowest and highest value in an array. I am using a for loop to find the highest and it works, but finding the lowest doesn't work. It keeps returning a n negative number

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
  
        highest = rainfall[0];
	lowest = rainfall[0];
	for (int i = 0; i < ray_size; i ++)
	{
		fin >> rainfall[i];
		total += rainfall[i];

		if (rainfall[i] > highest)
		{
			highest = rainfall[i];
			month_high = months[i];
		}
		if (rainfall[i] < lowest)
		{
			lowest = rainfall[i];
			month_low = months[i];
		}
		if (rainfall[i] == 0)
		{
			no_rain[i] = months[i];
		}

	}




The output that I get is this
http://imgur.com/6LBc2KC

this is the data going in
8 7.6 4.2 2.4 0.2 0 0 0 1.2 3.6 5.8 12
closed account (48T7M4Gy)
what is the value of ray_size?
The value is 12
const int ray_size = 12;
Hi,

We need some more code, the entire function and the code that calls it, with the values used to call it :+)

Cheers :+)
closed account (48T7M4Gy)
Hmm. I thought you might have had a different value because the weird number could be caused by you going outside the limits of the array or rainfall array hasn't been initialised and is reading a junk value.

If you display the rainfall figures then maybe an error will show up somewhere else. On the off-chance change the zeroes to 0.0
Last edited on
This is so frustrating, I just cant figure it out anymore. if anyone can help i would be very happy. It;s due tomorrow and i just don't know anymore, the damn code is supposed to work


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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

//Function protypes
void print_no_rain(string no_rain[], int ray_size, ofstream & fout);
float avg(float total, int ray_size);
void begin(string & username, string &filename1, string & filename2, ifstream & fin, ofstream & fout);
void display1(string cityname, string username, string month_low, string month_high, float average, float lowest, float highest,string no_rain[],int ray_size, ofstream & fout);
void graph_display(int ray_size, string months[], float rainfall[], ofstream & fout);


int main()
{



	const int ray_size = 12;
	
	ifstream fin;
	ofstream fout;
	string filename1, filename2, username, cityname;
	float rainfall[ray_size];
	float average = 0;
	
	float total = 0;
	float highest;
	float lowest;
	string months[12] = { "January" , "February", "March", "April", "May","June", "July", "August", "September", "October", "Nomvember", "December" };
	string month_high;
	string month_low;
	string no_rain[12];
	
	// Collect the username and file names from the user

	begin(username, filename1,filename2,fin,fout);
	getline(fin, cityname);
	highest = rainfall[0];
	lowest = rainfall[0];
	for (int i = 0; i < ray_size; i ++)
	{
		fin >> rainfall[i];
		total += rainfall[i];

		if (rainfall[i] > highest)
		{
			highest = rainfall[i];
			month_high = months[i];
		}
		if (rainfall[i] < lowest)
		{
			lowest = rainfall[i];
			month_low = months[i];
		}
		if (rainfall[i] == 0)
		{
			no_rain[i] = months[i];
		}

	}

		average = avg(total, ray_size);
		display1(cityname, username, month_low, month_high, average, lowest, highest, no_rain, ray_size, fout);
		graph_display(ray_size, months, rainfall, fout);
	
	return 0;
}



void print_no_rain(string no_rain[], int ray_size, ofstream & fout)
{
	for (int x = 0; x < ray_size; x++)
	{
		cout << no_rain[x];
		fout << no_rain[x];
		
	}
}

// func calculate the average
float avg(float total, int ray_size)
{  
	float average;
	average = total / ray_size;
	return average;
}

// func collects user name and file names, also opens/creates files
void begin(string &username, string &filename1, string &filename2, ifstream &fin, ofstream &fout  ) 
{
	cout << "Enter your name:";
	getline(cin, username);
	cout << "Enter the input file name:";
	getline(cin, filename1);
	cout << "Enter the output file name:";
	getline(cin, filename2);
	fin.open(filename1.c_str());
	fout.open(filename2.c_str());
	cout << " " << endl;
}

//func displays data
void display1(string cityname, string username, string month_low, string month_high, float average, float lowest, float highest, string no_rain[], int ray_size, ofstream & fout)
{

	cout << "The rainfall report for " << cityname << "prepared by " << username << ":" << endl;
	fout << "The rainfall report for " << cityname << "prepared by " << username << ":" << endl;
	cout << setw(35) << left << fixed << setprecision(2) << "Year's Average Rainfall:" << average << " Inches" << endl;
	fout << setw(35) << left << fixed << setprecision(2) << "Year's Average Rainfall:" << average << " Inches" << endl;
	cout << setw(34) << "Month with Lowest Rainfall:" << month_low << " Rainfall:" << lowest << " Inches" << endl;
	fout << setw(34) << "Month with Lowest Rainfall:" << month_low << " Rainfall:" << lowest << " Inches" << endl;
	cout << setw(35) << "Months with no Rainfall:";
	fout << setw(35) << "Months with no Rainfall:";
	print_no_rain(no_rain,ray_size,fout);
	cout << setw(35) << "\nMonth with Highest Rainfall: " << month_high << " Rainfall:" << highest << " Inches" << endl;
	fout << setw(35) << "\nMonth with Highest Rainfall: " << month_high << " Rainfall:" << highest << " Inches" << endl;
	cout << "\n\n";
	fout << "\n\n";

	
}
//func to display the graph
void graph_display(int ray_size, string months[], float rainfall[], ofstream & fout)
{
	fout << "------------------------------------------------------------------------------" << endl;
	cout << "------------------------------------------------------------------------------" << endl;
	for (int i = 0; i < ray_size; i++)
	{
		fout << setw(10) << months[i] << "|";
		cout << setw(10) << months[i] << "|";
		for (int y = 0; y < rainfall[i]; y++)
		{
			cout << " * ";
			fout << " * ";
		}

		cout << " " << endl;
		fout << " " << endl;
	}
	cout << "------------------------------------------------------------------------------" << endl;
	cout << setw(11) << "Inches" << " 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20" << endl;
	fout << "------------------------------------------------------------------------------" << endl;
	fout << setw(11) << "Inches" << " 1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20" << endl;

}









Last edited on
closed account (48T7M4Gy)
OK that's a great start. Now just cut and paste your data file into your post and we'll have a play with it.
Last edited on
the text fil

Los Angeles
8 7.6 4.2 2.4 0.2 0 0 0 1.2 3.6 5.8 12
closed account (48T7M4Gy)
You haven't included main() etc????
yes, its all there along with the function protypes
Crikey, I don't see anything obvious, first instinct is to use a debugger. Look at how the values change as the program executes line by line - deduce where it all goes wrong :+)
closed account (48T7M4Gy)
You haven't initialised rainfall[] = {0} in line 24
closed account (48T7M4Gy)
Enter your name:cc
Enter the input file name:rainfall.txt
Enter the output file name:c.txt

The rainfall report for Los Angelesprepared by cc:
Year's Average Rainfall:           3.75 Inches
Month with Lowest Rainfall:        Rainfall:0.00 Inches
Months with no Rainfall:           JuneJulyAugust
Month with Highest Rainfall:      December Rainfall:12.00 Inches


------------------------------------------------------------------------------
January   | *  *  *  *  *  *  *  *
February  | *  *  *  *  *  *  *  *
March     | *  *  *  *  *
April     | *  *  *
May       | *
June      |
July      |
August    |
September | *  *
October   | *  *  *  *
Nomvember | *  *  *  *  *  *
December  | *  *  *  *  *  *  *  *  *  *  *  *
------------------------------------------------------------------------------
Inches      1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20
Press any key to continue . . .
closed account (48T7M4Gy)
line 26: float rainfall[ray_size] = { 0 };

BTW good graph, needs a small amount of work and you need another dataset without zero rainfall just as a check on non-zero minima. :)
Last edited on
I'm lost with this stuff, using another file the minimum is always zero even if no zero is in the file.
closed account (48T7M4Gy)
I'm lost with this stuff, using another file the minimum is always zero even if no zero is in the file.


Yeah there's a subtlety I forgot to mention. I agree that you take rainfall[0] as the lowest and highest but only in principle, because if you initialise rainfall to {0} , rainfall[0] will be 0 so no record will be less! Get it?

So what you do is initialise lowest = 10000 or some humungous number. Get it?

PS and highest = -50000 for arguments sake and to be on the safe side.
Last edited on
closed account (48T7M4Gy)
PS that's why you need to initialise variables when you declare them eg at lines 30 and 31. :)
Thanks for your help!
Topic archived. No new replies allowed.