简体   繁体   中英

Comparing values from two arrays c++

I would like to compare the values stored in two equally sized arrays and report back the comparison values. For some reason the program I have written does not return any output. Any thoughts would be very much appreciated.

#include <iostream>
using namespace std;

int compareTriplets(int a[3],int b[3]) {
    int A=0,B=0,i=0,j=0;
    while (i < 3 && j < 3){
        if (a[i]>b[j]){
            A++;
            i++;
            j++;
        }
        else if (a[i]<b[j]){
            B++;
            i++;
            j++;
        }
        
    }
    int compare[2]={A,B};
    return compare[2];
}
int main(){
    int a[3]={1,2,3}, b[3]={3,2,1};
    cout<<"The comparison scores are: "<<compareTriplets(a,b)<<endl;

}

Your return statement return compare[2] is erroneous for several reasons:

  • You return value is int not an array of int .
  • by return compare[2] you access and return the third element of the array which is undefined behavior.
  • You return a locally allocated array.

In any case, you have also several options for what you can do, Instead of returning an array. you could also pass it to the function and fill the values within it, Moreover, you could use various components of the STL. eg,, std::vector , std::array , std::pair , etc.

Here is a quick snippet on how you could implement this function:

#include <iostream>
#include <array>

void compareTriplets(int a[3], int b[3], int scores[2]) {
    for (int i = 0; i < 3; ++i) {
        if (a[i] > b[i]){
            ++scores[0];
        }
        else {
            ++scores[1];
        }
    }
}

std::array<int, 2> compareTripletsArray(int a[3], int b[3]) {
    int A=0,B=0;
    for (int i = 0; i < 3; ++i) {
        if (a[i] > b[i]){
            ++A;
        }
        else {
            ++B;
        }
    }
    return {A,B};
}

std::pair<int,int> compareTripletsPair(int a[3], int b[3]) {
    int A=0,B=0;
    for (int i = 0; i < 3; ++i) {
        if (a[i] > b[i]){
            ++A;
        }
        else {
            ++B;
        }
    }
    return std::make_pair(A, B);
}

int main(){
    int a[3] = {1,2,3}, b[3] = {3,2,1}, scores[2] = {0,0};
    
    // Case 1: passing the result
    compareTriplets(a, b, scores);
    std::cout<<"The comparison scores are: "<< scores[0] << " and " << scores[1] << std::endl;
    
    // Case 2: returning a std::array
    const std::array<int, 2> scores_array = compareTripletsArray(a, b);
    std::cout<<"The comparison scores (array) are: "<< scores_array[0] << " and " << scores_array[1] << std::endl;
    
    // Case 3: returning a std::pair
    const std::pair<int,int> scores_pair = compareTripletsPair(a, b);
    std::cout<<"The comparison scores (pair) are: "<< scores_pair.first << " and " << scores_pair.second << std::endl;
    
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM