eliminating dup values in array

This is a discussion on eliminating dup values in array within the C Programming forums, part of the General Programming Boards category; I need to write a program that reads in 20 numbers between 10 and 100 using a single-subscripted array and ...

  1. #1
    Unregistered
    Guest

    eliminating dup values in array

    I need to write a program that reads in 20 numbers between 10 and 100 using a single-subscripted array and then as each number is read, print it only if it's not a duplicate of a number already read.

    I can read in the numbers and initialize the element values in the array, but have no idea how to eliminate the dups when printing.

    Our teacher said we could search the array and eliminate the dup and shift the next element up but she didn't tell us how to do that. I'm lost. We have learned binary search trees yet, just linear search and binary search.

    I think I have to use a bunch of nested for loops, but can't get it.

    A few hints please. Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The easiest way is this:
    Code:
    int array[20] ={0};
    int temp,x,y;
    for( x = 0; x < 20; x++ )
    {
        prompt for number %d, x 
        temp = read a number from user
        for( y = 0; y < x; y++ )
            if temp == array[y]
                dupe = true
        if dupe != true
            array[x] = temp
        /* else ignore it */
    }
    Now just loop through the list and print any non-zero value when you're done.

    Quzah.
    }
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Just read each number and then check it against the other numbers in the array. There are plenty of ways to do this, here is one of them:
    Code:
    static int 
    checkDup ( const int *a, const int val )
    {
      int x, count = 0;
      for ( x = 0; x < VAL_SIZE; x++ )
        if ( a[x] == val ) count++;
      return ( count > 1 ) ? 1 : 0;
    }
    Call checkDup for every number that you read and if it returns 1, don't print it.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2008, 10:25 AM
  2. Replies: 2
    Last Post: 11-19-2008, 02:36 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. How to read in empty values into array from input file
    By wpr101 in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2002, 10:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21