# Thread: Help me Solve this problem

1. ## Help me Solve this problem

I have an array(size 35) with various positive double values. I need to use these values in an algorithm ultimately to calculate an (x,y) position on a graph, but that is irrelevant. What I need help with is how to narrow this array down from 35 values to 20. You see, the 35 values range (from around 40 to 80...usually) and what I want to do, is get my array of 35 (all the values) to become a smaller array of 20 that is made up of the 20 highest values from the array of 35. I have been working on this algorithm the past 3 hours and am getting kind of sick of it, so any hints or help from you guys would be appreciated.
Here is basically what I have so far.
Code:
```for( i=0; i+1<=35; i++ )
{
GetVal ( dRSSI[i] );                          // I fill my array of 35 w/ values i need
}
for( i=0, n=0; i+1<=35, n+1<=20; i++, n++ )
{
if( dRSSI[i] >= dRSSI[i+1] )                 //If val a > b, keep a and put it into smaller
{                                    //array
}
}```

2. If you are allowed to use the standard library, then the solution might be nth_element.

3. I am actually working in CVI (in a version which is unable to access certain c++ functions) so I don't think that will work

4. The easiest (though not most efficient) way might be to sort the array and then select the first (or last) 20?

5. Sort the array in descending order then copy the first 20 elements out of it and into a new, smaller array?

edit: beaten but still relevant

6. What's wrong with applying a sort to your array to sort from low to high or vice-versa??

7. Yes... That would be the obvious choice.
However, that way is not very user friendly because you constantly need to remember to sort the array before you do the algorithm (takes time).
Also, in my algorithm, I need to use values that I have stored in other arrays to relate to my values in dRSSI[].
Look at the code below. I reverted back to what I had, which calculates every values distance and uses all values in the algorithm. Again, my goal is to only use the 20 highest RSSI values (instead of all of them) to calculate position.
Code:
```//Array of X-Coordinate values
double xLCMs[NUMBER_OF_LCMS] = {9, 9, 9, 7, 20, 20, 20, 20, 31, 31, 31, 31, 42, 42, 42, 53.5, 53.5, 53.5,32.5, 34, 34, 34, 34, 43.5, 43.5, 43.5, 43.5, 43.5, 43.5, 54.5, 54.5, 54.5, 54.5, 54.5, 54.5};
//Array of Y-Coordinate values
double yLCMs[NUMBER_OF_LCMS] = {12, 26, 40, 56, 12, 26, 40, 54, 12, 26, 40, 54, 12, 26, 40, 12, 26, 40,101, 115, 122, 132, 142.5, 73, 87, 101, 115, 129, 142, 73, 87, 101, 115, 129, 142};
double dA=0;
double dN=4.0;
double sumXNumerator;
double sumYNumerator;
double sumDenominator;
double dDistance[NUMBER_OF_LCMS];
double dXSumNumerator[NUMBER_OF_LCMS];
double dYSumNumerator[NUMBER_OF_LCMS];
double dSumDenominator[NUMBER_OF_LCMS];
int i;

for( i=0; i+1<=NUMBER_OF_LCMS; i++ )
{
GetTableCellVal ( tabHandle, TABPANEL_TABLE_VALUES, MakePoint( 2, i+1 ), &dRSSI[i] );
dDistance[i] = pow ( 10, ((dRSSI[i]-dA)/(dN*10.00)) );
dXSumNumerator[i] = xLCMs[i]/dDistance[i];
dYSumNumerator[i] = yLCMs[i]/dDistance[i];
dSumDenominator[i] = 1.00/dDistance[i];
}
//Calculate the sum of each array
Sum1D( dXSumNumerator, 20, &sumXNumerator );
Sum1D( dYSumNumerator, 20, &sumYNumerator );
Sum1D( dSumDenominator, 20, &sumDenominator );

//Use the sum's calculated for XNum,YNum, and Denom and determine (x,y) position.
*XPuckValue = ( sumXNumerator/sumDenominator );
*YPuckValue = ( sumYNumerator/sumDenominator );

PlotPoint ( panelHandle, PANEL_SYSTEM, *XPuckValue, *YPuckValue, VAL_X,
VAL_BLUE );```

8. Originally Posted by letsgoredwings
Yes... That would be the obvious choice.
However, that way is not very user friendly because you constantly need to remember to sort the array before you do the algorithm (takes time).
I'm not sure I understand your point here -- you're going to have to remember to do something before you start, whether it's sort or prune or whatever. And for 35 elements, I would bet that sorting would be just as fast as anything else to get 20 elements.

The catch is going to be to remember that when you sort on distance[], you'll also need to swap xLCMs[] and yLCMs[] also. What I would probably do is, instead of sorting the arrays themselves, sort a list of indices. So you'd have an index[] that starts out at 0 through 34, and then gets shuffled around to give the order. Then you can display xLCM[index[0]] to get the x-coordinate of the largest, and xLCM[index[1]] to get the x-coordinate of the next-largest, and so on.

9. My point is that as a user constantly were to use this program, it would get annoying to constantly have to remember to sort the column before running through your algorithm....
I want as much of the program to happen automatically... without the user doing anything...(not sure if that makes sense... lets just say i need to find another way to get the solution besides sorting column)

10. You can use a sorting algorithm to do the sorting, the user doesn't have to remember to do anything.

11. yeah... i was trying to sort the column using the set table cell attribute function when i should've looked for, and found the sort function...
i'm a noob... or whatever you nerds call it

anyways, i think i got it now