# Thread: top ten values while I insert data

1. ## top ten values while I insert data

Hi , Pleaze Help..
I insert various values and I have an array[10]. Give me an idea of how to store in that array the ten biggest values that I have entered. I insert 1000 values let's say.(P.S. The values are taken from a file, and I want suggestions not the code)

2. Perhaps using a series of if, else if, and else statements starting with the highest and if the value in the data is low, use the next conditional statement. If something is higher, set the value to the point where the statement was true then reorder those below it by "scooting" them. This is the easiest method, but I haven't really tried it.

3. The data are stored in curr1 each time:
I use this code but somethings going wrong can you find what?

Code:
```                for (j=0;j<10;j++){
if (curr1>TopTen[j])
{
for(i=j;i<10;i++)
{
TopTen[i+1]=TopTen[i];
}
TopTen[j]=curr1;

break;
}
}```

4. - read 10 values
- sort the array into decending order
- for each additional value, compare with the last entry in the array
-- if less than or equal, throw it away
-- else find correct place to insert into array(*)

(*) or just overwrite the 10th entry and re-sort it.

5. insert 1000 values into a max-heap, and then extract the maximal element 10 times

6. chAnged the code to this

Code:
```
for (j=0;j<10;j++)
{
if (curr1>TopTen[j])
{
temp=TopTen[j];
TopTen[j]=curr1;

for(i=j;i<10;i++)
{
temp2= TopTen[i+1];
TopTen[i+1]=temp;
temp=temp2;
}
break;
}
}```
but still does nt work any idea why?

7. Using proper indentation would give us a better idea of what you're trying to do.

8. Perhaps maybe this might get you started:

Code:
```for(value_count = 0, value_count < 1000, value_count++)
{
// insert code to read from your file
/* this sorts the list */
if (value_read > value_order[0]) // if the value read is above the highest order
{
value_order[8] = value_order[9]; // moves existing values removing the current lowest
value_order[7] = value_order[8];
value_order[6] = value_order[7];
value_order[5] = value_order[6];
value_order[4] = value_order[5];
value_order[3] = value_order[4];
value_order[2] = value_order[3];
value_order[1] = value_order[2];
value_order[0] = value_order[1];
value_order[0] = value_read; // this must be last
}

else if (value_read > value_order[1]) // if value read is above the second highest order
{
value_order[8] = value_order[9]; // moves existing values removing the current lowest
value_order[7] = value_order[8];
value_order[6] = value_order[7];
value_order[5] = value_order[6];
value_order[4] = value_order[5];
value_order[3] = value_order[4];
value_order[2] = value_order[3];
value_order[1] = value_order[2];
value_order[1] = value_read; // this must be last - array index 0 is not modified
}

else if // and so on
}```
This gives the rough idea - it's the system I use for my 2D game's high scores. Note the patterns and you should be able to continue on. I'm sure there's a better way, but it gives the general idea.

9. > value_read > value_order[0]
I feel ill just looking at that

It only takes 5 lines, not 100's of copy / paste

10. Originally Posted by Salem
I feel ill just looking at that
think how this code will look like for the array of size 100, and you'll fill better immediatly

11. Well, it gives the idea on how it can be done (so the user can visualize the process a bit better (hopefully anyway)). I don't understand the "> value_read > value_order[0]" thing, but oh well.

Popular pages Recent additions