Trouble Sorting & Calculating the Median of a Vector

I am trying to to pull data from a file into a vector, then calculate different statistics from said vector. I've got everything working great except for one part. The program requires the median be calculated, which means I need to sort the data in the vector. However I can't do a quick easy sort, I'm required to use the functions swap(double, double), sort(vector), and index_of_smallest.

I can't for the life of me figure out how to get these all working in sync to calculate the median(not to mention my algorithms appear to be wrong as I return nothing but 0's for the median value.)

Where am I going wrong? I have a feeling I'm either not sorting right, or the index_of_smallest function is supposed to be doing something I don't realize.

Thanks guys!

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
#include <iostream> // for screen and kbd io 
#include <iomanip> 
#include <fstream>  // file io
#include <cstdlib>  // for exit()
#include <cmath>    // for sqrt
#include <string>
#include <vector>

using namespace std;

void fill_vector(ifstream& in_file,vector<double>& v);
//Precondition: in_file is open
//Postcondition: original vector v is cleared (all elements are deleted)
//and v.size() set to 0
//v[0] through v[v.size()-1] have been filled with the values read sequentially
//from the text file opened (represented by in_file).
//The values are filled starting at index 0.
//When eof of file is reached, the number of value read must be equal to v.size().
void sort(vector<double>& v);
//Algorithm to use : Selection Sort.
//Postcondition: The values of v[0] through v[v.size()-1] have//been rearranged so that v[0] <= v[1] <= ... <= v[v.size()-1].
void swap_values(double& v1,double& v2);
//Interchanges the values of v1 and v2.


int index_of_smallest(const vector<double>& v, int start_index);
//Precondition: 0 <= start_index < v.size()-1.
//Returns the index i such that v[i] is the smallest of the values
//v[start_index], v[start_index + 1], ..., v[v.size()-1].
double mean_average(const vector<double>& v);
//Precondition: v.size() > 0
//Postcondition: mean_avg returns the mean average of values in v[0]...v[v.size()-1]
//Errors: values.size() == 0 mean_average returns error code: 2.
double median_average(const vector<double>& v);
//Precondition: values.size() > 0
//Postcondition: median_average returns the median_average of values inv[0]...v[v.size()-1]
//Errors: v.size() == 0 mean_average returns error code: 2.
double standard_deviation(const vector<double>& v);
//Precondition: v.size() > 0
//Postcondition: standard_deviation returns the standard_deviation of values in	v[0]...v[v.size()-	1]
//Errors: v.size() == 0 standard_deviation returns error code: 2. 
int main()
{
	ifstream in_file;
	string name;
	double average;
	double stdDeviation;
	double median;
	vector<double> v;

	int number_of_values = 0;
	cout << "Enter a file name. "<< endl;
	cin >> name;
	in_file.open(name);
	if (in_file.fail())	{
		cout << "Cannot open file " << name
			<< " Aborting." << endl;
		exit(1);
	}
	
	fill_vector(in_file, v);
	in_file.close(); //close the input file for reading
	number_of_values = v.size();

	if (number_of_values > 0) {
		//compute stats	
		average = mean_average(v);
		median = median_average(v);
		stdDeviation = standard_deviation(v);

		//output format specifications
		//cout.setf(ios::showpoint);
		cout.setf(ios::fixed);
		cout.precision(6);

		cout << "Average of "
			<< number_of_values << ((number_of_values == 1) ? " number" : " numbers") << " is "
			<< average
			<< endl;
		cout << "Standard Deviation of "
			<< number_of_values << ((number_of_values == 1) ? " number" : " numbers") << " is "
			<<stdDeviation << endl;
		cout << "Median of "
			<< number_of_values << ((number_of_values == 1) ? " number" : " numbers") << " is "
			<< median << endl;
	}
	else
		cout << "File " << name << " is an empty file.\n";

	return 0;
}

void fill_vector(ifstream& in_file, vector<double>& v){   //This function is good to go!
	v.clear();
		int i;
		while (in_file >> ws && !in_file.eof() ){
			in_file >> i;
			v.push_back(i);
		} 
}

void sort(vector<double>& v){
	//selection sort
	double j = 0;
	
	double temp = 0;
	double iterationIndex = 0;
	
	sort(v);
	for (iterationIndex = 0; iterationIndex < v.size() - 1; iterationIndex--){
		for (j = iterationIndex + 1; j = v.size() - 1; ++j){
				if (v.at(j) < (v.at(iterationIndex) - 1)) {
				swap_values(j, iterationIndex);
			}
		}
		} 
		
	}


void swap_values(double& v1, double& v2){
	/*vector<double> v;*/
	double temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}


int index_of_smallest(const vector<double>& v, int start_index){
	if (0 <= start_index < v.size() - 1){	
	double min = v.at(start_index);
	int index_of_min = start_index;
	for (int index = start_index + 1; index < v.size(); index++){
		if (v.at(start_index) < min){
			min = v.at(start_index);
			index_of_min = index;
		}
	}
	
	return min;
}
	else {
		return 0;
	}
}

double mean_average(const vector<double>& v){   //Good to go!
	double sum = 0.0;
	double mean = 0.0;
	if (v.size() > 0){
		for (int i = 0; i < v.size(); ++i){
			sum = sum + v.at(i);

		}
		mean = (sum / v.size());
		return mean;
	}
	else{
		return 0;
	}
}
	double median_average(const vector<double>& v){
		double sortedMedian = 0.0;
		if (v.size() > 0){
			if ((v.size() % 2) == 0){
				sortedMedian = ((v.size() / 2) + ((v.size() / 2) - 1)) / 2.0;
			}
		}
			else {
				sortedMedian = v.at(v.size() / 2);
			}
		return sortedMedian;
	}

	double standard_deviation(const vector<double>& v){ //Good to go!
			if (v.size() == 0.0){
			return 0.0;
		}
			if (v.size() == 1.0){
				return 0.0;
			}
		double mean = mean_average(v);
		double sum = 0.0;
		double sqrtdiffsum = 0.0;
		for (int i = 0; i < v.size(); ++i){
			double difference = v.at(i) - mean;
			sqrtdiffsum += difference * difference;
		}
			double variance = sqrtdiffsum / (v.size() - 1);
			return sqrt(variance);
		}
	
Explain lines 104-107.
Explain line 109.
What are you swapping?

What is semantically/logically wrong here:
1
2
3
4
int index_of_smallest( const vector<double>& v, int start_index ) {
  double min = v.at(start_index);
  return min;
}
Topic archived. No new replies allowed.