Write the definition of a function, isReverse, whose first two parameters are arrays of integers of equal size, and whose third parameter is an integer indicating the size of each array. The function returns true if and only if one array is the reverse of the other. (“Reverse” here means same elements but in reverse order.)

2
Write the definition of a function, isReverse, whose first two parameters are arrays of integers of equal size, and whose third parameter is an integer indicating the size of each array. The function returns true if and only if one array is the reverse of the other. (“Reverse” here means same elements but in reverse order.)

LANGUAGE: C++

CHALLENGE:

Write the definition of a function, isReverse, whose first two parameters are arrays of integers of equal size, and whose third parameter is an integer indicating the size of each array. The function returns true if and only if one array is the reverse of the other. (“Reverse” here means same elements but in reverse order.)

SOLUTION:


bool isReverse (int one[], int two[], int size){
    int matches=0;
    for (int k=0; k<size; k++){
        if (one[k] == two[size-k-1]){
            matches++;
        }
    }
    if (matches==size){
        return true;
    }else{
        return false;
    }
}

2 comments

  • Ben Large says:

    Here is the code for C:

    int isReverse (int one[], int two[], int size){
    int matches=0;
    int k;
    for (k=0; k<size; k++){
    if (one[k] == two[size-k-1]){
    matches++;
    }
    }
    if (matches==size){
    return 1;
    }else{
    return 0;
    }
    }

  • Don says:

    bool isReverse( int a[], int b[], int c)
    {
    for(int i=0; i<c; i++)
    {
    if(a[i]!=b[c-1-i])
    return false;
    else
    true;
    }
    return true;
    }
    for anyone who is doing the assignment, this is the best way to do it

Add comment