Store only non-double elements

This is a discussion on Store only non-double elements within the C Programming forums, part of the General Programming Boards category; Hi there, this is getting me frustrated and I'm trying to solve this to keep working on my code: I ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    61

    Store only non-double elements

    Hi there, this is getting me frustrated and I'm trying to solve this to keep working on my code:

    I have an array of integers whose elements may be equal:
    ex.: [1, 3, 3, 4, 10];

    I want to save, inside another array, only the elements that are different among eachother. I tag my code below; I can't get where I do it wrong, in fact, it does not recognize the different numbers.

    Thank you in advance for helping

    S.

    Code:
     //Get rid of the double node numbers:
    	s = 0;
    	for(i=0; i<2*nbnd_el; i++){
    		for(k=0; k<2*nbnd_el; k++){
    			if( _node[k] =! _node[i] ){
    				_NODE[s] = _node[i];
    				s = s+1;
    				printf("NODE = %d\n", _NODE[s]);
    			}
    		}
    	}
    p.s. This is not homework, I've been out from the Uni since 2005 already

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    _node[k] =! _node[i]
    Are you sure you want to assign _node[k] with the value of !_node[i]?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by matsp View Post
    Code:
    _node[k] =! _node[i]
    Are you sure you want to assign _node[k] with the value of !_node[i]?

    --
    Mats
    Hi mats, thanks for replying. No, what I am doing is checking whether any element of _node[] appears more than once in the array, and to do that I thought of running through the array element by element and checking if any element appears again inside the exact array copy _node[k] respect to _node[i]

    Thanks
    S.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what I was trying to say is that you are not testing correctly if the element is in the other array, but rather copying the result of "not _node[i]" into _node[k]. You will probably notice this as _NODE[s] never gets updated except when _node[i] is 0, as !_node[i] is only true when it's zero.

    The correct way to check if _node[k] is not equal to _node[i] would be using the != operator.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by matsp View Post
    So, what I was trying to say is that you are not testing correctly if the element is in the other array, but rather copying the result of "not _node[i]" into _node[k]. You will probably notice this as _NODE[s] never gets updated except when _node[i] is 0, as !_node[i] is only true when it's zero.

    The correct way to check if _node[k] is not equal to _node[i] would be using the != operator.

    --
    Mats
    Hi Mats, Thanks again; actually I hadn't realized the syntax error in the use of the operator; thank you about that. However, I still obtain what I am not looking for, in fact, given my array as:

    _node[] = [10 11 11 12 12 9 9 8 8 5];

    I would like to obtain _NODE[] = [10 11 12 9 8 5], but the result that I get is the following:

    NODE = 1
    NODE = -1208836096
    NODE = 1
    NODE = -1209129760
    NODE = -1209129760
    NODE = -1082134028
    NODE = -1210072491
    NODE = -1209129760
    NODE = -1208836096
    NODE = 1
    NODE = -1082134560
    NODE = 1
    NODE = -1208836096
    NODE = -1209131020
    NODE = 1
    NODE = 10
    NODE = 11
    NODE = 11
    NODE = 12
    NODE = 12
    NODE = 9
    NODE = 9
    NODE = 8
    NODE = 8
    NODE = 5
    NODE = -1209131020

    Why this?

    Thank you,
    Simone

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If your current code is similar to the posted above, then you are printing the _NODE[s] AFTER you have moved to the next location, which leads to weird printing.

    Also, I think your inner loop needs to look at ALL elements before deciding if it's in the second set or not, so you would want to move the assignment of the value _NODE[s] to outside of the "k" loop.

    Also, you probably want to check if the value is in the _NODE array, rather than in the _node array. All values will be in the _node array at least once, as that's where you are getting the value from!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by matsp View Post
    If your current code is similar to the posted above, then you are printing the _NODE[s] AFTER you have moved to the next location, which leads to weird printing.

    Also, I think your inner loop needs to look at ALL elements before deciding if it's in the second set or not, so you would want to move the assignment of the value _NODE[s] to outside of the "k" loop.

    Also, you probably want to check if the value is in the _NODE array, rather than in the _node array. All values will be in the _node array at least once, as that's where you are getting the value from!

    --
    Mats
    Hi there Mats, sorry for taking so long to reply; I was trying it in another way, but noithing; with your suggestion on the position of the print (as a check), I actually get to print it correctly, but the new array _NODE now correpsonds exactly to the original _node.

    It is a very simple algorithm in principle: loop over every element, and save the value to a new array only if the element does not repeat; nevertheless, I cannot get it to work correctly.

    Sorry for asking again, but would you have another suggestion?

    All the best
    Simone

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I thought I had already explained that the current code is broken:
    Code:
    For every element i in _node
       for every element k in _node
          if element k is not equal to element i
             add element s to _NODE.
    Your algorithm should be:
    Code:
    for every element i in _node
       for every element k in _NODE
          if element k is equal to element i
              f = 1; break;
      if (!f) insert into _NODE.
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    61
    Quote Originally Posted by matsp View Post
    I thought I had already explained that the current code is broken:
    Code:
    For every element i in _node
       for every element k in _node
          if element k is not equal to element i
             add element s to _NODE.
    Your algorithm should be:
    Code:
    for every element i in _node
       for every element k in _NODE
          if element k is equal to element i
              f = 1; break;
      if (!f) insert into _NODE.
    --
    Mats
    Thank you veyr much , I really appreciate!

    I let you how that goes and post the final algorithm in the forum
    All the best
    Simone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. getline problem
    By scottmanc in forum C++ Programming
    Replies: 9
    Last Post: 04-13-2003, 10:27 PM
  5. what's the difference?
    By iluvmyafboys in forum C++ Programming
    Replies: 13
    Last Post: 02-28-2002, 09:25 PM

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