charles
charles

Reputation: 313

Function not reading a 2D array's [i][0] row

Consider the following array :

arr[0][0] = 5.42; // val
arr[0][1] = (int)1;    // freq
arr[1][0] = 41.45;
arr[1][1] = (int)1; // freq
arr[2][0] = 10.55;
arr[2][1] = (int)1;// freq
arr[3][0] = 78.55;
arr[3][1] = (int)1;// freq

it is supposed to represent a (non cumulative) frequency table. so each value has a frequency of 1.

Now consider the following function :

      const int SIZE = 1;
    double standardDeviation(double list[][SIZE], double mean, int size) {
    double sumSquared = NULL;
    double sum = NULL;
    double sumFx = NULL;
    int sumFreq = NULL;
    for(int i = 0; i < size; i++)
        sumFreq += list[i][1]; 

    return sumFreq;
    }

(obviously there's some code missing) but in the present code, my objective is to returned the sum of the frequencies, which should be 4, but it's returning values only from members of the array with index [i][0] - basically, the values.

Any ideas why?

Upvotes: 0

Views: 57

Answers (2)

Carl Norum
Carl Norum

Reputation: 224864

Your array isn't being accessed correctly, since you have:

const int SIZE = 1;

You need that to be:

const int SIZE = 2;

As an aside, none of these initializations make sense:

double sumSquared = NULL;
double sum = NULL;
double sumFx = NULL;
int sumFreq = NULL;

NULL is 0, yes, but semantically it's intended to be used to indicate a null pointer, and none of those variables are pointers.

Upvotes: 2

Ben Voigt
Ben Voigt

Reputation: 283624

The parameter is declared as double list[][1].

So list[i][1] is out of bounds.

Upvotes: 1

Related Questions