C++ how to convert from a dense matrix to sparse matrix?

I have a matrix class whose data layout is like this (like Armadillo matrix, for example)

int n_rows, n_cols;
float* data;
float& at(int row, int col) {return data[row * n_cols + col];}

From an object like this, how can I compute P_x, P_nnz, P_i, P_p ?

c_float P_x[3] = {4.0, 1.0, 2.0, };
c_int P_nnz = 3;
c_int P_i[3] = {0, 0, 1, };
c_int P_p[3] = {0, 1, 3, };

Thank you.