Sorting Array without changing Index Values?

I'm SUPER rusty at C++ and am trying to brush up. I'm trying to write a program that will allow a person to input 10 numbers, then sort them and output the result. The sorted results, though, need to be linked to the original Index values. For example, if the user enters Item 1 = 4, Item 2 = 12, Item 3 = 1, Item 4 = 25, I want the program to sort the values and then print them like this:
Item 3 = 1
Item 1 = 4
Item 2 = 12
Item 4 = 25

I found the idea of making a copy of the original array and linking the sorted array values (in the copy of the array) to the index values in the original array, but I'm lost about how to do it. Here's what I have so far:

#include <iostream>
using namespace std;
int main (void)
{
system ("CLS");
int iPerson[10];
int iPersonCopy[10];
int iItems, min, max;
cout << "Please enter the number of items each person has: " << endl;
for (int n = 0; n < 10; n++){
cout << "Person " << n+1 << ": ";
cin >> iItems;
iPerson[n] = iItems;
cout << endl;
}
for (int i = 0; i < 10; i++){
iPersonCopy[i] = iPerson[i];
}

min = iPersonCopy[0];
max = iPersonCopy[0];
for(int i = 0; i < 10; i++){
if (min > iPersonCopy[i]){
min = iPersonCopy[i];
}
else if (max < iPersonCopy[i]){
max = iPersonCopy[i];
}
}
cout << "The maximum number of items is " << max << ". " << endl;
cout << "The minimum number of items is " << min << ". " << endl;

void bubbleSort(int iPersonCopy[], int n=10);{
int i;
int j;
for(i = 0; i < 10; i++){
for(j = 0; j < 10; j++){
if(iPersonCopy[i] < iPersonCopy[j]){
int temp = iPersonCopy[i];
iPersonCopy[i] = iPersonCopy[j];
iPersonCopy[j] = temp;
}
}
}
}

I think I'm missing something here so that I can keep the indexes with the appropriate values, but don't know what I'm missing...

cout << "Here's your sorted information: " << endl;
for (int m = 0; m < 10; m++){
cout << "Person " << m+1 << " has" << iPerson[m] << " items." << endl;
}
return (0);
system ("BREAK");


Any ideas???
Are you doing this for the sake of writing your own sort algorithm? If you stored the user input in a vector, you can call the standard sort() function and it will be done for you.
If you want to keep the index, you can create a second vector with (value,index) pairs and sort it as usual.
Alternatively, you can create a vector of pointers to the original elements and provide a custom comparison function. However, you have to keep in mind that the pointers become invalid as soon as you change the original vector.
Last edited on
@ mzimmers: I'm just trying to brush up my skills... I'm going back to school in January and haven't done any programming in 8 years so I'm trying to get back into it, starting with the basics.

@ both: I found an assignment online that recommends doing this with arrays, but everyone else out there says to do it with vectors. I really don't know how to use vectors in C++ (or what the difference between a vector and an array is), but I'm sure I'll figure it out...

Is a vector just like a multidimensional array? A friend of mine recommended using a multidimensional array, where the first item entered would be a number from the user, and the second item in the array would be a counter. He said that I'd be able to sort the data in the array by the first value, keeping the second value unchanged. What do you think of this idea?

Also, can vectors have multiple data types within them? (I'm guessing not) What if I wanted a user to enter the name of a person and then enter the amount of items that person had... how could I store both char or string data in the same array as the int data that would be inputed? I don't know how to do this with an array (I don't think it's possible) but is it possible with a vector?
No, a vector isn't a multidimensional array. It's a template object intended to replace arrays. As such, it comes with a whole bunch of handy functions to access and process it. It also offers a few advantages over arrays (like dynamic growth when needed).

As far as I know, you can create a vector for a class or function, to do what you stated above. I've never done it, but I'm fairly confident it can be done.
Also, can vectors have multiple data types within them? (I'm guessing not) What if I wanted a user to enter the name of a person and then enter the amount of items that person had... how could I store both char or string data in the same array as the int data that would be inputed? I don't know how to do this with an array (I don't think it's possible) but is it possible with a vector?


When you declare a vector, you say what type of object it's going to hold - and it can then only hold that type of object. The real use, though, is that you can define your own struct/class, containing whatever data you want (say a name, an amount and whatever), and store lots of those in the vector.

That's what I'd be doing in this example - have a struct that contains the number and the original index, and store those in the vector. That way you keep them together and don't have to jump through hoops to keep the relationship.

You could also use a simple array of the structs instead of the vector, then you'd be writing the sorting code, too - which I should think is the original object of this exercise?

Cheers,
Jim
An array is a built in language construct. A vector is part of the standard template library.
Here is an example of using a class with a vector. I'm using lambdas with sort and for_each, a newer feature from C++0x/C11.

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
class twoInts
{
  public:
    int pos;
    int value;
    twoInts( int p, int v ) : pos(p), value(v) {}
};

int main( int argc, char* argv[] )
{
  vector< twoInts > v;
  v.push_back( twoInts( 0, 12 ) );
  v.push_back( twoInts( 1, 53 ) );
  v.push_back( twoInts( 2, 7 ) );
  v.push_back( twoInts( 3, 19 ) );

  sort( v.begin(), v.end(), []( const twoInts& t1, const twoInts& t2 )
  {
    return t1.value < t2.value;
  } );

  for_each( v.begin(), v.end(), []( const twoInts& t )
  {
    cout << "Pos " << t.pos << " = " << t.value << endl;
  } );
	
}


Output

Pos 2 = 7
Pos 0 = 12
Pos 3 = 19
Pos 1 = 53


Thanks for the suggestions everyone! All this vector stuff looks like a foreign language to me, so I decided to just do it the laborious, slow, and low-tech way with a multidimensional array. Here's the important part of the code:

system ("CLS");
int iPerson[2][10];
int iItems, min, max;
int counter = 0;
cout << "Please enter the number of items each person has: " << endl;
for (int n = 0; n < 10; n++)
{
cout << "Person " << n+1 << ": ";
cin >> iItems;
iPerson[0][n] = iItems;
iPerson[1][n] = counter;
counter++;
cout << endl;
}void bubbleSort(int iPerson[], int n=10);
{
int i;
int j;
for(i = 0; i < 10; i++)
{
for(j = 0; j < 10; j++)
{
if(iPerson[0][i] < iPerson[0][j])
{
int temp1 = iPerson[0][i];
iPerson[0][i] = iPerson[0][j];
iPerson[0][j] = temp1;
int temp2 = iPerson[1][i];
iPerson[1][i] = iPerson[1][j];
iPerson[1][j] = temp2;
}
}
}
}

Eventually I'll get this vector thing... but I think it will be a while.
Vectors really aren't that tough to get your arms around. I'm relatively new to them myself, but I use them instead of arrays more often than not.

Just remember that they're a class, and as such, are accessed via class methods. The data itself is hidden from you, so don't make any assumptions about how they're stored (I learned this the hard way). Just use the class interface and you'll be good.

I haven't looked at your work above. In the future, if you'd put your code into code blocks, that would be a good thing.
Topic archived. No new replies allowed.