Yes. The number of pixels is not
double num_pixels = x_range_max * y_range_max;
You didn't subtract the minimum of the range so you're assuming far too many pixels and dividing by a value that's too large.
Corrected:
double num_pixels = (x_range_max - x_range_min) * (y_range_max - y_range_min);
@andy identified another problem in the code that is probably not causing black pixels but will cause you to just take the middle pixel instead of the average of the pixels in a square.
There is yet another problem in your code that causes it to take an area that's smaller than you intended.
You should change your for loops to:
for (int k = x_range_min; k <= x_range_max; k++) {
for (int j = y_range_min; j <= y_range_max; j++) {
And your calculation for the number of pixels to:
double num_pixels = (x_range_max - x_range_min + 1) * (y_range_max - y_range_min + 1);
With your current implementation, you're missing the right and bottom-most pixels. I.e. you're doing this (after you have applied @andy's fixed - before that you're just taking 'S'):
P P
P S