Sorted 2d Array, casting error's when calculating median

The array is sorted correctly. My issue is I'm receiving errors in the median calculation. In some examples it is mentioned that there is an issue with the elements of the array being initialized as a double with regards to % operator and needs to be cast as an int. I tried re-casting my element variable num_elements to int, which silenced the error in the if condition, but had no effect on the calculations involving the array elements. I also read about there being an issue with the array subscripts, but re-casting the array doesn't eliminate the error's either. I'm confused as to why this is not an issue in the sorting for loop, but when I try to calculate the median it is?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  double median_func(double weather_array[MAX][COL], double num_elements, int column)
{
    
    double median = 0;
    double temp =   0;
    
    for(int i = 0; i < num_elements - 1; i++)
    {
        for(int j = i + 1; j < num_elements; j++)
        {
            if(weather_array[i][column] > weather_array[j][column])
            {
                temp = weather_array[j][column];
                weather_array[j][column] = weather_array[i][column];
                weather_array[i][column] = temp;
            }
            
        }
        
    }
    /*for(int i = 0; i < num_elements; i++)
     {
     cout << "sorted array " << weather_array[i][1] << endl;
     }*/
    
    if(num_elements % 2 == 0)// invalid operands to binary expression ('double' and 'double')
    {
        median = (weather_array[num_elements/2] + weather_array[(num_elements/2) - 1])/2;// array subscript not an integer
    }
    else
    {
        median = weather_array[num_elements/2];// array subscript not an integer
    }
    
    delete [] weather_array;
    return median;
}
The error is quite clear: array subscripts shpuld be integers. What type your num_elements is? It should be int.
Topic archived. No new replies allowed.