 # Vector outputting garbage

Hi! I’m trying to use vectors for the first time for a problem here. According as I have read about it, I’ve written the following code. It gives me wrong output and when I display the content of vector it shows me lots of random stuff. Can anybody help as for why is this thing happening? you are taking i=1 to i=a.size in the printing loop. however the index of vector starts from 0. take i=0 to i<a.size()

In the loop

``````for(int i=1;i<=N;i++)
{
scanf("%llu",&r);
v.push_back(r);
}
``````

push_back pushes the element to the first available index.
Initially,the vector is empty,so the first element is pushed to v.
The loop for i=1 to N means that you are pushing N elements from v to v[n-1].It doesn’t matter if you use loop as i=2 to N+1 , the loop still implies you have to push n elements in the vector.

Hi Pranjal! Thanks! It worked. But I don’t understand why. I started taking input from position 1 then why is it considering 0?

u started taking the input from position 1, yes.! but there is a difference here. if this is an array u can input the first number to a. but here, u simply write a.push_back() without mentioning that push_back should input the number to a so your input loop runs n times, takes n inputs and inserts it into the vector starting from position 0 to n-1. that’s y it dint work