1

I am creating the function, compare_arrays to check if elements inside the pre-defined arrays are similar. I want to use pointer and pass the arrays by reference into the function. This is the function I coded so far but pylint is giving me an error for my second if loop stating that *arry1 and *arry2 are not defined. Can someone help me understand this error better.

Also, how do I call the function and pass in arr_one, arr_two, etc.....

#include <iostream>

using namespace std;

int size1 = 3, size2 = 3;
int arr_one[] = {1, 2, 3};
int arr_two[] = {1, 2, 3};



int main(){

    bool compare_arrays( int *arry1, int size1, int *arry2, int size2);{
        if (size1 != size2){
            return false;
        }
        for(int i=0; i < size1; i++);{
            if (*arry1 != *arry2);{
                return false;
            }
        }
        *arry1++;
        *arry2++;
        return true;
        }
}
1
  • Defining functions within the body of functions is not allowed in C++ - it's only a typo (a semi-colon) which turns the function definition into a basic declaration (i.e. a declaration that is not a definition) but, because of that, the names of function arguments have no relationship to the variables being used. You claim there is an error on your "second loop, but there is only one loop in the program. Voting to close as your problems is little more than a set of basic typos. Commented Feb 26, 2019 at 5:01

2 Answers 2

2

The biggest and main problem in your code is the inclusion of the ; at the end of compare_arrays. What you're actually doing is declaring a function, compare_arrays, then creating a separate scope, which is why arry1 is not defined. If you remove this semi-colon, your error will start to make more sense:

bool compare_arrays( int *arry1, int size1, int *arry2, int size2) {

Now the compiler will tell you you cannot define an function within a function, which is easy to solve. Move compare_arrays outside of main:

bool compare_arrays( int *arry1, int size1, int *arry2, int size2){
    if (size1 != size2){
        return false;
    }
    for(int i=0; i < size1; i++);{
        if (*arry1 != *arry2);{
            return false;
        }
    }
    *arry1++;
    *arry2++;
     return true;
}

int main() {
}

This will now compile, but probably not run as you expect, because you have an extra erroneous semi-colon at the end of your second if statement. What this means is the semi-colon is treated as the expression from the if, then you'll always return false;. Fix this by removing the semi-colon:

for (int i = 0; i < size1; i++) {

And now you can simply call your code in main like so:

int main() {
    if (compare_arrays(arr_one, size1, arr_two, size2)) {
        std::cout << "arrays are equal\n";
    }
}

However, you can trivialise the whole thing by using std::vector:

#include <vector>
#include <iostream>
int main() {
    std::vector<int> v1 = { 1, 2, 3 };
    std::vector<int> v2 = { 1, 2, 3 };
    if (v1 == v2)
        std::cout << "arrays are equal\n";
}
Sign up to request clarification or add additional context in comments.

Comments

0

You put compare_arrays function body inside the main function. remove the body of the function out the main and just call it from inside the main function. Also, you put the semi-colon after if statement and function header and that's wrong. your code should be like this

  #include <iostream>

using namespace std;

int size1 = 3, size2 = 3;
int arr_one[] = { 1, 2, 3 };
int arr_two[] = { 1, 2, 3 };

bool compare_arrays(int *arry1, int size1, int *arry2, int size2) {
    if (size1 != size2) {
        return false;
    }
    for (int i = 0; i < size1; i++) {
        if (*arry1 != *arry2) {
            return false;
        }
    }
    *arry1++;
    *arry2++;
    return true;
}

int main() {
    cout << compare_arrays(arr_one, size1, arr_two, size2) << endl;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.