Thread: top ten values while I insert data

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    14

    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. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    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. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    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. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    - 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    30
    insert 1000 values into a max-heap, and then extract the maximal element 10 times

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    14
    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. #7
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Using proper indentation would give us a better idea of what you're trying to do.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  8. #8
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    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. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > value_read > value_order[0]
    I feel ill just looking at that

    It only takes 5 lines, not 100's of copy / paste
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote 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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    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 subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  2. simultaneously waiting for data on FIFO and UDP using select call
    By yogesh3073 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-05-2007, 09:53 AM
  3. How to insert data to the top of exist text file?
    By ooosawaddee3 in forum C++ Programming
    Replies: 4
    Last Post: 08-16-2005, 08:08 AM
  4. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM
  5. Help!!!!!!!!
    By Shy_girl_311 in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2001, 02:22 PM