connecting array to match the number even if it is sorted

closed account (GybDjE8b)
How do we connect the Id array with the number so that when you sort it the id is with the number

Ex: 1. 5
2. 4
3. 6
sorted = 2. 4
1. 5
3. 6

Are you trying to set the index of the array to 2.4? Your example is pretty horrible.
closed account (GybDjE8b)
okay think of it as
1) 5
2) 4
3) 1

u sort it it shouldn't be
1) 1
2) 4
3) 5
the id should be constant with its number


so it should be
3) 1
2) 4
1) 5
Last edited on
closed account (GybDjE8b)
bump
So, basically, you want your sorted array to also contain a record of the original positions of the elements, as they were in the unsorted array?

I'd recommend using an array of std::pair<int, int>. One half of the pair stores the value, and the other stores the original position. You then sort the pairs by the value. Then, when you look at the pairs in value-sorted order, the second half of the pair gives you the original position.
closed account (GybDjE8b)
yeah ive been trying to pair i don't quite understand how to use pair
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    int newarray[n];
    int temp[n];
    
    for(int j = 0; j < n; j++)
    {
    array[j].total = (360*array[j].year) + ((array[j].month-1)*30) + array[j].day;
    cout << id[j] << " : " << array[j].total << " ";
    newarray[j] = array[j].total;
    pair<int, int> temp[j];
    temp[j] = (id[j],newarray[j]);
    cout << temp[j] << " ";
    }
    cout << endl;
Last edited on
You would find your code much easier to read and understand if you used sensible, consistent indentation.

So would most of the rest of us.

Don't just dump poorly formatted code on us and expect us to do all the work. Tell us what the actual problem is now.
closed account (GybDjE8b)
I don't get what you mean but okay..... don't need to be a douche about it since i'm a beginner -.- ...... I'm asking how does it work and I don't quite understand how it work is my problem if you actually read and that is an example that I've tried using but it doesn't work....... It won't set the array together
Last edited on
You posted some code. Presumably, there is some kind of problem with that code? The thing is, we can't read your mind. You have to tell us what the problem is. Are you getting compilation errors? Are you getting run-time errors? Is there some behaviour that happens when you run the code that you're not expecting?

You're asking us to give up our time and effort, free of charge, to help you. It behoves you to do whatever you can to make our job easier, and that includes taking the effort to type in whatever information might be useful in helping us fix your code.

closed account (GybDjE8b)
cool............. I don't know if you know the definition of a beginner cause i honestly don't know what to tell you ......... sorry for wasting your time and effort obviously im asking nicely and you could have easily responded nicely to what i'm suppose to do
You know, I'm taking the time and effort to try and help you here. Since your only response it to be rude and insulting, then.... yeah, I think I'll stop.

Good luck with insulting people, and then trying to get them to help you.

Edit:

i honestly don't know what to tell you

You could tell us what it is you're seeing in your code that makes you think there's a problem. Are you seeing compiler errors? Are you seeing run-time crashes? Are you seeing some behaviour that you believe to be incorrect?

im asking nicely

Calling somebody a "douche" is your idea of asking nicely?
Last edited on
closed account (GybDjE8b)
LOL coming from somebody insulting me "Don't just dump poorly formatted code on us and expect us to do all the work". Did i say go type my code for me? nope..... thx for noticing how you were insulting me too :) o and on top of that i asked nicely until u started to insult me and have a huge attitude with me...... guess u really don't know how to read and no your not taking any time or effort to help me by insulting me thank you for wasting my time here
Last edited on
There was no insult there. I didn't call you anything unpleasant or nasty at all. I simply pulled you up on a less-than-helpful way you were looking for help.

But at this point, it looks as though you're far more interested in being argumentative and defensive, than you are in doing the things that might actually get you the help you're looking for. I'm not interested in that, so I'll leave you to have fun on your own.
Topic archived. No new replies allowed.