# Thread: Can't figure out this mode function

1. ## Can't figure out this mode function

This is going on three days now. For back ground I will explain the rest of the code but won,t post it as the total is two hundred lines. I'm building a statistics program for an assignment. The code letts the user make three choices , 1 is enter data into an array, two is display the data i.e. Max, min, median, mode, variance and standard deviation. The third choice is to exit the program. This mode function works fine if I enter in a bunch of values where there is a mode, but if I enter one value and then display it it seems like it is not doing the test for mode. My original idea for the mode function if there is a mode is to create a countArray and increment the location with similar values further down the array. Does anyone see where I'm going wrong?

Code:
```static int isThereMode(const float *data, int count){

int i,j;
int boolean;
int countArray[200];
float temp[200];
float tempHolder;
//create a temporary array that will be sorted
for(i=0;i<count;i++){
temp[i] = data[i];
}
//sort the temp array from lowest to highest
for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(temp[i]>temp[j]){
tempHolder = temp[j];
temp[j] = temp[i];
temp[i]=tempHolder;
}
}
}

if(count==1){
boolean = 0;

}
//initialize the array to all zeros
for(i=0;i<count;i++)
countArray[i]=0;

for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(data[i]!=data[j]){
printf(" debug there is no equal data in array\n");
boolean = 0;
}
else if(data[i]==data[j]){
boolean = 1;
}
}
}
return boolean;
}

static float modeOfValues(const float *data,int count){
int meanCounter = 0;
int position = 0;
int i,j;
float mode;
int countArray[200];
float temp[200];
float tempHolder;

for(i=0;i<count;i++){
temp[i] = data[i];
}

for(i=0;i<count;i++){
for(j=i+1;j<count;j++){
if(temp[i]>temp[j]){
tempHolder = temp[j];
temp[j] = temp[i];
temp[i]=tempHolder;
}
}
}

for(i=0;i<count;i++)//initialize the array to all zeros
countArray[i]=0;
/*look at the first element in the data array and compare it to the
the next element in the array, if there are matches increment the
countArray location where a match occurs */
for(i=0;i<count;i++){
for(j=i+1;j<=count;j++){
if(data[i] == data[j]){
countArray[i]+=1;
}
}
}
/*check the countArray for the highest number */
for (i=0;i<count;i++){
if(countArray[i] > meanCounter){
meanCounter = countArray[i];
position = i	;
}
}

mode = data[position];

return mode;
}```

2. Do you call isThereMode from your main? It appears that it correctly identifies (in the n=1 case) that there is no mode.

3. the main function is in another file and calls the displayStatistics function if the user enters 2 for their choice.To enter data you enter 1 at the menu and press ctrl-d when your done and get returned to the menu to make your choice. The displayStatistics function calls the isThereMode function and if it passes that test the modeOfValues function gets called. It works fine if I enter a bunch of floats into the array but if I enter only one value press ctrl-d and display the data, enter only one value again, display the data, it doesn't do the test properly for the mode even though there is one. I think it may have something to do with how I'm testing the copied array for presence of similar n=numbers but I'm at a mental block with what is exactly going on.

4. The "proper thing to do" would be to not display anything (a data set of n=1 does not have a mode). Or are you supposed to be combining data sets so now you have n=2?

5. here is the main functions code so that I can better explain what is expected of the program

Code:
```int main() {

float data[SIZE];

int count = 0;

int choice;

printf("Mini-Statitics Package\n");

printf("----------------------\n");

do {

printf("\nThis program will perform the following\n");

printf("1) Enter data\n");

printf("2) Display the data and the following statistics: \n");

printf("   the number of data items, the high and low values in the data,\n");

printf("   the mean, median, mode, variance and standard deviation.\n");

printf("3) Quit the program\n");

scanf("%d", &choice);

getchar();

switch(choice) {

case 1: getData(data, &count);

break;

case 2: displayStatistics(data, count);

break;

case 3: printf("Thank you and good bye!\n");

break;

default: printf("%d is not a valid choice\n", choice);

}

} while (choice != 3);

return 0;

}```
there is an array of floats which is 200 positions. The user can enter as many as they like as long as they don't go over 200 values. The user signals they are done by pressing Ctrl-d and hitting enter. This brings them back to the menu where they can then display the info on the values. If one value is entered it works fine, no mode gets displayed. If I then enter another value, say the first was 1 , I choose to display, I choose to enter in more values, I enter 1 , I choose to display, that displays the mode as 1, which tecnically it shouldn't because the array only contains two ones, there is no greatest amount of values in comparison to others. Now I choose to enetr the number 2. The array now contains 1 1 2. I display and it tells me no mode which is incorrect because the mode should now be 1 , because in comparison the the three values ,1 appears more time then 2. By specification I have to display no mode if there is no mode present.

edit: when the user enters in data after exiting out of the first entering data choice, the data gets entered right after the previous data