Array bounds: your loop for (int i = 0; i <= n; ++i) is going to cause you all sorts of trouble because you're accessing out of bounds on xi and yi. arrays in c++ are zero-indexed, so you should loop from 0 to n-1. Like this: for (int i = 0; i < n; ++i).
This fixes the issue.