Compare the Triplets C++ Program
Compare the triplets c++ program.
In this article, you will learn how to compare the triplets c++ program.
Example
Enter size of the array::
4
Enter the 4 elements of the first array::
12 34 56 9
Enter the 4 elements of the second array::
6 34 78 10
Triplate position:: 1 2
You should have knowledge of the following topics in c++ programming to understand these programs:
- C++
main()
function - C++
while
loop - C++ Array
Source Code
// Compare the Triplets C++ Program
#include <iostream>
using namespace std;
int main() {
int s=0;
cout << "Enter size of the array::\n";
cin >> s;
int arr1[s], arr2[s];
int i=0;
cout << "\nEnter the " << s << " elements of the first array::\n";
while (i<s) {
cin >> arr1[i];
i++;
}
i=0;
cout << "\nEnter the " << s << " elements of the second array::\n";
while (i<s) {
cin >> arr2[i];
i++;
}
// It will compare the triplets
int x1=0, x2=0, l=s;
while (l--) {
if (arr1[l] > arr2[l]) x1++;
if (arr1[l] < arr2[l]) x2++;
}
// It will print the final output
cout << "\nTriplate position:: " << x1 << " " << x2;
return 0;
}
Output
Enter size of the array::
4
Enter the 4 elements of the first array::
12 34 56 9
Enter the 4 elements of the second array::
6 34 78 10
Triplate position:: 1 2
Explanation
In this given program we have taken 4
elements of the first array are
and 12 34 56 9
4
elements of the second array are 6 34 78 10
. Then we compared the first array's element with the second array's element in these positions:
- arr1[0] < arr2[0], the first array's element is greater than the second array so x2 got the 1 point.
- arr1[1] > arr2[1], the first array's element is equal to the second array so they got 0 points.
- arr1[2] > arr2[2], the first array's element is less than the second array so x2 got the 1 point.
- arr1[3] > arr2[3], the first array's element is less than the second array so x2 got the 1 point.
After comparison, x1 has 1 point and x2 has 2 points.