So I'm trying to write a basic stats program with arguments as inputs, I've done min, max, and average, and median should be pretty easy, but I'm stuck on mode (most frequently occuring). I want it to eventually be a proper mode function (ie. bi-modal or more, able to return more than one value if ther is more than one mode).
This is my code so far, the logic through the mode function is very convoluted and weird, but its the only way I could think of doing it. Theres also alot of re-declaring ints and such I havent cleaned up. Output for mode atm with this code is always zero =/
any ideas ?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int array[2];
int temp[2][2];
int checkint(char s[]) //Checks if string is purely numerical
{
int length = strlen(s); // Define string length
for (int i = 0; i < length; i++) // For each character
{
if (isdigit(s[i])); // If numerical, then do nothing
else return 0; //If not, then return 0
}
return 1;
}
int* bubble_sort(int *r, int y)
{
int i, j, temp;
for (i = 0; i < y-1; i++);
for (j = 0; j < y-i-1; j++);
if (r[j] > r[j + 1])
{
temp = r[j];
r[j] = r[j + 1];
r[j + 1] = temp;
}
return r;
}
static int maximum(int r[], int y) //Returns maximum integer
{
int f = 0;
for(int i = 0; i < y; i++) //For each int in the array
{
if (r[i] > f) // If it is larger than previously, store it as new f
f = r[i];
else;
}
return f; //Return final f value as maximum
}
static int minimum(int r[], int y) //Returns minimum integer
{
int f = 100000;
for(int i = 0; i < y; i++)// For each int in the array
{
if (r[i] < f) // If it is smaller than previously, store it as new f
f = r[i];
else;
}
return f; //Return final f as minimum
}
static float average(int r[], int y) //Returns average of array as a floating point
{
float f = 0;
for(int i = 0; i < y; i++) //For each int in the array
f = f + r[i]; // Add them together
f = (float)f / y; //Find the average
return f; // And return it
}
int mode(int *r, int y)
{
int count = 1;
int trace = -1;
int max = 0;
bubble_sort(r, y); //Sort the array
temp[0][0] = r[0]; //Add the first int, since it has to be new by default
for(int i = 0; i < y; i++) //For each int in the array
if(r[i+1] != r[i])
{
temp[count][0] = r[i+1]; // Add it to the temp array if it is new
count++;
}
else;
for(int h = 0; h < count; h++) //Initialize temp[][1] to 0
temp[h][1] = 0;
count = 1;
for(int i = 0; i < y; i++) //For each element in the array
if(r[i+1] == r[i]) //If same, then add to count
count++;
else //If not then store count in respective temp array position
{
trace++;
temp[trace][1];
count = 1;
}
for(int i = 0; i < trace; i++) //For each element in temp, test for max
if(temp[i][1] > max)
max = temp[i][1];
//Only return the one value for now, though the infrastructure is there now to return more in the future
return max;
}
int main(int argc, char *argv[])
{
int temp = (argc - 2);
array[temp];
if(argc < 2) //Check if enough arguments have been supplied
{
printf("Too few arguments for this program.\n");
exit(EXIT_FAILURE); //If not then exit
}
else
{
for (int i = 1; i < argc; i++) //For every argument provided (excluding 0)
{
int ans = atoi(argv[i]); //Find the int equivalent
int var = (i-1);
array[var] = ans; //Store it in the array
}
int max = maximum(array, (argc-1)); //Call the functions
int min = minimum(array, (argc-1));
float avrg = average(array, (argc-1));
int *f;
f = &(array[0]);
int mod = mode(f, (argc-1));
printf("The maximum is %d\n", max);
printf("The minimum is %d\n", min);
printf("The average is %6.2f\n", avrg);
printf("The mode is %d\n", mod);
}
// Exit indicating success
exit(EXIT_SUCCESS);
return 0;
}
what im trying to get to in the mode function is a condensed 2-d array which has all the values from the original array in temp[h][0], but with repeated values removed, and with the respective count for each number in the corresponding temp[h][1] position.