Hello,

I'm having trouble with one of my coding assignments. I have to compare 2 arrays to see if they're identical. My code compiles fine, but i'm not getting the correct results. Mind you I'm in a beginner's course so I cannot have any advanced coding, I appreciate those suggestions, but again I cannot use them in my assignment.

#include <iostream>
#include <iomanip>
using namespace std;

bool compare_arrays(int a[], int b[]);


int main()
{
    int i;
    int list1[5];
    int list2[5];

    cout << "Please enter a series of numbers (up to 10) for your first array: " << endl;
    cin >> list1[5];
    cout << "Please enter a series of numbers (up to 10) for your second array: " << endl;
    cin >> list2[5];

    cout << "The arrays are: "
         << compare_arrays(list1, list2)
         << endl;

         return 0;
}

bool compare_arrays(int a[], int b[])
{

    bool answer = true;




    for (int i = 0; i < a[i]; i++)
    {

        if(a[i] == b[i])
        {   
            return true;
        }

        else 
            return false;

    }




}

When I enter my array values, I keep getting "The arrays are: 0" I don't know what I'm doing wrong

Recommended Answers

All 14 Replies

Your function returns true if the first elemenet of the first array is equal to the first element of the second array and false otherwise. That is, it always stops after the first iteration of the loop returns a result based just on that one iteration (because when a return is executed, the execution of the function is aborted).

That's not right. If you see that an item in the first list is different from the corresponding item in the other list, it's actually okay to return false right away: if one item is different, that's enough to say that the lists are different - so not looking at the rest of the list is okay in that case. But in case of returning true it's not okay. You can't just say that the two lists are equal because some of their items are equal - all their items (that is all items in the same position) need to be equal. So you definitely need to go through the whole list before you can return true. This means that the return true must be outside the loop.

But the loop still needs to be implemented to scan the entire array correct? I messed around with the code and now it's returning value 32

But the loop still needs to be implemented to scan the entire array correct?

Right, you still need the loop. What you need to change is that return true should happen after the loop, not during, so that it actually makes sure that all the elements are the same before it returns true.

I messed around with the code and now it's returning value 32

The only way that your function could possibly return a value other than true (1) or false (0) would be if the end of the function was reached without executing any return statement. In that case the behavior would be undefined and you'd basically get a random return value.

Try this:

bool compare_arrays(int a[], int b[], int maxcnt)
{
    bool answer = true;
    for (int i = 0; i < maxcnt && answer == true; i++)
    {
        answer = (a[i] == b[i]);
    }
    return answer;  // Note that you should always have a default return value.
}

Remember to pass the array size (5 in your example) as the 3rd argument to compare_arrays().

I've tried everything and still nothing. Whatever I do I keep getting "The arrays are: 0"

You appear to be trying to store all the array like this:

cin >> list1[5];

This means "take something from the keyboard and store it in the sixth element of the array named list1". Tell me, what is the size of that array? Does it have a sixth element? Shouldn't you be storing a number in the first element list1[0], and another number in the second element list1[1], and so on?

ok so I changed my cin >> to cin >> list1[i], once I enter my first input I get a stack access violation error when running the exe. I'm assuming that has something to do with pointers, but I'm not supposed to use pointers yet

Change this line cin >> list1[5]; to the following:

for(int i = 0; i < 5; i ++)
cin >> list1[i];

Similarly change for list2 as well.

But then how I would I get out of the loop

But then how I would I get out of the loop

for(int i = 0; i < 5; i ++)
cin >> list1[i];

This loop ends by itself when i reaches 5. That's how a for loop works.

but I'm not supposed to use pointers yet

This list1[i] is actually this *(list1+i) You're already using pointers. It's just made a bit easier for you.

I mean I understand that, but when I enter it it won't ask for the next set of numbers

Have you tried making it ask, using cout?

for(int i = 0; i < 5; i ++)
{
  cout << "Number please: ";
  cin >> list1[i];
}

I think my problem is my actual bool function. When I run the program, it always prints they are not equal.

#include <iostream>
#include <iomanip>
using namespace std;

bool compare_arrays(int a[], int b[], int size);


int main()
{
    int i;
    int list1[5] = {0};
    int list2[5] = {0};
    int size;
    int setofnum1;
    int setofnum2;

    cout << "Please enter a series of numbers (up to 10) for your first array: " << endl;

    cin >> setofnum1;

    cout << "Please enter a series of numbers (up to 10) for your second array: " << endl;

     cin >> setofnum2;

    if (compare_arrays(list1, list2, size))
        cout << "The arrays are equal" << endl;


    else
        cout << "The arrays are not equal" << endl;


         return 0;
}

bool compare_arrays( int a[], int b[], int size)
{
    bool answer = true;
  for ( int i = 0; i < size; ++i)
  {
    if (a[i] != b[i])
    return false;
  }

  return answer;
}

So what I don't know is if it's the function or my if statement towards the end of my main, or the way the numbers are being inputted. I tried the block of code you posted before the:

for(int i = 0; i < 5; i ++)
{
  cout << "Number please: ";
  cin >> list1[i];
}

but it just keeps asking Number Please over and over. Now I'm not advanced or anything, but getting to the they are not equal is the programming going through the entire thing and reaching there, is the for loop even checking the iterations in the array, or is it not even reading the input.

I'd also like to say thanks for helping me out.

In the code in the post immediately before this, you never set size.

Also, you set values in setofnum2 and setofnum1, but you ask your function to compare list1 and list2.

This loop works perfectly.

    cout << "Please enter a series of 5 numbers for your first array: " << endl;

    for (int i =0; i<5; i++)
    {
        cout << "Enter value: " << endl;
        cin >> list1[i];
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.