The compiler print b,a and b,how??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

The compiler print b,a and b,how???

#include<iostream> using namespace std; int main(){ int n,e=0,w=2,s=3; cin>>n; int arr[n*3]; for(int i=0;i<n;i++){ for(int p=0;p<3;p++){ cin>>arr[e]; ++e; } } ++e; for(int p=0;p<e;p++){ int y; y=arr[w]; int a; int b; if(arr[p]>y){ a=arr[p]-y; } else{ a=y-arr[p]; } if(arr[p+1]>y){ b=arr[p+1]-y; } else{ b=y-arr[p+1]; } if(a>b){ cout<<"b"<<endl; } else if(b>a){ cout<<"a"<<endl; } else if(b==a){ cout<<"c"; } p=p+2; w=(s*2)-1; s+=3; } return 0; } input is =2(1,2,3),(1,3,2)

29th Jul 2021, 3:01 PM
Shruti Shukla
Shruti Shukla - avatar
10 Answers
+ 2
Why the code is so complicated to understand?? I think the for loop in second half Of code need to be used carefully.
29th Jul 2021, 3:20 PM
ln e
+ 1
If you have written this code then you should know how? What output do you expect? If you write clean code like this then you can easily debug problem. Explained in code. I have explained for b now check for a and b https://code.sololearn.com/csyeiVqueC39/?ref=app
29th Jul 2021, 3:13 PM
A͢J
A͢J - avatar
+ 1
I mean to say that in a loop, p value will increase by 3 and in the input the elements is 6 .so, how compiler running 3 times ........?
29th Jul 2021, 4:02 PM
Shruti Shukla
Shruti Shukla - avatar
0
P++
29th Jul 2021, 4:19 PM
Shruti Shukla
Shruti Shukla - avatar
0
Shruti Shukla Yes but because of e = 7 for loop will work 3 times.
29th Jul 2021, 4:21 PM
A͢J
A͢J - avatar
0
Okay
29th Jul 2021, 4:24 PM
Shruti Shukla
Shruti Shukla - avatar
0
Shruti Shukla Check my edited code there is ++e after taking input in an array. So after taking input in an array e will be 6 but after ++e, e will be 7 and next loop will work till 3 times.
29th Jul 2021, 4:25 PM
A͢J
A͢J - avatar
0
But if we remove ++e line ,the compiler still run 3 times
29th Jul 2021, 4:27 PM
Shruti Shukla
Shruti Shukla - avatar
0
Shruti Shukla No it is running 2 times. Try to take input like this: 2 1 2 3 1 3 2
29th Jul 2021, 4:28 PM
A͢J
A͢J - avatar
0
Okay
29th Jul 2021, 4:29 PM
Shruti Shukla
Shruti Shukla - avatar