vendredi 26 février 2021

How to compare the elements on two arrays on C++

A c ++ problem where I have to determine if the user hit the target. After entering the coordinates of where enemies coulb be, the user then enters more coordinates and I must compare them, print YES if the element[i] of the attacks matches any element of enemies [n]. I know that I'm comparing positions and not elements that's why it's not working but I'm lost.I also tried to solve it by making only one array but it felt better this way.

    #include <iostream>
    using namespace std;

    int main()
    {
    int n, k, b;
    int enemies[];
    int attacks[];
    
    cin>>n;
    for (int i=0; i<n; i++) {
        cin>>b;
        enemies[i]=b;
    }
    
    cin>>k; 
    for (int i=0; i<k; i++) {
        cin>>b;
        atacks[i]=b;
    }
    
    for(int i=0; i<k; i++){
       if(atacks[i]==enemies[i]){
                cout<<"YES"<<endl;
       }
       else{
           cout<<"NO"<<endl;
       }
    
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire