Thread: Comparing Elements of 2 arrays

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    18

    Unhappy Comparing Elements of 2 arrays

    I'm a bit lost on this assignment, not sure why i can't seem to get it. I understand the proccess I will have to take once I start the program, but the problem is that I'm not real sure where to start.

    I'm supposed to have 2 arrays A and B, each containing 10 integers which can either be hard coded into the program or taken by the user (I'm assuming hardcoding them is a bit more simple). I then need to write a function that tests if every element of array A is equal to it's corresponding element in array B. Basically, it has to check if A[0] is equal to B[0], A[1] is equal to B[1], and so on. If all of the elements are equal the function should return True, if at least one element is not equal it should return FALSE.

    When i get home from work I will post the code i have been working on so far, but it's not much. Any help until then would be incredibly appreciated. I don't have much time to turn this in and I've been working doubles all weekend, trying to program when i get home after midnight. Like i sai ANY HELP would be a blessing, Thank You in advance to anyone who may be abe to help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would reach for a for loop to solve this problem.
    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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Code:
    #include <stdio.h>
    #define ARY_SIZE 10
    
    int main (void)
    {
    //Local Declarations
    	int numbersA[ARY_SIZE];
    	int numbersB[ARY_SIZE];
    	int i;
    	int j;
    
    //Statements
    	printf("Please Enter 10 Integers For Array A\n");
    	for (int i = 0; i < ARY_SIZE; i++)
    		scanf("%d", &numbersA[i]);
    	
    	printf("\nPlease Enter 10 Integers For Array B\n");
    	for (int j = 0; j < ARY_SIZE; j++)
    		scanf("%d", &numbersB[j]);
    	
    	for (int i = 0; i < ARY_SIZE; i++)
    	   {
    		for (int j = 0; j < ARY_SIZE; j++)
    			{
    			if (numbersA[0] == numbersB[0])
    				return true;
    			}
    		}
    	return 0;
    }
    This is what I have so far and i'm stuck as to knowing whether I need to test each corresponding element in the loop and how i can return if it comes up false in the process. Def a newbiw here, so constructive criticism will still be appreciated
    Last edited by SupraMan; 11-23-2010 at 02:23 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    As you stated yourself: write a function other than the main function to perform this task. Call that function from the main function.

    You do not need nested for loops.
    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

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    i'm sorry, you lost me there, i never said anything aout using a function outside of main. here is a updated version of my code, tell me if i'm getting anywhere, or just tell me wtf im supposed to do because this is getting old...

    Code:
    #include <stdio.h>
    #define ARY_SIZE 10
    
    int main (void)
    {
    //Local Declarations
    	int numbersA[ARY_SIZE];
    	int numbersB[ARY_SIZE];
    	int i;
    	int j;
    	bool allMatch = 0;
    
    //Statements
    	printf("Please Enter 10 Integers For Array A\n");
    	for (int i = 0; i < ARY_SIZE; i++)
    		scanf("%d", &numbersA[i]);
    	
    	printf("\nPlease Enter 10 Integers For Array B\n");
    	for (int j = 0; j < ARY_SIZE; j++)
    		scanf("%d", &numbersB[j]);
    
    		if (numbersA[0] == numbersB[0] && numbersA[1] == numbersB[1] && numbersA[2] == numbersB[2] && 
    			numbersA[3] == numbersB[3] && numbersA[4] == numbersB[4] && numbersA[5] == numbersB[5] && 
    			numbersA[6] == numbersB[6] && numbersA[7] == numbersB[7] && numbersA[8] == numbersB[8] && 
    			numbersA[9] == numbersB[9])
    		{	
    			allMatch = 1;	
    		}
    		else {
    			allMatch = 0;
    			}
    		if (allMatch == 1)
    			printf("TRUE\n");
    		else
    			printf("FALSE\n");
    	return true;
    	}
    can you see anything wrong with what i've done here? I think I finally got it to work, whether it looks good or not. Let me know if i can simplify this in any way!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SupraMan
    i'm sorry, you lost me there, i never said anything aout using a function outside of main.
    In that case, you should explain what you wrote:
    Quote Originally Posted by SupraMan
    I then need to write a function that tests if every element of array A is equal to it's corresponding element in array B. Basically, it has to check if A[0] is equal to B[0], A[1] is equal to B[1], and so on. If all of the elements are equal the function should return True, if at least one element is not equal it should return FALSE.
    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

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by SupraMan View Post
    can you see anything wrong with what i've done here? I think I finally got it to work, whether it looks good or not. Let me know if i can simplify this in any way!
    Plenty wrong. That if() statement compares all of the elements of the two arrays. However, the first time through the loop, only numbersB[0] is initialised (it is the only element of numbersB read in). All of the other comparisons other than "numbersA[0] == numbersB[0]" give what the C standard calls undefined behaviour - anything is allowed to happen and, practically, "anything" includes things you don't want to happen (program crashes, etc).

    Since you are doing a loop over j that reads numbersB[j], why not do a comparison of numbersA[j] with numbersB[j]?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    111
    SupraMan: laserlight's comment is to the point - you said yourself that the function should return TRUE or FALSE depending on whether or not all the elements of the two arrays were equal. That means, logically, that you need a function outside of main. Main is the main function (duh!), meaning it will run and terminate when the program completes. Successfull termination in C is indicated by returning zero (0). If you want your program to check for a TRUE or FALSE return value, you have to call a function from the main function, and this function will do the comparison and return TRUE/FALSE (to the main function). What you're attempting to do with the above is not returning TRUE/FALSE, but just printing "TRUE" or "FALSE" on the console. If "return TRUE or FALSE" is the wording in your assignemnt, you can be sure that they want you to use a function.
    Last edited by cnewbie1; 11-23-2010 at 06:42 AM.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    I really do appreciate all of this advice, but the problem is that i'm obviously not sure exactly how to accomplish this or I would've done it in the first place. with all of the information given, i need some sort of example to push me in the right direction. I stated that im new to this, as in, this is the first programming class that I have ever taken, which means, i need things to actually be explained to me if theyre going to help at all. If people don't have enough time, i completely understand, but at the same time, if theres time to explain all of it in words, there should be enough time to give examples, because in actuality, that is what's going to help me! Please, now i even have LESS time to turn in this project so i have to put a turbo on it. I tested every aspect of the program and it returns True or False in response to the Boolean 0 or 1 from what the requirements are, but apparently it's still incorrect. Just to make itclear, the program does not compare the arrays completely ONLY THE CORRESPONDING ELEMENTS! That is what has made this code so difficult for me to create from scratch, because there are literally no examples of this process in my book or online that I could find. The only elements being compared are those of the same index.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    I understand now that i must call a function, but i'm not so good at calling functions as of right now, it's one thing i havent been able to grasp very well up to this point, so if anyone could please show me how i should be setting up the call and what function i should be using to compare corresponding elements for equality, would be most appreciated! I pretty much had to put this program together without any prior knowledge of Loops in arrays because of a family emergency i had during the assignment period of this project,

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SupraMan
    Just to make itclear, the program does not compare the arrays completely ONLY THE CORRESPONDING ELEMENTS!
    Yes. Ironically, this should actually be easier than what you did.

    Here's an idea: put aside what you are doing right now, and write a program that uses a for loop to print the integers from 0 to 9, inclusive. That is, you must not manually print the integers, but rather print i, where i is the loop index. Post that program for us to see.

    After that, adapt this program that prints the integers from 0 to 9 to print the ten elements of the array a from index 0 to index 9, i.e., to print a[0], a[1], ..., a[9], using the for loop. That is, you must not manually list out the elements to be printed, but rather print a[i], where i is the array index.

    When you are done, you should be much closer to accomplishing your task of comparing the corresponding elements of the arrays a and b, i.e., to compare a[0] with b[0], a[1] with b[1], ..., a[9] with b[9].
    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

  12. #12
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    I just stayed up until 5am writing that mess of a program above these threads, and i have work and school all day today. I'm going to continue to try and learn these concepts, but right now i'm afraid im just not going to get this assignment done by tonight at midnight, becuz i in fact work until midnight. Thanks anyways for your help, but i just didn't put enough time aside to learn these steps before writing the program. Unfortunately I didn't have your help BEFORE i stayed up all night writing this program that should be amazingly simple. I'll keep posting on this thread though to show how im learning it

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    To give you a visual on what LaserLight is telling you....

    This....
    Code:
    		if (numbersA[0] == numbersB[0] && numbersA[1] == numbersB[1] && numbersA[2] == numbersB[2] && 
    			numbersA[3] == numbersB[3] && numbersA[4] == numbersB[4] && numbersA[5] == numbersB[5] && 
    			numbersA[6] == numbersB[6] && numbersA[7] == numbersB[7] && numbersA[8] == numbersB[8] && 
    			numbersA[9] == numbersB[9])
    		{	
    			allMatch = 1;	
    		}
    		else {
    			allMatch = 0;
    			}
    		if (allMatch == 1)
    			printf("TRUE\n");
    		else
    			printf("FALSE\n");
    	return true;
    Can be replaced with...

    Code:
    allmatch =  true;
    for (int i = 0; i < 10; i++)
      { if (arraya[i] != arrayb[i]
        {allmatch = false;
          break; } }

  14. #14
    Registered User
    Join Date
    Nov 2010
    Posts
    18
    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
    	for (int i = 0; i <= 9; i++)
    		printf("%d\n", i);
    	return 0;
    }
    there's the simple for statment printing 10 integers from 0-9. Now to attempt to apply them to my situation!

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good. Just a slight modification:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        for (int i = 0; i < 10; i++)
            printf("%d\n", i);
        return 0;
    }
    It makes no difference here, but it helps when you are dealing with an array of 10 elements.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Compare elements in arrays
    By axe in forum C Programming
    Replies: 13
    Last Post: 11-16-2007, 03:04 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. Replies: 7
    Last Post: 04-13-2003, 10:53 PM
  4. ? comparing elements of a char array
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 09-20-2001, 07:55 AM
  5. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM