Car Problem in vector without using algorithm


unable to get the shortest result of cars.

Tell me the problem statement, or what you want to do, so that i can debug it accordingly.

Sir i dont want to use sort function over there so i am trying without inlude just find out where i am wrong

See now, now the logic is as per your comparator function:

Sir, It is similar as I have already
0 1
1 2
3 4
5 6
7 8 this result i want to show but it is giving output same as i did.

Here’s your code bro, never forget to pass by reference if you want to swap. Have edited your comparator function just for simplicity. Now you can edit as per your need. All the best

Firstly Thank you So much.Once i thought that could be mistake of reference but we are passing array so i thought it will take automatically as ref.
like we do in array
void swap(int a,int b)
{
int temp=a;
a=b;
b=temp}
int main(){
int arr[3]={1,2,3};
swap(arr[1],arr[0]);
}
if we do like this then it will fine am i right??

I usually don’t recommend to swap two values by call by value method. Also if you would do like this, without passing by reference. I don’t think it will get swapped.
Arr[0] will be 1 and arr[1] will be 2 only.wait letme show you something.

1 Like

See this, now got the concept?
pass-by-reference-vs-pass-by-value-animation

1 Like

Got it. I have an idea about it. but my concern is in the array we have already as a ref so directly it can be swap (in case of the array only) work as I sent earlier

When we make a call by passimg a function let function be f and array be arr
So if you will do

F(datatype *a){
//line 1
//line 2
}
Int main(){
//line 1
//line 2
F(arr)
}

Call be reference will be only for char data type. Not sure for int data type.

1 Like

Ok Sir And Thank You :slight_smile:

See this


So if you do it with pointer it does behave like call by reference. Also in the code i have given to you. Have done operator overloading. If in case it’s nit understandable. Then there’s video by prateek bhaiya on YouTube where he has shown making comparator function via bubble sort you can watch that too.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.