selection sort in array sorts correctly except for first 2 values

So i am in my first programming class, i am writing a program that brings in data values from a text file, stores it in an array of size 100, then performs actions including mean, standard deviation, min and max

the minimum is an issue for me, as the sorting works perfectly except for the first 2 values.
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
 #include <iostream>
#include <iomanip>
#include <string>
#include <fstream>


using namespace std;

void sort(double[], int);
void swap(double[], const int, const int);

int main()
{
	const int MAX = 100;
	double values1[MAX], values2[MAX], tempval, sum = 0, sum2 = 0, mean, standev;
	int numbers, count = 0, n = 0;
	string filename;
	ifstream  InStream;
	cout << "Ryan Junge - Computer Science 1 - Section 0111 - Program 6" << endl << endl
		<< "This program will produce statistics for at most 100 real numbers" << endl << endl
		<< "Enter the name of the data file : ";
	getline(cin, filename);
	InStream.open(filename.c_str());
	if (!InStream)
		cout << "\n\nThe file could not be opened";
	else
	{
		InStream >> tempval;
		while (!InStream.eof() && InStream && count < MAX)
		{
			values1[count] = tempval;
			if (count < 99)
			{
				count = count + 1;
				InStream >> tempval;
			}
			
			
		}
		if (InStream.eof())
		{
			values1[count] = tempval;
		}
		
		if (!InStream.eof())
		{
			cout << "\n\nThe file contains more values than could be processed.";
		}
		n = count + 1;
		count = 0;
		while (count < n)
		{
			sum = sum + values1[count];
			sum2 = sum2 + (values1[count] * values1[count]);
			count = count + 1;
		}
		mean = sum / n;
		standev = sqrt(sum2 / n - (mean * mean));
		cout << endl << n << " numbers were stored in the array";
		cout << "\n\nthe mean is : " << mean;
		cout << "\n\nthe standard deviation is : " << standev;
		
	
		for (int j = 0; j < count; j++)
		{
			values2[j] = values1[j];
			
		}
		sort(values2, count);
		cout << "\n\nthe min value is: " << values2[0] << "\n\nthe max value is:" << values2[n-1];
		cout << "\n\nthe first six values are: ";
		cout << " " << values2[0] << " " << values2[1] << " " << values2[2] << " " << values2[3] << " " << values2[4] << " " << values2[5];
	
		
		
		
		
	
	}
	

	cin.ignore(128, '\n');
	cin.get();
	return 0;
}
void sort(double values[], int last)
{
	int best;

	for (int place = 0; place < last - 1; place++)
	{
		best = place;
		for (int i = place + 1; i < last; i++)
		{
			if (values[i] < values[best])
				best = i;
			swap(values, place, best);
		}
	}
}
void swap(double nums[], const int a, const int b)
{
	double temp;
	temp = nums[a];
	nums[a] = nums[b];
	nums[b] = temp;
}



output is here :

Enter name of the data file : numbers.dat

100 numbers were stored in the array

the mean is : 463.88

the standard deviation is : 283.386

the min values is : 18

the max values is : 990

the first six values are: 18 2 38 38 43 56





I figured i could manually swap the first two values, but that wouldnt actually fix the error in my code. I can't figure out what is wrong in my sorting/swapping of values that would have the first 2 values in each others' spot.
Last edited on
The comparison/swap is incorrect. I'm not sure why you have a "best" in there. Just get rid of it.
1
2
	if (values[i] < values[place])
		swap(values, i, place);

Topic archived. No new replies allowed.