Thread: Comparing two arrays

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Comparing two arrays

    I have an assigment that basically asks the program to read from a file "data.txt". This file has 50 values. So it is neccesary to make two arrays, the first one should take the first 25 values and the second array should take the remaining 25. Then compare them and give a result when the numbers are the same for the same suscript.


    So it needs to read the same value at the same position for each array, that's where i get my problem :/

    I get the following for the function (which is what i lack) in order to compare them:

    Code:
    int equal_arrays(int a1 [], int a2 [], int n) 
    {
    	int i, result;
    		for (i=0; i<n; ++i)
    			if (a1[i] != a2[i]) result = 0;
    			else result = 1;
    		
    	return (result);
    }
    but.. I have no clue how to compare the arrays so it gives me the number of the one that repeats on both of them. I'm sure that what I've done so far is somewhere close to the end. I've been trying to make it work and think of alot of things but seems that none of them work. Any help would be really appreciated. Thanks in advance.
    Last edited by Gonz; 03-23-2011 at 01:29 AM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Location
    China
    Posts
    74
    that comparing function is foolish. it returns 1 when the last elements is equal.
    Code:
    // the correct one:
    int equal_arrays(int a1 [], int a2 [], int n) 
    {
    	int i, result;
    		for (i=0; i<n; ++i)
    			if (a1[i] != a2[i])
                                    return 0;
    	return (1);
    }
    or you can simply use memcmp() to compare two arrays.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    Quote Originally Posted by RichSelian View Post
    that comparing function is foolish. it returns 1 when the last elements is equal.
    or you can simply use memcmp() to compare two arrays.
    Okay so I got the following from some reasearch i did. Not sure if its okay.

    Code:
    #include <string.h>
    ....
           int x[26], t[26];
    ....
           result = memcmp (x,t,26);
           if (result == 0)
    	 printf ("%d",x);
           return (0);
    }
    never mind got it to work with the old code. I noticed that if I loop it for the result = 1 i can compare them until the loop is done. Will post the code in a while because it might need more work to do :/
    Last edited by Gonz; 03-23-2011 at 01:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing string arrays
    By never_lose in forum C Programming
    Replies: 17
    Last Post: 03-20-2011, 01:38 AM
  2. Comparing elements of character pointer arrays
    By axe in forum C Programming
    Replies: 2
    Last Post: 11-14-2007, 12:20 AM
  3. Comparing Arrays
    By Raison in forum C++ Programming
    Replies: 10
    Last Post: 04-20-2004, 02:21 PM
  4. comparing character arrays
    By Neildadon in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2003, 05:29 AM
  5. Comparing Character Arrays
    By LostNotFound in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2003, 12:42 PM