Help with Arrays to Vectors!!!

Hello, I am doing a project for my computer class and we are to convert arrays to vectors and change the parameters and I need help. I have the code written but need help with the conversions. Any help is highly appreciated.


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
213
214
215
216
217
218
219
220
221
222
223
224

#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, double a[], const int size, int& number_used);
//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, double a[], int number_used);
//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.Project 6 Computing Statistics Using Vectors: Mean, Standard Deviation, Median

int index_of_smallest(const vector<double>& v, int start_index, const double a[], int number_used);
//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, double value[], const int size, int number_of_values );
//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, const int size, int number_of_values);
//Precondition: values.size() > 0
//Postcondition: median_average returns the median_average of values in
//v[0]...v[v.size()-1]
//Errors: v.size() == 0 mean_average returns error code: 2.

double standard_deviation(const vector<double>& v, const double value[]);
//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. 


const int MAX_SIZE = 1000;

int main()
{
	ifstream input;
	char name[16];
	double value[MAX_SIZE];
	double average;
	double stdDeviation;
	double median;
	int number_of_values = 0;
	cout << "Please enter a file name: " << endl;
	cin >> name;
	
	input.open(name);
	if (input.fail())
	{
		cout << "Cannot open file " << name
			<< " Aborting." << endl;
		exit(1);
	}
	fill_vector(input, value, MAX_SIZE, number_of_values);
	input.close(); //close the input file for reading

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

		//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, double value[], int size, int& number_used, vector<double>& v)
{

	while (in_file >> ws && !in_file.eof() && number_used < size)// compute average
	{
		number_used++;
		in_file >> value[number_used - 1];
	}
}
 
void sort(vector<double>& v, double a[], int number_used)
{
	//selection sort
	int index_of_next_smallest;

	for (int index = 0; index < number_used - 1; index++)
	{//Place the correct value in a[index]:
		index_of_next_smallest =
			index_of_smallest(a, index, number_used);
		swap_values(a[index], a[index_of_next_smallest]);
		//a[0] <= a[1] <=...<= a[index] are the smallest of the original array 
		//elements. The rest of the elements are in the remaining positions.
	}
}


void swap_values(double& v1, double& v2)
{
	double temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}


int index_of_smallest(const vector<double>& v, const double a[], int start_index, int number_used)
{
	double min = a[start_index];
	int index_of_min = start_index;
	for (int index = start_index + 1; index < number_used; index++)
	if (a[index] < min)
	{
		min = a[index];
		index_of_min = index;
		//min is the smallest of a[start_index] through a[index]
	}

	return index_of_min;
}
double mean_average(const vector<double>& v, const double value[], const int size, int number_of_values)
{
	double sum = 0.0;
	if (number_of_values > size)
		number_of_values = size;

	for (int i = 0; i < number_of_values; i++)
		sum = sum + value[i];
	if (number_of_values > 0)
		return sum / number_of_values;
	else if (number_of_values == 0) {
		cerr << "mean_average run-time error: number of values  = " << number_of_values << endl;
		exit(2);
	}
	else {
		cerr << "mean_average run-time error: number of values  = " << number_of_values << endl;
		exit(3);
	}
}
double median_average(const vector<double> & v, double value[], const int size, int number_of_values)
{
	if (number_of_values > size)
		number_of_values = size;

	sort(value, number_of_values); 

	if (number_of_values > 0) {
		return (number_of_values % 2) ?
			value[(number_of_values) / 2]
			: ((value[number_of_values / 2 - 1] + value[number_of_values / 2]) / 2);
	}
	else if (number_of_values == 0) {
		cerr << "median_average run-time error: number of values = " << number_of_values << endl;
		exit(2);
	}
	else {
		cerr << "median_average run-time error: number of values = " << number_of_values << endl;
		exit(3);
	}
}
double standard_deviation(const vector <double>& v, const double value[], const int size, int number_of_values)
{
	double sum = 0.0;

		double stdDeviation;

	if (number_of_values > size)
		number_of_values = size;
	double average = mean_average(value, size, number_of_values);
	for (int i = 0; i < number_of_values; i++){
		sum = sum + (value[i] - average)*(value[i] - average);
	}
	//note that these errors below should be caught by mean average
	//but cannot assume that some other programmer might not 
	//change the error processing in mean average.
	if (number_of_values > 1)
		return sqrt(sum / (number_of_values - 1));
	else if (number_of_values == 1) {
		return 0.0;
	}
	else {
		cerr << "standard_deviation run-time error: number of values =" << number_of_values <<
			endl;
		exit(2);
	}
}




anyone??
Here is what I have so far.


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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

#include <iostream> // for screen and kbd io 
#include <iomanip> 
#include <fstream>  // file io
#include <cstdlib>  // for exit()
#include <cmath>    // for sqrt
#include <vector>   // for vector
using namespace std;


void fill_vector(ifstream& in_file, vector<double>& v);

double mean_average(const vector<double>& v);









void fill_vector(ifstream& in_file, vector<double>& v);


void sort(vector<double>& v);


void swap_values(double& v1, double& v2);
//Interchanges the values of v1 and v2.

int index_of_smallest(vector<double>& v);




double median_average(const vector<double>& v);


double standard_deviation(const vector<double>& v);


const int MAX_SIZE = 1000;

int main()
{
	ifstream input;
	char name[16];
	double value[MAX_SIZE];

	//
	vector<double> v;
	//

	double average;
	double stdDeviation;
	double median;
	int number_of_values = 0;
	cout << "Please enter a file name: "
		
		<< endl;
	
	cin >> name;
	
	input.open(name);
	if (input.fail())
	{
		cout << "Cannot open file " << name
			<< " Aborting." << endl;
		exit(1);
	}


	//
	fill_vector(input, v);
	//


	number_of_values = v.size();

	//fill_array(input, value, MAX_SIZE, number_of_values);
	input.close(); //close the input file for reading

	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";

	system("PAUSE");

	return 0;
}



//
void fill_vector(ifstream& in_file, vector<double>& v)
{
	int x;

	v.clear();  //empty the vector


	while (in_file >> ws && !in_file.eof())
	{
		in_file >> x;
		v.push_back(x);
	}

	for (int i = 0; i<4; i++)
		cout << v[i] << "\n";
}




void sort(double a[], int number_used)
{
	//selection sort
	int index_of_next_smallest;

	for (int index = 0; index < number_used - 1; index++)
	{//Place the correct value in a[index]:
		index_of_next_smallest =
			index_of_smallest(a, index, number_used);
		swap_values(a[index], a[index_of_next_smallest]);
		//a[0] <= a[1] <=...<= a[index] are the smallest of the original array 
		//elements. The rest of the elements are in the remaining positions.
	}
}


void swap_values(double& v1, double& v2)
{
	double temp;
	temp = v1;
	v1 = v2;
	v2 = temp;
}


int index_of_smallest(vector<double>& v)
{
	double min = a[start_index];
	int index_of_min = start_index;
	
	for (int index = start_index + 1; index < number_used; index++)
		if (a[index] < min)
		{
			min = a[index];
			index_of_min = index;
			//min is the smallest of a[start_index] through a[index]
		}

	return index_of_min;
}



//
double mean_average(const vector<double>& v)
{
	double sum = 0.0;
	int number_of_values = v.size();
	int size = v.size();

	for (int i = 0; i < number_of_values; i++)
		sum = sum + v[i];
	if (number_of_values > 0)
	{
		cout << sum / number_of_values;
		return sum / number_of_values;
	}

	else if (number_of_values == 0) {
		cerr << "mean_average run-time error: number of values  = " << number_of_values << endl;
		exit(2);
	}
	else {
		cerr << "mean_average run-time error: number of values  = " << number_of_values << endl;
		exit(3);
	}
}
//

//double mean_average(const vector<double>& v)
//{
//	double sum = 0.0;
//	int number_of_values = v.size();
//	int size = v.size();
//
//	if (number_of_values > size)
//		number_of_values = size;
//
//	for (int i = 0; i < number_of_values; i++)
//		sum = sum + value[i];
//	if (number_of_values > 0)
//		return sum / number_of_values;
//	else if (number_of_values == 0) {
//		cerr << "mean_average run-time error: number of values  = " << number_of_values << endl;
//		exit(2);
//	}
//	else {
//		cerr << "mean_average run-time error: number of values  = " << number_of_values << endl;
//		exit(3);
//	}
//}

double median_average(const vector<double>& v)
{
	int number_of_values = v.size();
	int size = v.size(); 
	
	//int value = l;


	
	if (number_of_values > size)
		number_of_values = size;

	sort(//value, number_of_values);

	if (number_of_values > 0) {
		return (number_of_values % 2) ?
			value[(number_of_values) / 2]
			: ((value[number_of_values / 2 - 1] + value[number_of_values / 2]) / 2);
	}
	else if (number_of_values == 0) {
		cerr << "median_average run-time error: number of values = " << number_of_values << endl;
		exit(2);
	}
	else {
		cerr << "median_average run-time error: number of values = " << number_of_values << endl;
		exit(3);
	}
}
double standard_deviation(const vector<double>& v)
{
	int number_of_values = v.size();
	int size = v.size();
	double sum = 0.0;

	//	double stdDeviation;

	if (number_of_values > size)
		number_of_values = size;
	double average = mean_average(value, size, number_of_values);
	for (int i = 0; i < number_of_values; i++){
		sum = sum + (value[i] - average)*(value[i] - average);
	}
	//note that these errors below should be caught by mean average
	//but cannot assume that some other programmer might not 
	//change the error processing in mean average.
	if (number_of_values > 1)
		return sqrt(sum / (number_of_values - 1));
	else if (number_of_values == 1) {
		return 0.0;
	}
	else {
		cerr << "standard_deviation run-time error: number of values =" << number_of_values <<
			endl;
		exit(2);
	}
}
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <sstream>

std::vector<double> fill_vector( std::istream& stm ) // return by value
{
    std::vector<double> vec ;
    double d ;
    while( stm >> d ) /*if( !std::isnan(d) )*/ vec.push_back(d) ;
    return vec ;
}

// invariant: sequence does not contain NaNs
void selection_sort( std::vector<double>& seq, std::size_t from_pos = 0 )
{
    if( from_pos < seq.size() )
    {
        const auto iter_from =  seq.begin() + from_pos ;

        // http://en.cppreference.com/w/cpp/algorithm/min_element
        const auto iter_smallest = std::min_element( iter_from, seq.end() ) ;

        // http://en.cppreference.com/w/cpp/algorithm/iter_swap
        std::iter_swap( iter_from, iter_smallest ) ; // std::swap( *iter_from, *iter_smallest ) ;

        selection_sort( seq, from_pos+1 ) ;
    }
}

// etc.

int main()
{
    std::istringstream stm( "3 9 8 2 9 1 6 4 8 5 7 4 3 6 6 3 9 2" ) ;

    auto seq = fill_vector(stm) ;
    const std::size_t number_used = seq.size() ;
    std::cout << number_used << " numbers were read\n" ;

    selection_sort(seq) ;
    for( double d : seq ) std::cout << d << ' ' ;
}

http://coliru.stacked-crooked.com/a/136555d8ddff80b5
You're in my CS 220. I know that for a fact. I haven't turned this in yet because I can't figure it out for the life of me. I'm awful at coding, I don't really think I'm in the right major. Anyways, I'm getting a B somehow in his course. I guess I can be hopeful.

JLBorges, nothing that you put even looks similar to what we're doing. However, I appreciate your attempt to help.
Topic archived. No new replies allowed.