Again, I'm making a program that asks for some data then spits out the mean, median, mode and range. However, my mode fuction doesn't seem to be working as intended, as when I use a debugger, I can see that only the first two values are being used in data_common (aka data_common[0]and data_common[1] contain the correct values but data_common[2] is 0, which is weird as that isn't what is should be set to as default).
Any help on what's going on?
Code:
double mode(double data[], int data_elements){
double data_common[100][2] = { //stores what variables are stored and how many instances there are
{1073709551614}, //random number, incredibly unlikely someone will actually input this
{1073709551614}
};
double most_often = 0; //stores how many times the most common variable shows up
double mode1 = 0; //return value, what variable is the most common
for(int i = 0;i < data_elements;i++)
{
for(int j = 0;j < data_elements;j++) //check if value in data[i] is already in data_common
{
if(data_common[j][0] == data[i])
{
data_common[j][1]++; //if it already exists, add one to how many times it has shown up
}
if(data_common[j][0] == 1073709551614) //execute if data_common[j] hasn't been given a value
{
for(int x = 0; x < data_elements;x++)
{
if(data[i]==data_common[x][0]) //again check if a different value in the array is already storing it
goto LOOP; //if it is, then exit the loop
}
data_common[j][0]=data[i]; //if not, give data_common[j] the value of data[i]
data_common[j][1]=1; //make sure we know that that value has shown up once before
}
LOOP:;
}
}
for(int i = 0; i < data_elements;i++) //check every variable
{
if(data_common[i][1] > most_often) //if value is greater than current highest
{
most_often = data_common[i][1]; //set new highest
mode1 = data_common[i][0]; //make sure we get the number which has the most occurences
}
}
return mode1; //return the number with most occurences
}
main.h
main.c