Guest User

Untitled

a guest
Dec 13th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. //Clayton Colwell
  2. //10.2.12
  3. //Assignment 6
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. double** create2dArray(int, int); //Create array of width, height
  9.  
  10. int getMenuChoice(int, int, char[]); //Get int between x and y with prompt
  11. int compare_doubles(const void*, const void*); //Compare doubles for sort
  12. double calcMean(double*, int); //Calculate mean of dataset of size
  13. double calcStdDev(double*, int); //Calculate std dev of dataset of size
  14. double** calcMode(double*, int); //Calculate mode of dataset of size (Mode may be multiple values=>array)
  15. double calcRange(double*, int); //Calculate range of dataset of size
  16.  
  17.  
  18. int main() {
  19. const int MAX_DATA = 50; //Max size of stats array
  20. int userN = 0; //# of points user wants to enter
  21. int menuSelection = 0; //Menu Selection 1Mean,2Std.Dev,3Mod,4Range,5Restart/Exit
  22.  
  23. //Welcome and get userN (Loop until valid)
  24. printf("############# Welcome to the Statistical Calculator #############\n");
  25. printf("Note: This program can only accept up to %d data points.\n", MAX_DATA);
  26. userN = getMenuChoice(1, MAX_DATA, "\nNumber of data points: ");
  27.  
  28. //Declare data array
  29. double* dataSet = calloc(userN - 1, sizeof(double*));
  30.  
  31. //Fill array
  32. int inputCount;
  33. for(inputCount = 0; inputCount < userN; inputCount++) {
  34. printf("Input %d: ", inputCount + 1);
  35. scanf("%lf", &dataSet[inputCount]);
  36. }
  37.  
  38. //Display menu
  39. printf("\nOptions:");
  40. printf("\n(1) Mean");
  41. printf("\n(2) Standard Deviation");
  42. printf("\n(3) Mode");
  43. printf("\n(4) Range");
  44. printf("\n(5) Restart/Exit");
  45. menuSelection = getMenuChoice(1,5,"\nSelection: ");
  46.  
  47. //Perform operation
  48. switch(menuSelection) {
  49. case 1:
  50. printf("MEAN: %f", calcMean(dataSet, userN));
  51. break;
  52. case 2:
  53. printf("STANDARD DEVIATION: %f", calcStdDev(dataSet, userN));
  54. break;
  55. case 3:
  56. printf("MODE: ");
  57. calcMode(dataSet, userN);
  58. break;
  59. case 4:
  60. printf("RANGE: %f", calcRange(dataSet, userN));
  61. break;
  62. case 5:
  63. printf("RESTART/EXIT: ");
  64. break;
  65. }
  66.  
  67. free(dataSet);
  68. getch();
  69. }
  70.  
  71. double** create2dArray(int arrayWidth, int arrayHeight) {
  72. //Make 2D array
  73. double **array = malloc(arrayHeight*sizeof(double*));
  74. //Loop through each row of above and created 'zeroed' arrays for 'width'
  75. int i;
  76. for(i=0;i<arrayHeight;i++) {
  77. array[i] = calloc(arrayWidth, sizeof(double));
  78. }
  79. return array;
  80. }
  81.  
  82. int getMenuChoice(int lowerBound, int upperBound, char prompt[]) {
  83. int input = lowerBound - 1;
  84. while(input == lowerBound - 1) {
  85. printf("%s", prompt);
  86. scanf("%d", &input);
  87. if(input < lowerBound || input > upperBound) {
  88. input = lowerBound - 1;
  89. printf("ERROR! Value is not between %d and %d.\n", lowerBound, upperBound);
  90. }
  91. }
  92. return input;
  93. }
  94.  
  95. int compare_doubles(const void *a, const void *b) {
  96. const double *da = (const double *) a;
  97. const double *db = (const double *) b;
  98. return (*da > *db) - (*da < *db);
  99. }
  100.  
  101. double calcMean(double* array, int size) {
  102. double mean = 0;
  103. int i;
  104. for(i = 0; i < size; i++)
  105. mean += array[i];
  106. mean = mean/size;
  107. return mean;
  108. }
  109.  
  110. double calcStdDev(double* array, int size) {
  111. double mean = calcMean(array, size);
  112. double stdDev = 0;
  113. int i;
  114. for(i=0;i<size;i++)
  115. stdDev += (array[i]-mean)*(array[i]-mean);
  116. stdDev = stdDev/(size - 1);
  117. return stdDev;
  118. }
  119.  
  120. double** calcMode(double* array, int size) {
  121. int modeArrayWidth = (size + 1)/2; //Abs. max width of mode array
  122. qsort(array, size, sizeof(double), compare_doubles); //Sort data
  123. double** modeInfo = create2dArray(modeArrayWidth, 2); //WHY U CRASH?!?!
  124.  
  125. //Find mode(s)
  126. int i;
  127. static nextModeIndex = 0;
  128. for(i=0; i<modeArrayWidth; i++) {
  129. if(array[i] == array[i+1]) {
  130. int checker;
  131. int index = -1;
  132. for(checker=0; checker<modeArrayWidth; checker++) {
  133. if(modeInfo[checker][0] == array[i]) {
  134. index = checker;
  135. checker = modeArrayWidth;
  136. printf("INME");
  137. }
  138. }
  139. if(index == -1) {
  140. printf("HI");
  141. modeInfo[nextModeIndex][0] = array[i];
  142. modeInfo[nextModeIndex][1] += 2.0;
  143. nextModeIndex++;
  144. }
  145. else {
  146. printf("HELLO");
  147. modeInfo[index][1] += 1.0;
  148. nextModeIndex = index + 1;
  149. }
  150. }
  151. }
  152. for(i=0;i<modeArrayWidth;i++)
  153. printf("%f %f\n", modeInfo[i][0], modeInfo[i][1]);
  154. return modeInfo;
  155. }
  156.  
  157. double calcRange(double* array, int size) {
  158. qsort(array, size, sizeof(double), compare_doubles);
  159. return array[size-1] - array[0];
  160. }
Add Comment
Please, Sign In to add comment