Selection sort a dynamically allocated array

I can't figure out why my sorting is not working. The array reads in the values correctly before the selection sort function call, but when the function is called, it screws everything up. P.S. sorry if my code looks confusing. Any help is appreciated..thanks
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
 #include <iostream>
using namespace std;

class TestScores
{
private:
    int numTestScores;
    double *scores;
public:
    TestScores()
    { numTestScores = 1;
        scores = new double [numTestScores];
        setTestScores();
    }
    TestScores(int x)
    {
        if (x <= 0)
            x = 1;
        else
            numTestScores = x;
        scores = new double [numTestScores];
        setTestScores();
    }
    void setTestScores();
    void selectionSort();
    double average();
    void displayScores();
    
};

void TestScores::setTestScores()
{
    
    for (int i= 0; i<numTestScores; i++)
    {
        do
        {
            cout << "What is score " << i+1 <<": ";
            cin >> *(scores + i);
            if (*(scores + i) < 0 || *(scores + i) > 10)
                cout << "Score must be 0 - 10\n";
            
            
        }
        while (*(scores + i) < 0 || *(scores + i) > 10);
        
        
    }
}
double TestScores::average()
{
    double sum = 0;
    
    for (int i = 0; i < numTestScores; i++)
        sum += *(scores+i);
    return sum/numTestScores;
}
void TestScores::displayScores()
{
    cout << "Below are all the test scores.\n";
    for (int i=0; i<numTestScores; i++)
        cout << "Score " << i+1 << ": " << *(scores+i) << endl;
    
}
void TestScores::selectionSort()
{
    int startScan, minIndex;
    double minValue;
    
    for (startScan=0; startScan<(numTestScores-1); startScan++)
    {
        minIndex =startScan;
        minValue=*(scores+startScan);
        for (int index=startScan+1; index<numTestScores; index++)
        {
            if (*(scores+index)<minValue)
            {
                minValue=*(scores+index);
                minIndex=index;
            }
            *(scores+minIndex)=*(scores+startScan);
            *(scores+startScan)=minValue;
        }
    }
}


int main()
{
    int testScores;
    
    cout << "Enter the amount of test scores: ";
    cin >> testScores;
    TestScores set1(testScores);
    
    set1.selectionSort();
    set1.displayScores();
    cout << "Average of all scores: " << set1.average() << endl;
    system("pause");
    return 0;
}
move swapping part (lines 81-82) to the outer loop (between lines 83 and 84)

Edit: another implementation of selection sort:
1
2
3
4
5
6
7
8
9
#include <algorithm>
//...
double* array_begin = scores;
double* array_end = array_begin + numTestScores;
double* minimum;
while(minimum = std::min_element(array_begin, array_end) != array_end) {
    std::iter_swap(array_begin, minimum);
    ++array_begin;
}
Last edited on
Thank you, it worked!
Topic archived. No new replies allowed.