Having trouble sorting an array with data read from a file.

This program is supposed to read a file of weather snow and rainfall data for about 1171 days of data, then read the file into an array, and give the user the average snowfall and rainfall, median snowfall and rainfall, and the standard deviation.

My program uses a few conversion functions so that the file data can be placed into an array, then the average for snow and rain is calculated.

I am having trouble sorting the array from largest to smallest value in each of the two columns. I have the sort function below, but I am not sure how to assign values from each column to be sorted. I have not called the sort function yet for the sake of compiling properly.

Output example (this information is taken from the bottom 9 lines output, where the large number is the day, followed by the snow fall for the day in centimeters, and rainfall for the day in centimeters.):

1162 14 35
1163 8 22
1164 0 0
1165 0 11
1166 0 9
1167 0 72
1168 0 15
1169 0 20
1170 0 768
Mean snowfall: 1.27839 cm
Mean rainfall: 73.8301 cm


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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  // reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include<sstream>
#include<algorithm>
#include<iterator>
#include<vector>
#include <cstdlib>   // for calls to rand(), srand()
#include <ctime>

using namespace std;


struct weatherarray
{
int snow;
int rain;
};


template <typename T>
T StringToNumber ( const string &Text )//Text not by const reference so that the function can be used with a 
{                               //character array as argument
	stringstream ss(Text);
	T result;
	return ss >> result ? result : 0;
}

vector<string> split(const string& s, const string& delim, const bool keep_empty = true) 
{
    vector<string> result;
    if (delim.empty()) {
        result.push_back(s);
        return result;
    }
    string::const_iterator substart = s.begin(), subend;
    while (true) {
        subend = search(substart, s.end(), delim.begin(), delim.end());
        string temp(substart, subend);
        if (keep_empty || !temp.empty()) {
            result.push_back(temp);
        }
        if (subend == s.end()) {
            break;
        }
        substart = subend + delim.size();
    }
    return result;
}

weatherarray *snowandrain = new weatherarray[2000];

double averagesnow (int size);
double averagerain (int size);

int main () 
{
  
int i = 0; 

  string line;
  ifstream myfile ("weatherdata.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
    {
		 vector<string> word = split(line,"\t");
		//copy(word.begin(), word.end(), ostream_iterator<string>(cout, "\n"));
			
		snowandrain[i].snow = StringToNumber <int> (word.at(0));
		snowandrain[i].rain = StringToNumber <int> (word.at(1));
		cout<<i<<" "<< snowandrain[i].snow<<" "<< snowandrain[i].rain<<"\n" ;
		i++;		
			
    }
    myfile.close();
	
	double snow_avg = averagesnow(i);
	cout<<"Mean snowfall: "<<snow_avg<<" cm"<<endl;
	double rain_avg = averagerain(i);
	cout<<"Mean rainfall: "<<rain_avg<<" cm"<<endl;
	//double med_snow = mediansnow(i);
	//cout<<"Median snowfall: "<<med_snow<<" cm"<<endl;
  }

  else cout << "Unable to open file"; 
  
  

  return 0;
}

double averagesnow(int size)
{
	double snowsum = 0; 
	
	for ( int n = 0; n < size; n++)
	{
		snowsum += snowandrain[n].snow;
	}
	return snowsum/size;
	
	
}

double averagerain(int size)
{
	double rainsum = 0;
	
	for (int n = 0; n < size; n++)
	{
		rainsum+= snowandrain[n].rain;
	}
	return rainsum/size;
}

void order(int&, int&);
int get_rand_int(int);

int sort()
{
	const int SIZE=1171;
	int A[SIZE];
	int i,j;

	srand( (unsigned) time(NULL));  // SEED rand() ONCE and ONLY ONCE!!!

	// Initialize the array with random numbers and print it out
	cout << "Pre-sorted array: " << endl;
	for(i=0; i<SIZE; i++)
	{
		A[i] = get_rand_int(snowandrain[i].snow);
		cout << A[i] << " ";
	}
	cout << endl << endl;


	// Sort the numbers. For a particular value of i we test all the indices, j, 
	// greater than i and call order on A[i] and A[j].  
	// After the inner loop (the "for j" loop) finishes the first time we have 
	// placed the smallest element in A[0].  After it finishes the second time the
	// second smallest element is in A[1] and so on. 

	for(i=0; i<SIZE-1; i++)			
		for(j=i+1; j<SIZE; j++)		
			order(A[i], A[j]);   

	// Print out the final array
	cout << "Sorted array: " << endl;
	for(i=0; i<SIZE; i++)
		cout << A[i] << " ";
		
	cout << endl << endl;

	return 0;
}



/*-----------------------------------------------------------------
order
	Pre:  two integers, a and b are input
	Post: a is less than or equal to b when the function ends
-------------------------------------------------------------------*/
void order(int& a, int& b)
{
int temp;

	if (a > b) {
		temp = a;
		a = b;
		b = temp;
	}	
}


/*--------------------------------------------------------------------------
get_rand_int
		Pre: 	upper is a non-negative integer
		Post: 	a random integer between 0 and upper is returned
----------------------------------------------------------------------------*/
int get_rand_int(int upper)
{
	int num;

	// in lab we got a random number by using mod like this:
	// num = rand() % (upper+1);
	
	// But the following way adds a bit more ambiguity to the numbers:
	/* int num2 = rand();
	cout << RAND_MAX << " rand_max " << endl;
	cout << (double) num2 / ((double) RAND_MAX + 1.0) << " double division " << endl;
	cout << (((double) num2 / ((double) RAND_MAX + 1.0)) * (upper+1)) << " full line" << endl;
 
		num = (int) (((double) num2 / ((double) RAND_MAX + 1.0)) * (upper+1)); 

	cout << num << " num" << endl; */
	num = (int) (((double) rand() / ((double) RAND_MAX + 1.0)) * (upper+1));

	return(num);
}
	

		






Topic archived. No new replies allowed.