sorting Arrays and corresponding numbers

Hi! I'm having a problem with arrays. This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
     const int n = 3;
     int apples[n+1];  

     apples[1] = 73020;
     apples[2] = 63250;
     apples[3] = 102990;     

     bsort(n, apples);     

     for(int i=1;i<=n;i++)
     {
          cout << setw(3) << right << i << " " ;
          cout << setw(10) << right << apples[i] << endl;
     }
     system("pause");
     return 0;
}


I can sort out the number of apples in order, but the corresponding apple numbers are wrong:

Screen output:
1
2
3
4
1	63250
2	73020
3	102990
Press any key to continue . . .


What I'm tring to achieve is that the apple numbers should correspond to the numbers sold.

Desired output should be:
1
2
3
4
2	63250
1	73020
3	102990
Press any key to continue . . .


I am teaching myself and would appeciat all the help I can get.
Many thanks.
First off, a couple unrelated issues. Arrays are 0-based, so if your array is size 2, you access those 2 elements with myArray[0] and myArray[1]. You are trying to force c++ to work with 1 based indexing, and that's going to cause problems. Change your code to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
     const int n = 3;
     int apples[n];  // n, not n+1

     apples[0] = 73020;  // 0, not 1
     apples[1] = 63250;  // 1, not 2
     apples[2] = 102990;// 2, not 3     

     bsort(n, apples);     

     for(int i=1;i<n;i++)     //   <, not <=
     {
          cout << setw(3) << right << i << " " ;
          cout << setw(10) << right << apples[i] << endl;
     }
     system("pause");
     return 0;
}


Next, you are viewing the original index of the integers as some sort of identifier - it's not, it's just the location of the each integer in the apples array. You would have to do something more complicated to keep track of the original index of each integer, but do you really care about that? Will it affect anything later on?
You could create a class/struct that contains bothnumbers. Make the apples array contain objects the class and sort it. That way the two numbers will always stay together.
Thank you I will creat a class that contains both as I want to be able to list to most veriaty sold and their total number sold.

Many thanks for the advice,
Topic archived. No new replies allowed.