Thread: Finding an array of unique values within a 2D array

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Finding an array of unique values within a 2D array

    Hi! I am new to C programming. I am trying to write a program that involves finding unique values in a 2D array and saving them in a separate array. The values in the 2D array are not sorted. Also, a value and its negative should be considered the same. For example, if the 2D array is
    Code:
    int array[][] = {1, 2, 3}
                                            {-3, 4, 1} 
                                            {-1, 2, 5}
                                            {5, 2, 6}
    //The array of the unique values should be
    unique[] = {1, 2, 3, 4, 5, 6}
    Please explain the code that would carry out this function. Thank you!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    An simple insertion sort using absolute values would solve the problem.

    Tim S.
    PS: No code supplied by me.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Thanks for the reply. However, what I wanted to ask was that how can I move through the 2D array and select the unique values. I have to ensure that the values are not similar to those encountered previously before I can add them to the 1D array of unique values. Moreover, if a value is negative and its positive is not an element in a 2D array, it should go in the unique array as positive.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    26
    Well... what have you tried so far?

    You could use for loops to iterate the elements of the array. A for loop will work to check it against the elements of your unique[] array. Try to figure it out and implement something then if you have a specific question or get stuck, come back and ask about that.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Out of curiosity, but suppose this was a 1D instead of 2D array. Would you know how to solve the problem then?
    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

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    I have tried solving the problem using for loops as shown below. I get a segmentation fault. The code is given below. The x[] is the array of unique elements (size of x[] is n+1) and I have already assigned values to its first two elements as can be seen. The cn[][] is the 2D array (size [c][3]). Since I am new to C programming, I am not that good as using nested for loops. Please correct the error in the code.
    Code:
    int x[n+1];
    x[0] = 0;
    x[1] = cn[0][0];
    int i, j, k, l;
    for(k = 2; k < n+1; )
    for(i = 0; i < c; i++)
    for(j = 0; j < 3; j++)
    for(l = 1; l < k; l++)
    if(x[l] != cn[i][j])
    {
    x[k] = cn[i][j];
    k++;
    }

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Imagine that your unique array was full of zero's to start. Now your function could "walk" through the number array, and every value it found, it could add 1 to the unique array INDEX element. If you found a 4 then unique[4] = unique[4]+1. (C has a more concise way of representing an increment, find it).

    When you have "walked" through every value in the number array, your unique[] will have a story to tell you. Print it out and study it. Read up on "Counting Sort", which uses this principle very well.

    Your other code is working WAY too hard. This is beautifully simple.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding unique strings in an array
    By knirirr in forum C Programming
    Replies: 3
    Last Post: 02-20-2008, 07:08 AM
  2. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 09:15 PM
  3. Unique Random Numbers in 2D Array
    By kssjbr in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2003, 02:45 AM
  4. sizeof(array) returns differnt values for same array?
    By Diamonds in forum C++ Programming
    Replies: 6
    Last Post: 02-02-2003, 04:27 PM
  5. finding max values in an array
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-29-2002, 02:47 PM

Tags for this Thread