Thread: need hint how to filter the results

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    need hint how to filter the results

    OK, I have an array with numbers, and an array with the number of occurences of each corresponding number from the number array. I need to display the numbers from number array which occured the most. The way I did it, it displays them and they repeat themselves. E.g. 7 occured 7 times, 87 occured 7 times, 7 occured 7 times etc. It is supposed to display it only once. Can somebody give me hint how to do that?
    What I have so far is this:
    Code:
    for(i = 0; i < SIZE; i++)
         {
          search = numbers[i];
           
          for(j = 0; j < SIZE; j++)
              {
               if(numbers[j] == search)
                 occurences[i]++;
              }
         }
    
       printf("\n");
    
       largest_occ = occurences[0];
    
       for(i = 0; i < SIZE; i++)
          {
            if(largest_occ < occurences[i])
               largest_occ = occurences[i];
          }
    
       for(i = 0; i < SIZE; i++)
          {
            if(largest_occ == occurences[i])
              {
               modes[j] = largest_occ;
               j++;
              }
          }
    The last for loop was to put all the modes in a separate array so I could filter it, but it does not work.
    Any hints how I could go about it?

  2. #2
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
       for(i = 0; i < SIZE; i++)
         {
          search = numbers[i];
           
          for(j = 0; j < SIZE; j++)
              {
               if(numbers[j] == search)
                 occurences[i]++;
              }
         }
         // What do you think j equals right now?
    
       printf("\n");
    
       largest_occ = occurences[0];
    
       for(i = 0; i < SIZE; i++)
          {
            if(largest_occ < occurences[i])
               largest_occ = occurences[i];
          }
    
       for(i = 0; i < SIZE; i++)
          {
            if(largest_occ == occurences[i])
              {
               // Do you think you're using j correctly?
               modes[j] = largest_occ;
               j++;
              }
          }

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Yes, I have already corrected the j problem, I know... However, it still did not solve the question how to filter the results...

  4. #4
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Could you post your updated code that fixed the j variable issue?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    I just reset it to "0" before the loop, it is only a counter... it's incremented right after the statement that uses it.
    Code:
    for(i = 0; i < SIZE; i++)
         {
          search = numbers[i];
           
          for(j = 0; j < SIZE; j++)
              {
               if(numbers[j] == search)
                 occurences[i]++;
              }
         }
    
       printf("\n");
    
       largest_occ = occurences[0];
    
       for(i = 0; i < SIZE; i++)
          {
            if(largest_occ < occurences[i])
               largest_occ = occurences[i];
          }
    
       j = 0;
    
       for(i = 0; i < SIZE; i++)
          {
            if(largest_occ == occurences[i])
              {
               modes[j] = largest_occ;
               j++;
              }
          }
    What I think is that the problem lies in what I put in the modes array...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filter directory search results.
    By Loic in forum C++ Programming
    Replies: 11
    Last Post: 08-18-2008, 09:32 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Results of March Monthly Contest
    By PJYelton in forum Contests Board
    Replies: 23
    Last Post: 04-17-2005, 09:46 AM
  4. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  5. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM