sort with pointer new float[M]

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
// Test Averaging.

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(){
    float* ScorePointer;
    int M;
    float ValueHolder;
    int PlaceHolder;
    int n1, n2;
    float total;

    // Array creation.
    cout << "Enter number of tests to score." << endl;
    cin >> M;
    cout << endl;
    ScorePointer = new float[M];
    if (ScorePointer == 0){
        cout << "Invalid." << endl;
        exit(0);
    }

    // Test score input.
    for (n1 = 0; n1 < M; n1++){
        cout << "Enter score for test " << n1 + 1 << endl;
        cin >> ScorePointer[n1];
        if (ScorePointer[n1] < 0){
            cout << "Invalid test score entry." << endl;
            exit(0);
        }
    }

    // Sorting ascending order.
    if (M > 1){
        for (n1 = 0; n1 < M -1; n1++){
            ValueHolder = ScorePointer[n1];
            PlaceHolder = n1;
            for (n2 = 1; n2 < M; n2++){
                if (ScorePointer[n2] < ValueHolder){
                    ValueHolder = ScorePointer[n2];
                    PlaceHolder = n2;
                }
            }
            ScorePointer[PlaceHolder] = ScorePointer[n1];
            ScorePointer[n1] = ValueHolder;
        }
    }
    cout << endl << "Test score list :" << endl;
    for (n1 = 0; n1 < M; n1++){
        cout << ScorePointer[n1] << endl;
    }

    // Average all scores.
    total = 0;
    for (n1 = 0; n1 < M; n1++){
        total += ScorePointer[n1];
    }
    cout << endl << "The average of all scores is " << total / M << endl;

    delete [] ScorePointer;
    return 0;
}


This seems simple enough, but the sort is not working. Am I making a simple mistake I do not see or do I have to treat sorting differently after dynamically allocating an array?

I am to:
1. Create an array by dynamic allocation, and store in it test scores.
2. Sort the test scores.
3. Calculate the average score.

Everything works except that the sort does not happen correctly.
Last edited on
Can you tell us your example expected program output that you should have?
Tell more the problem that currently makes you sad.
The output makes me sad because for example if I say I want to enter 5 test scores with scores of 1, 2, 3, 4, 5 (already in ascending order, but that doesn't matter really), the program sorts these into 1, 3, 4, 2, 5. Actually it seems to always order the numbers like that so the first number is always the lowest while the second to last number is always next lowest, then the second number in the output list is the third lowest and so on. I don't see why it should be like that but that is what happens.

... Okay, not always in that order I guess. It still doesn't sort correctly.
Last edited on
I still haven't figured this out. My experiments with pointers suggest this should work. I am sorry I just realized there is an output format. Sorry I didn't use it earlier. Again, the thing that should happen which does not is that after entering the test scores, the program should sort them from lowest to highest. All it seems to do is shuffle them which really would be cool if that was what I was trying to do, but alas !cool.

Sample output.

Sample 1:
Enter number of tests to score.
5

Enter score for test 1
3
Enter score for test 2
4
Enter score for test 3
7
Enter score for test 4
5
Enter score for test 5
1

Test score list :
1
7
5
3
4

The average of all scores is 4


Sample 2:
Enter number of tests to score.
5

Enter score for test 1
33
Enter score for test 2
75
Enter score for test 3
99
Enter score for test 4
865
Enter score for test 5
101

Test score list :
33
99
865
75
101

The average of all scores is 234.6


Sample 3:
Enter number of tests to score.
9

Enter score for test 1
204
Enter score for test 2
333
Enter score for test 3
298
Enter score for test 4
102
Enter score for test 5
336
Enter score for test 6
12
Enter score for test 7
1
Enter score for test 8
992
Enter score for test 9
93

Test score list :
1
298
102
336
333
204
992
12
93

The average of all scores is 263.444


Again, what I was expecting to get was:

Desired sample one output:
blah, blah, blah
Test score list :
1
3
4
5
7
blah blah average blah


Desired sample two output:
blah blah blah
Test score list:
33
75
99
101
865
blah blah average blah


Desired sample three output:
blah blah blah
Test score list:
1
12
93
102
204
298
333
336
992
blah blah average blah
Last edited on
If think that the 2nd loop (with n2) should start at n1+1 and not 1.
Last edited on
OMG! Thank you so much! I've been depressed over this problem for days questioning whether I really understood anything. Does 1+1 really equal 2? Does it?!? Then why doesn't the computer believe you!
Topic archived. No new replies allowed.