C++ Sorting the “percentage” of two paired integer arrays

Write your question here.
I have a program with 2 "paired" integer arrays newNumerator[ ], and newDenominator[ ] which both have 9 integers in them. I wrote a function that sorts them in ascending order (lowest percentage or 'ratio' to highest), however it outputs the elements in the same order as they were before, and doesn't sort it at all. Here is the code, and sortData is the function that is (supposed) to sort it.
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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

typedef int arrayType[];

void readData(int numerator[], int denominator[], int size);
void reportData(arrayType, arrayType, int);
void reportOverall(int numerator[], int denominator[], int size);
void cleanData(int* numerator, int* denominator, const int size, int *newNumerator, int *newDenominator, int &newSize);
void reportMin(int newNumerator[], int newDenominator[], int newSize);
void sortData(int newNumerator[], int newDenominator[], int newSize);
void hrule();
int main()
{
    const int size = 12;
    int numerator[size];
    int denominator[size];
    int newSize = 12;
    int newNumerator[newSize];
    int newDenominator[newSize];
    
    
    cout << fixed << showpoint << setprecision(1);
    
    readData(numerator, denominator, size);
    reportData(numerator, denominator, size);
    reportOverall(numerator, denominator, size);
    
    hrule();
    
    cleanData(numerator, denominator, size, newNumerator, newDenominator, newSize);
    cout << "There are " << newSize << " scores that are not bonuses:\n";
    reportData(newNumerator, newDenominator, newSize);
    reportMin(newNumerator, newDenominator, newSize);
    hrule();
    sortData(newNumerator, newDenominator, newSize);
    cout << "The scores from highest to lowest are: " << endl;
    reportData(newNumerator, newDenominator, newSize);
    
    system("pause");
    return 0;
}

void readData(int numerator[], int denominator[], int size)
{
    ifstream dataIn;
    dataIn.open("data.txt");
    if(!dataIn)
    {
        cout << "File not found\n";
        system("pause");
        exit(1);
    }
    
    int count;
    for(count = 0; count < size; count++)
    {
        dataIn >> numerator[count];
    }
    for (count = 0; count < size; count++)
    {
        dataIn >> denominator[count];
    }
    
    dataIn.close();
}

void reportData(arrayType numerator, arrayType denominator, int size)
{
    int count;
    for (count = 0; count < size; count++)
    {
        if (denominator[count] == 0)
        {
            cout << "Score " << (count + 1) << " is " << numerator[count] << "/" << denominator[count] << " = " << "Bonus points!\n";
        }
        else
        {
            double percent = 100.0 * static_cast<double>(numerator[count]) / denominator[count];
            cout << "Score " << (count + 1) << " is " << numerator[count] << "/" << denominator[count] << " = " << (percent) << "%\n";
        }
    }
}

void reportOverall(int numerator[], int denominator[], int size)
{
    int count;
    int totalNumerator = 0.0;
    int totalDenominator = 0.0;
    
    for (count = 0; count < size; count++)
    {
        totalNumerator += numerator[count];
    }
    for (count = 0; count < size; count++)
    {
        totalDenominator += denominator[count];
    }
    
    double overallPercent = 100.0 * static_cast<double>(totalNumerator) / (totalDenominator);
    cout << "Total Points Earned (numerators): " << totalNumerator << endl;
    cout << "Total Points Possible (denominators): " << totalDenominator << endl;
    cout << "Overall Grade: " << overallPercent << "%\n";
}

void cleanData(int* numerator, int* denominator, const int size, int *newNumerator, int *newDenominator, int &newSize)
{
    int count;
    int count2 = 0;
    
    for(count = 0; count < size; count++)
    {
        if(denominator[count] != 0)
        {
            newNumerator[count2] = numerator[count];
            newDenominator[count2] = denominator[count];
            count2++;
        }
        else if(denominator[count] == 0)
        {
            newSize--;
        }
    }
    
}

void reportMin(int newNumerator[], int newDenominator[], int newSize)
{
    double minimum;
    int count;
    int location = 0;
    double quotient;
    for(count = 0; count < newSize; count++)
    {
        quotient = 100.0 * static_cast<double>(newNumerator[count]) / newDenominator[count];
        if (count == 0 || quotient < minimum)
        {
            minimum = quotient;
            location = count;
        }
    }
    cout << "The lowest earned percentage grade is " << newNumerator[location] << "/" << newDenominator[location] << " = " << minimum << "%\n";
}

void sortData(int newNumerator[], int newDenominator[], int newSize)
{
    int temp1;
    int temp2;
    bool swap;
    int count = 0;
    double percentageLeft = 100.0 * static_cast<double>(newNumerator[count]) / newDenominator[count];
    double percentageRight = 100.0 * static_cast<double>(newNumerator[count + 1]) / newDenominator[count + 1];
    
    do
    {  swap = false;
        for(count = 0; count < (newSize - 1); count++)
        {
            if(percentageLeft > percentageRight)
            {
                temp1 = newNumerator[count];
                newNumerator[count] = newNumerator[count + 1];
                newNumerator[count + 1] = temp1;
                
                temp2 = newDenominator[count];
                newDenominator[count] = newDenominator[count + 1];
                newDenominator[count + 1] = temp2;
                
                swap = true;
            }
        }
    } while (swap);
}

void hrule()
{
    cout << "\n****************************************\n\n";
}


Data File-
1
2
72 49 23 5 9 10 6 16 26 54 14 55
75 50 25 0 10 0 0 20 30 55 15 60
Last edited on
Have you tried stepping through it with a debugger, to see what's happening?
closed account (D80DSL3A)
percentageLeft and percentageRight are calculated only once, where they're declared.
They should be recalculated following line 163.
Last edited on
Well, yes. I was hoping that, by using a debugger, the OP would learn how to see that for themself.

Teach a man to fish, and all that...
Hi

Sorry off topic, but I'm trying to get hold of fun2code, I sent him a pm re: an old forum post about BigNums, and some work he'd done on trig functions using BigFloat.

Cheers!
Topic archived. No new replies allowed.