-2

I am working on a triplet code actually. The problem statement goes as follow: The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].

If a[i] > b[i], then Alice is awarded 1 point. If a[i] < b[i], then Bob is awarded 1 point. If a[i] = b[i], then neither person receives a point. Comparison points is the total points a person earned. The result has to be a two valued array. The code appears correct and fine to me, but iam not getting the desired and correct output. Somebody please help. The code i wrote is as follow:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int main(){
    int a[3];
    int b[3];
    int r[2] = {r[0],r[1]};
    r[0]=0;
    r[1]=0;
    cin>>a[3]>>b[3];
    for(int i=0;i<3;i++){
            if(a[i] >b[i]){
                r[0]=r[0]+1;
            }else if(b[i]>a[i]){
                r[1] = r[1]+1;

            }else{
                r[0]=r[0]+0;
                r[1]=r[1]+0;
            }
        }
    cout<<r[0]<<" "<<r[1];
}
Jerusha G
  • 21
  • 2
  • 7
    What do you expect this to mean? `int r[2] = {r[0],r[1]};` – gspr Oct 26 '20 at 09:49
  • 5
    `cin>>a[3]>>b[3];` is outside the bounds of the array. It has indices 0 to 2. You need a loop if you're going to read multiple values like the `for` loop you have below that. You don't actually need any arrays for this at all. Read 2 values, compare, score, repeat. – Retired Ninja Oct 26 '20 at 09:49
  • 1
    Please also read [Why should I not `#include `?](https://stackoverflow.com/Questions/31816095/Why-Should-I-Not-Include-Bits-Stdc-H.) – Ted Lyngmo Oct 26 '20 at 09:50
  • `a[3]` does not mean "the entire array `a`". If it existed, it would be a single `int`. – molbdnilo Oct 26 '20 at 09:54

1 Answers1

1

The solution of this question is easy. First You need to take array value using loop. Then compare them. http://cpp.sh/3kk5f

// Example program
#include <iostream>
using namespace std;

int main()
{
    int a[3],b[3],r[2]={};

    for(int i=0;i<3;i++) cin>>a[i];
    for(int i=0;i<3;i++) cin>>b[i];
    
    for(int i=0;i<3;i++){
     if(a[i] > b[i])   r[0]++;
     else if(b[i]>a[i]) r[1]++;
    }
    
    cout<<r[0]<<" "<<r[1]<<endl;
  
}

input:

1 2 3
1 4 5

output:

0 2
Sayed Sohan
  • 1,385
  • 16
  • 23