analysing code

This is a discussion on analysing code within the C Programming forums, part of the General Programming Boards category; Hey guys I am looking and a piece of code that is masking and finding duplicates and I am trying ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    analysing code

    Hey guys

    I am looking and a piece of code that is masking and finding duplicates and I am trying to figure out what one piece of code does. The piece I don't understand is rc=match<=same.
    Does anyone know what this does exactly and why it is relevant. I have never seen an assignment like this.

    Code:
    #include <stdio.h>
    #define SIZE 5
    int changeKey(int oldKey[], int newKey[], int n, int same) {
        int i, j, rc, match;
    
        match = 0;
        for (i = 0; i < n; i++)
            for (j = 0; j < n; j++)
                if (oldKey[i] == newKey[j])
                    match++;
         rc = match <= same; /*what exactly does this do*/
    	 printf("RC IS %d\n",rc);
    	 printf("MATCH IS %d\n",match);
    	
        if (rc == 1)
            for (i = 0; i < n; i++)
                oldKey[i] = newKey[i];
    			
    
        return rc;
    }
    
    
    void display(int key[], int n) {
        int i;
    
        for (i = 0; i < n; i++)
            printf("%d", key[i]);
        printf("\n");
    }
    
    int main(void) {
        int key[] =      {1,0,2,6,7}, rc;
        int attempt1[] = {2,7,4,5,9};
        int attempt2[] = {1,8,5,2,9};
    
        display(key, SIZE);
        rc = changeKey(key, attempt1, SIZE, 1);
        if (rc == 1)
            printf("Changed to: ");
        else
            printf("Remains as: ");
        display(key, SIZE);
        rc = changeKey(key, attempt2, SIZE, 2);
        if (rc == 1)
            printf("Changed to: ");
        else
            printf("Remains as: ");
        display(key, SIZE);
        rc = changeKey(key, attempt1, SIZE, 3);
        if (rc == 1)
            printf("Changed to: ");
        else
            printf("Remains as: ");
        display(key, SIZE);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    rc = match <= same; /*what exactly does this do*/

    The result of comparison(<=) either 0 or 1 is assigned to rc.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    First look at the precedence of the operators involved:
    <= is higher precedence than =
    Then bracket accordingly:
    rc = (match <= same)
    So first match is compared to same using less-than-or-equal, resulting in a boolean value (1 or 0)
    Then this boolean value is assigned to rc.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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