I have written a code for comparing the triplet problem. The problem statement is:
Input: 2 arrays of size 3
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 point. If a[i] < b[i]
then Bob is awarded the point. If a[i] == b[i]
then neither person receives the point. Comparison points is the total points the person earned. Given a
and b
determine their respective comparison points.
The code I wrote is:
#include<stdio.h>
void main(){
int i, alice[3], bob[3];
int a = 0;
int b = 0;
for(i=0; i<3; i++){
scanf("%d", &alice[i]);
}
for(i=0; i<3; i++){
scanf("%d", &bob[i]);
}
for(i=0; i<3; i++){
if(alice[i] > bob[i])
a++;
else if (alice[i] < bob[i])
b++;
}
printf("%d %d", a, b);
}
But when I put the two scanf()s in one line,
for(i=0; i<3; i++){
scanf("%d", &alice[i]);
scanf("%d", &bob[i]);
}
The output is like 2 1 or 1 2 for all the inputs. Is it wrong to put two scanf()
calls in a single for loop? I couldn't understand what is the reason behind this problem? Would someone kindly explain the reason?
The first version reads three values for alice
and then three values for bob
. The second version reads one value for alice
, then one for bob
, and repeats that 3 times.
If the entered numbers are 1, 2, 3, 4, 5, 6, then in the first example, alice
gets 1, 2, 3 while bob
gets 4, 5, 6; in the second example, alice
gets 1, 3, 5 while bob
gets 2, 4, 6. Quite different results.
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.