1. ## Sorting Question

I have too sort the iD numbers of an array in order. with a linearsort, but i have to keep the iD and GPAs matched up, because i have an array for ID and an array for the GPAs, so if they IDS move, then they wouldnt match the IDS. how can i sort them so they stick together? So the IDS get sorted and the GPAs stay with thier respective ids?

2. make a struct (or class) of ID and GPA
sort an array of that struct instead.

Alternatively, use std:: pair.

3. Originally Posted by cyberfish
make a struct (or class) of ID and GPA
sort an array of that struct instead.

Alternatively, use std:: pair.
Yeah im new in C++, and have no idea what that is, but my prof. said i need to stick with what i know and use a forloop and the linear arrays

4. Have you learned about structs and classes?

5. then, do it as you would on the array, but swap the elements in both arrays where you would have swapped one

6. Originally Posted by cyberfish
then, do it as you would on the array, but swap the elements in both arrays where you would have swapped one
Code:
```void lsort(int &n, int val[], double gp[]){

int temp,temp1;

for(int pass = 0; pass < n - 1; pass++)
for(int cand = pass + 1; cand < n; cand++)
if(val[pass] > val[cand]){
temp=val[pass];
val[pass]=val[cand];
gp[pass]=gp[cand];
val[cand]=temp;
gp[pass]=gp[cand];
}
return;
}```
yeah thats what i did but when i print the new array, in the output i get this::

ID GPA
123 1.9
124 1.9
225 1.1
632 3.2

THE ID GOES IN ORDER BUT THE 124 ID, gets the 123 id for some reason?
THE INPUT FROM FILE IS THIS
4
124 2.6
123 1.9
225 1.1
632 3.2

7. You have sucessfully implemented a sorting algorithm. It's what I'd call SimpleSort.

You are swapping items in the val array using a temporary just fine, but then you go and copy an item in the gp array over top of another item in the gp array (twice in fact), wiping that item out. What made you think that the process for swapping items in one array would be different to the process of swapping items in the other array?
You could make it at least slightly easier for yourself and use std::swap instead.

You could also use std::sort, but you'd probably need quite a peculiar assignment operator in order to swap items in both arrays. You'd be best to make a struct that holds both pieces of data if possible.

8. Originally Posted by iMalc
You have sucessfully implemented a sorting algorithm. It's what I'd call SimpleSort.

You are swapping items in the val array using a temporary just fine, but then you go and copy an item in the gp array over top of another item in the gp array (twice in fact), wiping that item out. What made you think that the process for swapping items in one array would be different to the process of swapping items in the other array?
You could make it at least slightly easier for yourself and use std::swap instead.

You could also use std::sort, but you'd probably need quite a peculiar assignment operator in order to swap items in both arrays. You'd be best to make a struct that holds both pieces of data if possible.
I cant use any structure or that std:: because she han't taught it yet, i have to use what i have there. So how can i fix it, that it get the right values for GPA?

9. Originally Posted by Rkukulski
I cant use any structure or that std:: because she han't taught it yet, i have to use what i have there. So how can i fix it, that it get the right values for GPA?
What you want to do is detect the ordering by comparing the data in the val array "check",
you swap the data for the items 'pass' and 'cond' in the val array "check",
and the items 'pass' and 'cond' in the gp array "failure".
You already worked out that you need a temporary item to swap items in the first array. You need to do the same thing for the other array as well. You've already made this "temp1" variable, but it's the wrong type to use for swapping data in the gp array of doubles.
I don't think I can make it any clearer without giving you the code.

10. You could always start by sorting one array first, then make a copy of that code and modify it for the second array
It should be easier that way. Get one working first, then copy that working implementation and do it for the second.

11. Originally Posted by Elysia
You could always start by sorting one array first, then make a copy of that code and modify it for the second array
It should be easier that way. Get one working first, then copy that working implementation and do it for the second.
I don't think that is what he wants to do, if I understand the question right.

12. What it means, first get the code for sorting the first array done.
Then, by using that as a template, get the code for sorting the secondary array done.

13. Originally Posted by Elysia
What it means, first get the code for sorting the first array done.
Then, by using that as a template, get the code for sorting the secondary array done.
Unfortunately you don't understand what the OP wants to do, because what you're talking about isn't possible. It would be impossible to sort one array without sorting the other array at the same time or the corresponding items from the other array will not match up.
The only way to even come close would be to generate an index list in a third array and then use that to step through the items in the other two arrays instead of stepping through them from 0 to n-1. That is unfortunately a lot more complex.

All Rkukulski needs to do is get the other swap corrected.

14. I was relaying the common message:
Don't focus on getting two tasks done at once. Fix the first, then the second.