Using same Index function for different Sort functions

Hello all!

I created a small practice program for practicing sorting data that is read from a file. The problem I ran into is that when I want to sort a different, specific column (then 2nd column in this case), I get the cannot convert data type error.

My goal is to be able to find the median, min, and max of only that column. Now, I understand that it's a data type mismatch issue, but I was wondering if there was a way to get around it? Because otherwise I would have to create another minIndex function which seems rather inefficient.

Here is the code:

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void readScore(ifstream & fin, double & x, int & y, string & z);

double median(double arr[], int n);

void swap(int & a, int & b);
int minIndex(double k[], int size, int startIndex);
void selectionSortAll(double k[], int l[], int size);
void selectionSortY(int l[], int size);

struct XYZ
{
    double varx[4];
    int vary[4];
    string varz[4];
};

int main()
{
    double x;
    int y;
    string z;

    ofstream fout;
    ifstream fin;

    fin.open("struct fstream.txt");

    XYZ calcX;
    XYZ calcY;
    XYZ calcZ;

    double scoreSumX = 0;
    int scoreSumY = 0;
    for (int i = 0; i < 4; i++)
    {
        readScore(fin, x, y, z);
        calcY.vary[i] = y;
        calcX.varx[i] = x;
        calcZ.varz[i] = z;
        cout << calcX.varx[i] << " ";
        cout << calcY.vary[i] << " ";
        cout << calcZ.varz[i] << endl;
        /*scoreSumX += calcX.varx[i];
        scoreSumY += calcY.vary[i];
        cout << "Current sum: " << scoreSumX << endl;
        if (i >1)
        {
            cout << "Current average: " << scoreSumX / (i + 1) << endl;
        }*/
    }

    cout << "" << endl;

    selectionSortAll(calcX.varx, calcY.vary, 4);

    for (int i = 0; i < 4; i++) {
        cout << calcX.varx[i] << " ";
        cout << calcY.vary[i] << " ";
        cout << calcZ.varz[i] << endl;
    }

    cout << median(calcX.varx, 4);

    fin.close();
    fout.close();

    return 0;
}

void readScore (ifstream & fin, double & x, int & y, string & z)
{
    fin >> x >> y >> z;
}

double median(double arr[], int n)
{
    if (n % 2 == 0)
        return arr[n / 2];
    else
        return arr[n / 2];
}

void swap(int & a, int & b) {
    int temp = a;
    a = b;
    b = temp;
}

int minIndex(double k[], int size, int startIndex) {
    int minI = startIndex;
    for (int i = startIndex; i < size; i++) {
        if (k[i] < k[minI]) {
            minI = i;
        }
    }
    return minI;
}

void selectionSortAll(double k[], int l[], int size) {
    int minI;

    for (int startIndex = 0; startIndex < size; startIndex++) {
        minI = minIndex(k, size, startIndex);
        swap(k[startIndex], k[minI]);
        swap(l[startIndex], l[minI]);
    }
}

void selectionSortY(int l[], int size) {
    int minI;

    for (int startIndex = 0; startIndex < size; startIndex++) {
        minI = minIndex(l, size, startIndex);
        swap(l[startIndex], l[minI]);
    }
}


The error occurs with the very last function, "selectionSortY", because the first argument "l" in this case is an int but the first argument in the function "minIndex" is a double. Would I have to just create another minIndex function for indexing different data types?

Oh and for reference, the contents of the input file are as follows:

1
2
3
4
634 423 qwerty
115 935 jkl
968 156 asdf
439 593 iopu


Just to be clear, the program works just fine without the "selectionSortY" function. "selectionSortAll" sorts the first two columns as intended.

Additionally, I'm new to programming, so please bear with me if I'm a bit slow to catch on or if I just straight up don't know what something is. Also for the actual assignment (much bigger than this so I didn't post it here), I'm somewhat limited on what sort of code I'm allowed to use since this is the first semester of a beginner course. We were more or less told to work with the libraries I already listed at the top of my code.

Thanks for your time and any help would be greatly appreciated =)
> Would I have to just create another minIndex function for indexing different data types?

Overloading the function is a possibility:
1
2
int minIndex( const double k[], int size, int startIndex ) ; // make it const correct
int minIndex( const    int k[], int size, int startIndex ) ; //  const correct 


Making the functions (compile-time) polymorphic is better:
1
2
3
template < typename T > int minIndex( const T k[], int size, int startIndex ) ;
template < typename T > void selectionSortY( T array[], int size ) ;
// etc. 


Tip: avoid names like l. l7 - is this ell-seven or is it seventeen?
The template one works, thanks! And yeah I was lazy to just use l (L) as a variable but for the real project I'm definitely not doing that.
Topic archived. No new replies allowed.