Re: pointing un-ascended array elements to an ascended array element

May 13, 2017 at 2:23am
Hi,
I created a function that reads elements of an array and arrange them in ascending format. Now I need to somewhat tag their ID to the element wherever their position. Example:

ID Value
-- -----
A = 100
B = 300
C = 20
D = 60

Result should be like:
C=20
D=60
A=100
Etc

Any hint on how I can achieve the result above?

Thank you for the help!

Last edited on May 13, 2017 at 2:24am
May 13, 2017 at 2:50am
use a struct - 2 data members - char ID, int value - make array/std::vector<struct> and std::sort by the int value data member
May 13, 2017 at 3:12am
Thank you for the responds

Our course were to ignore vectors (for this time).
Any other way?
May 13, 2017 at 3:16am
Any other way?


earlier ...
make array/std::vector
May 13, 2017 at 10:01am
You can use two parallel arrays. One for the names and one for the ids. Remember when you swap the names when sorting you also need to swap the ids.
May 13, 2017 at 10:41pm
Hi gunnerfunner, I will try read on vectors though our course is to ignore it for this time...but thank you very much for the help! Appreciate that
May 13, 2017 at 10:48pm
Hi Thomas
That would be the answer if the IDs are like their value. Eg ID 2 = 90, ID 1 = 50, ID 3 = 95.

If I sort ascending style re values and IDs, I think there would be a pattern:
Id 1 = 50
Id 2 = 90
Id 3 = 95
Etc
May 14, 2017 at 7:57am
What's bad in gunnerfunner's solution?

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

struct MyCharInt {
    char id {};
    int value {0};
};

int main()
{
    MyCharInt mycharint[] = { {'D',  60},
                              {'C',  20},
                              {'B', 300},                              
                              {'A', 100} };
                              
    std::cout << "My array now is like this:\n";
    for(const auto& a : mycharint) {
        std::cout << a.id << "-->" << a.value << '\n';
    }

    std::sort(mycharint, mycharint + ((sizeof mycharint) / (sizeof *mycharint)),
              [](MyCharInt first, MyCharInt second) {
                  return first.id < second.id;
              } );

    std::cout << "\nAnd now my array is like this:\n";
    for(const auto& a : mycharint) {
        std::cout << a.id << "-->" << a.value << '\n';
    }

    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    
    return 0;
}

Last edited on May 14, 2017 at 8:00am
May 14, 2017 at 12:19pm
That would be the answer if the IDs are like their value. Eg ID 2 = 90, ID 1 = 50, ID 3 = 95.
I think Thomas is suggesting one array of int to hold the values and one array of char to hold the tags. When you swap items in the value array, you need to swap the corresponding items in the tag array.

Notice that Thomas's and gunnerfunner's solutions work the same way: store both the tag and the value. When you swap values, swap the tags too. The difference is that Thomas stores the data in two arrays and gunnerfunner stores it in an array of structs.
Topic archived. No new replies allowed.