Thread: Help keeping a count of distinct values

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    15

    Help keeping a count of distinct values

    So this code I'm trying to calculate the number of distinct values entered into an array. If i enter the followings
    "3,4,5,6,7,7,6,e (anything that's not a number)"


    i get a total of 7 but in reality it should be a 5. can anyone help out?

    Code:
    #include <stdio.h>
    
    
    
    
    
    
    //---------function to find the distinct values----
    //------ need help here----------
    
    
    int find_distinct(int list[], int size)
        {
         int i, j,size2, distinct = 0;
         for(i = 0; i < size; i++)
         {
               
               for(j=i+1;j<size;j++)
               {
                if(list[i] == list[j])
                {
                continue;
                }
                else
                {
                distinct++;
                }
          
               }
          return distinct;      
         }
    
    
        }
     
        
         
           
    
    
    int main()
    {
       //-------variables------------------ 
        int list[1000];  // can hold 1000 items
        int i, element, distinct;
        int ret;
             
        printf("Enter in as much number as you want and it'll calculate:\nthe number of distinct values. \n");
        printf("Type in a non integer when you're done\n");
     
     
        //------ user puts in array 1000 times or until they press a non integar
        for (i = 0; i < 1000; ++i)
        {
          ret = scanf("%i", &list[i]);
          if(ret == 0)
          {
           element = i++;
          
              
           distinct = find_distinct(list, element);
           printf("\nThe distinct value: %i", distinct);      
     
           
           getch();  
           }
          
          
         }
         
     
    }
    Last edited by Kevin Nguyen; 09-12-2013 at 08:56 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If the input is sorted, it's an easy algorithm.
    Code:
    for each number:
      Copy the first new number
      Skip every subsequent number that equals number
    After you traverse the list, you are left with a list of uniques.

    If the list is unsorted, it is harder, but not impossible. The numbers have to be in a limited range though, so that you can separate the input into buckets for each valid number in the range. And then with the buckets, you can do whatever you want.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    15
    would i need to make a 2nd array then?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If the array is sorted, then no, you don't need a second array if you only want a count of distinct elements. Rather, instead of copying the distinct element, increment the count.

    If the array is not sorted, then sort it, and the answer becomes the same as above. If you are not permitted to sort it, then yes, you need a second array (or other data structure).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    15
    Here's what i have, when i get the total distinct values, it's now 19.... am i missing something that i don't see? i got my array sorted.

    Code:
    //---------function to find the distinct values-------
    
    int find_distinct(int list[], int size)
    {
        int i, j, distinct = 1,temp;
        for(i=0;i<size;i++)
        {
            for(j=i+1;j<size;j++)
            {
                //sorts the array
                if(list[i]>list[j])
                {
                    temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
                if(list[i]==list[j])
    
    
                    continue;
    
    
                else
    
    
                    distinct++;
    
    
            }
    
    
        }
    return distinct;
    }

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Here's what i have
    It would be a hell of a lot easier for us if you also posted a main, and the test array that your using.
    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.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You can't count the distinct values at the same time you're sorting the array.

    First sort the array, then loop through it and increment your counter every time the value changes.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Registered User
    Join Date
    Sep 2013
    Posts
    15
    heres what i have so far if i type in "3,4,5,4,4,5,6,6,e(to finish inputting numbers)" the distinct values should be 4, but i got 5. so im still off by 1.

    Code:
    #include <stdio.h>
    
    
    
    //---------function to find the distinct values-------
    
    
    
    
    int find_distinct(int list[], int size)
    {
    int i, j, distinct = 0, temp;
    for(i=0;i<size;i++)
    { //sorts array
    for(j=i;j<size;j++)
    {
    temp = list[i];
    list[i] = list[j];
    list[j] = temp;
    }
    }
    for(i=0;i<size-1;i++)
    {
    if(list[i]==list[i-1])
    continue;
    else
    distinct++;
    }
    return distinct;
    }
    
    
    int main()
    {
    //-------variables------------------
    int list[1000]; // can hold 1000 items
    int i, element, distinct;
    int ret;
    
    
    printf("Enter in as much number as you want and it'll calculate:\nthe  total of distinct value. \n");
    printf("\nType in a non integer and it'll end the number input\n");
    
    
    
    
    //------ user puts in array 1000 times or until they press a non integar
    for (i = 0; i < 1000; ++i)
    {
    ret = scanf("%i", &list[i]);
    if(ret == 0)
    {
    element = i++;
    
    
    
    
    
    
    distinct = find_distinct(list, element);
    printf("\nThe number of distinct value: %i", distinct);
    
    
    
    
    
    
    
    
    
    return 0;
    }
    
    
    
    
    }
    
    
    
    
    }
    Last edited by Kevin Nguyen; 09-12-2013 at 11:49 PM.

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    as asked by whiteflags & laserlight - is the input to be entered, sorted?

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The double loop to sort the array just swaps the members of the array. The values of the array will need to be compared in order to sort them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. keeping count..
    By EssiJoon in forum C++ Programming
    Replies: 3
    Last Post: 06-27-2012, 01:10 AM
  2. Replies: 2
    Last Post: 07-28-2010, 02:07 PM
  3. keeping values in cache
    By C_ntua in forum C Programming
    Replies: 7
    Last Post: 08-01-2008, 04:34 PM
  4. How many distinct values?
    By George2 in forum Tech Board
    Replies: 2
    Last Post: 05-03-2008, 09:08 AM
  5. Count distinct array values
    By rkooij in forum C Programming
    Replies: 4
    Last Post: 10-03-2006, 03:03 AM