The compiler print b,a and b,how??? | Sololearn: Learn to code for FREE!

+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)

7/29/2021 3:01:08 PM

Shruti Shukla

+2

Why the code is so complicated to understand?? I think the for loop in second half Of code need to be used carefully.

+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

+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 ........?

0

0

Shruti Shukla Yes but because of e = 7 for loop will work 3 times.

0

Okay

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.

0

But if we remove ++e line ,the compiler still run 3 times

0

Shruti Shukla No it is running 2 times. Try to take input like this: 2 1 2 3 1 3 2

0

Okay