Thread: Need some help with Counting Duplicates that my Array contains.

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    4

    Question Need some help with Counting Duplicates that my Array contains.

    I have basically written the whole program but at the end of my program i wrote a function that will count duplicates in my array. Supposedly I am doing something wrong with that function.

    My output is displayed as: [ this is wrong]

    C:UsersambarDesktopproggram2prog>program2 number.txt
    -21 -8 -8 2 3 3 5 5 23 99
    99 99 99 100 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    Duplicates: -21
    C:UsersambarDesktopproggram2prog>

    My output needs to be:

    2 5 3 99 -85 99 99 100 -823 -21 3 99 5
    running you program will show:

    C:cs222>prog2 numbers.txt-21 (1)-8 (2)2 (1)3 (2)5 (3)23 (1)99 (4)100 (1)

    My code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int length=0;
     int array[1000];
     int compare(const void *s, const void *t);
     int i,k;
    
    FILE * openinput(int argc, char * argv[]); 
    void new_func();
     
    
    int main (int argc, char * argv[])
     { 
       FILE * file = openinput(argc, argv);
         int total = 0;
         while (!feof(file))
         {
               int number;
               fscanf(file, "%d", &number);
               
               if(!feof(file))
               array[length++] = number;
         }
         fclose(file);
         qsort(array, length, sizeof(int), compare);
      new_func();
         system("PAUSE");
     }
    
        FILE * openinput(int argc, char * argv[])
         {
              if (argc != 2)
              {
                       printf("usage :sum filename \n");
                       exit(1);
              }
              
             FILE * returnvalue;
              returnvalue = fopen(argv[1], "r");
              
             if (! returnvalue)
              {
                    printf("unable to open %s \n", argv[1]);
                    exit(1);
              }
              
             return returnvalue;
         }
               
     int compare(const void *s, const void *t)
      {
        return *(const int *)s - *(const int *)t;
      }
         
    
    void new_func()
      {
       for (i=0; i < 1000; i++)
       printf("%d \t", array[i]);
     }
    Last edited by topgun303; 03-28-2012 at 07:20 PM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Oh God you killed the pretty code formatter, post the code again AS TEXT.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    Well I placed [code] my code [\code]. The website wont let me add attachments

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The end tag should be typed with a forward slash, not a backward slash.
    And the code you post needs to be pure text, no formatting.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    4
    I think code formating is fixed.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by topgun303 View Post
    I think code formating is fixed.
    Yes, it's fixed now - good deal.

    There are two different ways to solve this kind of a problem, that I'll describe.

    Pick the one you think best suited to your problem and skills in programming:

    1) Make a distribution array and count every integer. This works well if the range is not huge, and the values are all real integers. E.g.:
    The numbers are
    Code:
    {0,0,2,4,0,5,5,0}
    . You'll make an array of int's with 6 elements (so the index will range from 0 to 5).

    Code:
    int distrib[6]={0}; //set each value in the array to zero, initially
    
    for (each number in the set of numbers) {
       distrib[number]++; //if value is 2, distrib[2]++ (is incremented)
    }
    Now distrib[0] has the value 4, since there were four zeroes in the set of numbers above. distrib[5] has the value 2, etc.

    This technique can be used with letters as well, since letters are simply 8 byte int's (small range), which are printed with an offset when they are a string or have a char format specified: %c or %s.

    2) Second way is recommended when the range of numbers is REALLY large.

    Start by sorting the numbers, making all the repeating numbers, adjacent to each other:
    Code:
     {0,0,0,2,4,5,5}.
    Now in a loop, "walk" through the sorted numbers. You will print out each new number you find (non-repeating), and you will count all the numbers that do equal it, until the next non-repeating number has been reached. So reading the above, you'd have:

    0 - 1, 0 - 2, 0 - 3
    2 - 1
    4 - 1
    5 - 1, 5 - 2

    So the final output would be 0 - 3 occurrences, 2 - 1 occurrence, 4 - 1 occurrence, and 5 - 2 occurrences.

    In general, I use first version, but sometimes numbers are not int's or don't fall into the range needed. Then I can use the second version.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. avoid duplicates in an ordered array
    By baffleddreams in forum C Programming
    Replies: 1
    Last Post: 09-19-2010, 01:53 AM
  2. Merging integer array without duplicates.
    By nickman in forum C Programming
    Replies: 5
    Last Post: 08-22-2010, 02:06 AM
  3. Removing Duplicates from an Array
    By SlayerBlade in forum C Programming
    Replies: 4
    Last Post: 10-02-2005, 05:46 PM
  4. Array with at most n duplicates
    By kratz in forum C++ Programming
    Replies: 19
    Last Post: 07-16-2005, 11:46 PM
  5. Really basic remove duplicates from array
    By Chris Fowler in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 10:35 PM