Compare the Triplets in C Programming
Compare the triplets in c programming.
In this article, you will learn how to compare the triplets in c programming.
Example
Enter size of the array::
3
Enter the 3 elements of the first array::
17 28 30
Enter the 3 elements of the second array::
99 16 8
Triplate position:: 2 1
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C
while
statement - C Array
Source Code
// Compare the Triplets in C Programming
#include <stdio.h>
int main() {
int s=0;
printf("Enter size of the array::\n");
scanf("%d", &s);
int arr1[s], arr2[s];
int i=0;
printf("\nEnter the %d elements of the first array::\n", s);
while (i<s) {
scanf("%d", &arr1[i]);
i++;
}
i=0;
printf("\nEnter the %d elements of the second array::\n", s);
while (i<s) {
scanf("%d", &arr2[i]);
i++;
}
// It will compare the triplates
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
printf("\nTriplate position:: %d %d", x1, x2);
return 0;
}
Output
Enter size of the array::
3
Enter the 3 elements of the first array::
17 28 30
Enter the 3 elements of the second array::
99 16 8
Triplate position:: 2 1
Explanation
In this given program we have taken 3
elements of the first array are 17 28 30
and 3
elements of the second array are 99 16 8
. 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 less than the second array so x2 got the 1 point.
- arr1[1] > arr2[1], the first array's element is greater than the second array so x1 got the 1 point.
- arr1[2] > arr2[2], the first array's element is greater than the second array so x1 got the 1 point.
After comparison, x1 has 2 points and x2 has 1 point.