Sorting 2 arrays

complete
Last edited on
Im using the 2d array to read in 2 numbers and the final spot is for when the total is calculated for each person. This is the file. The first array reads the name. The 2d array reads both of those numbers.

Johnson 60 12.50
Aniston 65 13.25
Cooper 50 14.50
Gupta 70 14.75
Blair 55 10.50
Clark 40 18.75
Kennedy 45 20.50
Bronson 60 20.00
Sunny 65 18.75
Smith 30 9.75
http://www.cplusplus.com/forum/articles/17108/

Use a better data structure, like a struct to store stats. Show me a portion of the file so I can help you better.

Edit: thank you. I thought stats held much more complicated data, but quite frankly, you don't need a 2D array. I strongly recommend you use a struct:

1
2
3
4
5
6
struct Person
{
    string name;
    int firstNumber;
    double secondNumber;
};
Last edited on
honestly, I would use a struct. Unfortunately my teacher wants it done without structs for practice purposes
Your fundamental problem is in your sort function: you think you're swapping the rows of your 2D array, but what you're actually doing is swapping out-of-bound values stats[index][3] (CLM is equal to 3, which is the size of the column, not a valid subscript).

Here's your fixed code. Peruse 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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int ROW = 10, CLM = 3;

void readfile(string names[], double stats[][CLM], int row);
void sort(string names[], double stats[][CLM], int row);


int main()
{
    int i, j;
    string names[10];
    double stats[ROW][CLM];
    
    readfile(names, stats, ROW);
    sort(names, stats, ROW);

    for (i=0; i < ROW; i++)
    {
        cout << names[i] << endl;
        cout << stats[i][0] << endl;
        cout << stats[i][1] << endl;
        cout << stats[i][2] << endl << endl;
    }

    cin.get();
    return 0;
}

void readfile(string names[], double stats[][CLM], int row)
{
    int i, j;

    ifstream inFile;

    inFile.open("p1data.txt");

    if (!inFile)
    {
        cout << "Cannot open input file. "
             << "Program terminates." << endl;
    }
    else
        cout << "Open Successful\n\n";

    for (i = 0; i < row; ++i)
    {
        inFile >> names[i];

        for ( j = 0; j < CLM - 1; ++j)
        {
            inFile >> stats[i][j];
        }

        stats[i][CLM - 1] = 0;
    }
        
    inFile.close();
}

void sort(string names[], double stats[][CLM], int row)
{
    string temp;
    int iteration;
    int index;
    double temp2, temp3, temp4;

    for (iteration = 1; iteration < row; iteration++)
    {
        for (index = 0; index < row -iteration; index++)
        {
            if (names[index] > names[index + 1])
            {
                temp = names[index];
                temp2 = stats[index][0];
                temp3 = stats[index][1];
                temp4 = stats[index][2];

                names[index] = names[index + 1];
                stats[index][0] = stats[index + 1][0];
                stats[index][1] = stats[index + 1][1];
                stats[index][2] = stats[index + 1][2];

                names[index + 1] = temp;
                stats[index + 1][0] = temp2;
                stats[index + 1][1] = temp3;
                stats[index + 1][2] = temp4;
            }
        }
    }
}
So if i understand this correctly, by me putting CLM it was swapping values that didn't even exist?
That's correct.

Here's a more pragmatic approach. Frankly, it's much better since it exposes you to the standard template library, lambdas, iterators, pointers, constructors, etc. Schools need to do a better job at teaching!

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>

struct record
{
    std::string record;
    int number_1;
    double number_2, sum;

    record(std::string _name, int _number_1, double _number_2)
    {
        record = _name;
        number_1 = _number_1;
        number_2 = _number_2;
        sum = number_1 + number_2;
    }
};

int main()
{
    std::vector<record*> vector;

    std::ifstream file;
    file.open("input.txt");

    if (!file)
    {
        std::cout << "The file does not exist.";
        return 1;
    }

    std::string record;
    int number_1;
    double number_2, sum;

    file >> record >> number_1 >> number_2;

    while (file)
    {
        vector.push_back(new record(record, number_1, number_2));
        file >> record >> number_1 >> number_2;
    }

    file.close();

    std::sort(vector.begin(), vector.end(), [](record* a, record* b) {
        return (*a).record < (*b).record;
    });

    for (std::vector<record*>::iterator it = vector.begin(); it < vector.end(); ++it)
    {
        std::cout << (*it)->record << std::endl;
        std::cout << (*it)->number_1 << std::endl;
        std::cout << (*it)->number_2 << std::endl;
        std::cout << (*it)->sum << std::endl << std::endl;
    }

    return 0;
}
Topic archived. No new replies allowed.