### 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`:

 ``123456`` ``````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:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include #include #include 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!

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include #include #include #include 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 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::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.