# top ten values while I insert data

This is a discussion on top ten values while I insert data within the C Programming forums, part of the General Programming Boards category; Hi , Pleaze Help.. I insert various values and I have an array[10]. Give me an idea of how to ...

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;
}
}```

- 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++)
{
/* 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.