Thread: Eliminating duplicate values from a 20x2 array

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Eliminating duplicate values from a 20x2 array

    hi guys, i'm REALLY new to C programming, and have created a few basic programs, but this is one problem thats really stumped me, and thats how to eliminate multiple values from a 2D array. Basically, the user enters 20 numbers, and those are entered into the first column of the array. Then, i somehow have to eliminate the duplicate values in the first column and copy them into the second. Thats part of whats stumping me, but the other part is then with the second array, sorting them based on even/odd/negative values. The input is integer only, but the problem is that i can't use a threshold value in the second array to indicate whether a duplicate was found, as it would then throw off the sorting of the neg/even/odd elements.

    eg)
    Code:
     for (i=0;i<ROWS;i++){
    
                     for (j=0;j<ROWS;j++){
    
                        if (matrix[j[!=matrix[i]){
    
                            matrix[i]=-1
                        }
                      }
                    }
    The above would not be valid...I've searched the forums and gotten a few results, but nothing i can really use. If someone could give me a hand, i would appreciate it!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    For each row, check every filled column of the second column. If the number already exists, don't copy it:
    Code:
    for ( i = 0; i < ROWS; i++ ) {
      for ( j = 0; j < k; j++ ) {
        if ( a[i][0] == a[j][1] )
          break;
      }
    
      if ( j == k )
        a[k++][1] = a[i];
    }
    There's really no need to do anything more sophisticated since you're only working with 20 numbers.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    3

    Thank You!

    That worked great! Excellent man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-21-2005, 12:39 AM
  2. 3-d array assign string values
    By WaterNut in forum C++ Programming
    Replies: 8
    Last Post: 07-01-2004, 12:02 AM
  3. assigning values in file to an array
    By divinyl in forum C++ Programming
    Replies: 9
    Last Post: 07-29-2003, 08:33 AM
  4. 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
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM