What is the issue with my code?

https://ide.codingblocks.com/s/207270 It gets stuck when printing vector

Hello @tusharnitharwal,

You are getting segmentation error for printing the elements of vector.
Reason:
invalid memory location i.e. index access.
There is no relation of size of vector with the value of x.

I have corrected your code:

Hope, this would help.
Give a like if you are satisfied.

I had already successfully submittef the question using the sum variable. I just want to know why am i getting a segmentation fault

Hello @tusharnitharwal,

There are many reason for getting segmentation fault in C++.
In your case, as you were accessing an invalid memory that was out of the compiler’s access as you were taking x as size. So, it was leading to that error.

BTW @tusharnitharwal,

have you reopened this doubt?