scanf("%f", &k);

Your second scanf() should be this ^^

Don't typecast malloc()/calloc() > FAQ > Casting malloc - Cprogramming.com



int x= 0, k = 0;
/* ... */