Click here to Skip to main content
15,399,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an array that I ordered it in ascending order. so its ordered correctly.

My code:
float temp; int x; 
    for (int i = 0; i < 4344 ; i++)
    {
       for (int j = i + 1; j < 4344; j++)
       {   
           if (array_dist[i] > array_dist[j])
           {
                temp = array_dist[i];
                array_dist[i] = array_dist[j];
                array_dist[j] = temp;
                x = index_arr[i];
                index_arr[i] = index_arr[j];
             index_arr[j] = x;
           }        
        }
    }
        
    cout << "print out distance colum after ascending sorting : \n";
     
        for (int i = 0; i < 4344; ++i)
     {
         cout << index_arr[i] << " : "
             << array_dist[i] << endl;
     }


What I have tried:

But, I tried to follow the same ordering procedure to store the indices of the original array. But gave me wrong indices. Any suggestion plz.
Posted
Updated 30-Aug-21 0:20am
Comments
Richard MacCutchan 27-Aug-21 10:44am
   
I just tried that with some random values and it appears to work correctly. You need to provide more details.
prother123 27-Aug-21 13:02pm
   
Actually, it returns the indices in wrong way.
Richard MacCutchan 28-Aug-21 3:28am
   
I have no idea what that means, since you have not explained what actual results you get and why they are wrong. In all my tests the indices are correct (as far as I understand the issue) when they are returned.
Greg Utas 27-Aug-21 11:06am
   
The code shown looks fine, so did you remember to initialize each entry's index?
prother123 27-Aug-21 13:03pm
   
the index_arr has values from 0 ....4343
armagedescu 27-Aug-21 11:10am
   
What is the exact meaning of the distance colum after ascending sorting? In which way exactly it is incorrect?
Richard MacCutchan 27-Aug-21 11:28am
   
It is the original index position of the value.
armagedescu 28-Aug-21 7:40am
   
I need to know how exactly OP defines it. I'd like to understand what exactly he expects to see.
Richard MacCutchan 28-Aug-21 7:42am
   
Me too, that is the vital piece of information he has failed to provide.
armagedescu 28-Aug-21 7:43am
   
If is that you say, then it is the difference between i and index_arr[i].
prother123 27-Aug-21 13:04pm
   
the array_dist is sorted correctly, but the indices are not sorted correctly
armagedescu 28-Aug-21 7:39am
   
How do you prove the indices are not sorted correctly? Provide some exact detail on what is the actual behavior and what is expected behavior in simple words that can help to spot any problem. A short sample of what your code does and that you actually expect, with some explanations.
Rick York 27-Aug-21 11:20am
   
One suggestion I have is lose the value 4344 which appears in your code three times. That should be an argument to the function or a constant value. It should NOT appear multiple times in the code.
jeron1 27-Aug-21 11:53am
   
Ugh! I just had to fix an issue on some legacy assembler code where there was no equates for constants. Had to change 23h to 22h in a file filled with unrelated 23's. Oh the joy!
Patrice T 27-Aug-21 13:14pm
   
Show some code the can be compiled with sample data, 10 values are enough for testing.

1 solution

Start by doing a few things:
1) As Rick said, don't use "magic numbers" to control your app - either use #define to declare it as a constant and use that throughout, or pass it as a parameter to the function along with the two arrays to reorder.
2) Reduce the array size to 5 elements, and sort them: then check the indexes array to see if they get scrambled or are correct. If they are fine, change the data until you come up with a version that shows the problem you are having with the "full version".
2.1) If you can't duplicate the problem in small scale, check that your diagnosis that the sort function is wrong - perhaps it's the way you are checking the indexes before and / or after the sort that is at fault?
2.2) If you can duplicate the problem in small scale, use the debugger to follow your code while it is running and see exactly what is happening and where it goes wrong. That should give you clues as to why.

Just saying "it don't work" doesn't help anybody, particularly since we can't check your code with your data!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900