Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Clayton Colwell
- //10.2.12
- //Assignment 6
- #include <stdio.h>
- #include <stdlib.h>
- double** create2dArray(int, int); //Create array of width, height
- int getMenuChoice(int, int, char[]); //Get int between x and y with prompt
- int compare_doubles(const void*, const void*); //Compare doubles for sort
- double calcMean(double*, int); //Calculate mean of dataset of size
- double calcStdDev(double*, int); //Calculate std dev of dataset of size
- double** calcMode(double*, int); //Calculate mode of dataset of size (Mode may be multiple values=>array)
- double calcRange(double*, int); //Calculate range of dataset of size
- int main() {
- const int MAX_DATA = 50; //Max size of stats array
- int userN = 0; //# of points user wants to enter
- int menuSelection = 0; //Menu Selection 1Mean,2Std.Dev,3Mod,4Range,5Restart/Exit
- //Welcome and get userN (Loop until valid)
- printf("############# Welcome to the Statistical Calculator #############\n");
- printf("Note: This program can only accept up to %d data points.\n", MAX_DATA);
- userN = getMenuChoice(1, MAX_DATA, "\nNumber of data points: ");
- //Declare data array
- double* dataSet = calloc(userN - 1, sizeof(double*));
- //Fill array
- int inputCount;
- for(inputCount = 0; inputCount < userN; inputCount++) {
- printf("Input %d: ", inputCount + 1);
- scanf("%lf", &dataSet[inputCount]);
- }
- //Display menu
- printf("\nOptions:");
- printf("\n(1) Mean");
- printf("\n(2) Standard Deviation");
- printf("\n(3) Mode");
- printf("\n(4) Range");
- printf("\n(5) Restart/Exit");
- menuSelection = getMenuChoice(1,5,"\nSelection: ");
- //Perform operation
- switch(menuSelection) {
- case 1:
- printf("MEAN: %f", calcMean(dataSet, userN));
- break;
- case 2:
- printf("STANDARD DEVIATION: %f", calcStdDev(dataSet, userN));
- break;
- case 3:
- printf("MODE: ");
- calcMode(dataSet, userN);
- break;
- case 4:
- printf("RANGE: %f", calcRange(dataSet, userN));
- break;
- case 5:
- printf("RESTART/EXIT: ");
- break;
- }
- free(dataSet);
- getch();
- }
- double** create2dArray(int arrayWidth, int arrayHeight) {
- //Make 2D array
- double **array = malloc(arrayHeight*sizeof(double*));
- //Loop through each row of above and created 'zeroed' arrays for 'width'
- int i;
- for(i=0;i<arrayHeight;i++) {
- array[i] = calloc(arrayWidth, sizeof(double));
- }
- return array;
- }
- int getMenuChoice(int lowerBound, int upperBound, char prompt[]) {
- int input = lowerBound - 1;
- while(input == lowerBound - 1) {
- printf("%s", prompt);
- scanf("%d", &input);
- if(input < lowerBound || input > upperBound) {
- input = lowerBound - 1;
- printf("ERROR! Value is not between %d and %d.\n", lowerBound, upperBound);
- }
- }
- return input;
- }
- int compare_doubles(const void *a, const void *b) {
- const double *da = (const double *) a;
- const double *db = (const double *) b;
- return (*da > *db) - (*da < *db);
- }
- double calcMean(double* array, int size) {
- double mean = 0;
- int i;
- for(i = 0; i < size; i++)
- mean += array[i];
- mean = mean/size;
- return mean;
- }
- double calcStdDev(double* array, int size) {
- double mean = calcMean(array, size);
- double stdDev = 0;
- int i;
- for(i=0;i<size;i++)
- stdDev += (array[i]-mean)*(array[i]-mean);
- stdDev = stdDev/(size - 1);
- return stdDev;
- }
- double** calcMode(double* array, int size) {
- int modeArrayWidth = (size + 1)/2; //Abs. max width of mode array
- qsort(array, size, sizeof(double), compare_doubles); //Sort data
- double** modeInfo = create2dArray(modeArrayWidth, 2); //WHY U CRASH?!?!
- //Find mode(s)
- int i;
- static nextModeIndex = 0;
- for(i=0; i<modeArrayWidth; i++) {
- if(array[i] == array[i+1]) {
- int checker;
- int index = -1;
- for(checker=0; checker<modeArrayWidth; checker++) {
- if(modeInfo[checker][0] == array[i]) {
- index = checker;
- checker = modeArrayWidth;
- printf("INME");
- }
- }
- if(index == -1) {
- printf("HI");
- modeInfo[nextModeIndex][0] = array[i];
- modeInfo[nextModeIndex][1] += 2.0;
- nextModeIndex++;
- }
- else {
- printf("HELLO");
- modeInfo[index][1] += 1.0;
- nextModeIndex = index + 1;
- }
- }
- }
- for(i=0;i<modeArrayWidth;i++)
- printf("%f %f\n", modeInfo[i][0], modeInfo[i][1]);
- return modeInfo;
- }
- double calcRange(double* array, int size) {
- qsort(array, size, sizeof(double), compare_doubles);
- return array[size-1] - array[0];
- }
Add Comment
Please, Sign In to add comment