Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.55 KB | None | 0 0
  1. //  DataTool 0.3
  2. //
  3. //  Calculates 0 -> 10 inputed numbers
  4. //  20/9-2018
  5.  
  6. //  Todo:
  7. //  1)  All the menus with functions and subfunctions
  8. //  2)  Arrange all functions in groups corresponding to their use
  9. //  3)  ALWAYS remember to put that f*eaking & in scanf(); functions, that's what got me to 0.2
  10. //  4)  Remember to use the long form for scanning the array numbers, as they can't be negavite
  11.  
  12. #include <stdio.h>
  13. #include <limits.h>
  14.  
  15. void introMessage();
  16. void longLegendSubMenu();
  17. char navigationPrompt();
  18. void viewSubMenu(unsigned int array[], int arrayLength);
  19. int enterArraySubMenu(unsigned int array[], int arrayLength);
  20. void createComputeArray(unsigned int arrayToCopyFrom[], unsigned int arrayToPasteTo[]);
  21. void computeArraySubMenu(unsigned int array[], int arrayLength);
  22. unsigned int getMaxValueInArray(unsigned int array[], int arrayLength);
  23. unsigned int getMinValueInArray(unsigned int array[], int arrayLength);
  24. double getMeanValueInArray(unsigned int array[], int arrayLength);
  25. void getNormalizedValueInArray(unsigned int array[], int arrayLength, double arrayMeanValue);
  26. void computeArraySubMenuResultDisplay(unsigned int maxValueInArray, unsigned int minValueInArray, double meanValueInArray, unsigned int array[], int arrayLength);
  27.  
  28.  
  29.  
  30. //  Temporary declarations of functions:
  31. void resetArraySubMenu();
  32. int quitSubMenu();
  33.  
  34.  
  35. int main(void)
  36. {
  37.     unsigned int numberArray[10] = {0}, computeArray[10];
  38.     int numberArrayNonZeroElements = 0, programKeepOnSwitch = 1;
  39.     char userNavigationPromptMenuInput;
  40.    
  41.     introMessage();
  42.     longLegendSubMenu();
  43.    
  44.     while(programKeepOnSwitch == 1)
  45.     {
  46.         userNavigationPromptMenuInput = navigationPrompt();
  47.        
  48.         if(userNavigationPromptMenuInput == 'v')
  49.         {
  50.             viewSubMenu(numberArray, numberArrayNonZeroElements);
  51.         }
  52.         else if(userNavigationPromptMenuInput == 'e')
  53.         {
  54.             numberArrayNonZeroElements = enterArraySubMenu(numberArray, numberArrayNonZeroElements);
  55.         }
  56.         else if(userNavigationPromptMenuInput == 'c')
  57.         {
  58.             createComputeArray(numberArray, computeArray);
  59.             computeArraySubMenu(computeArray, numberArrayNonZeroElements);
  60.         }
  61.         else if(userNavigationPromptMenuInput == 'r')
  62.         {
  63.             resetArraySubMenu();
  64.         }
  65.         else if(userNavigationPromptMenuInput == 'q')
  66.         {
  67.             programKeepOnSwitch = quitSubMenu();
  68.         }
  69.         else
  70.             longLegendSubMenu();
  71.     }
  72.    
  73.     return 0;
  74. }
  75.  
  76. void introMessage()
  77. {
  78.     printf("\n\tDataTool 0.3:\n\n");
  79.     printf("\tInput and calculate maximum, minimum, mean and normalized values from a set\n");
  80.     printf("\tof numbers ranging from 0 to 10 total (non-negative integer) number(s)\n");
  81. }
  82.  
  83. void longLegendSubMenu()
  84. {
  85.     printf("\n\tShortcut legend:\n\n");
  86.     printf("\tView (v)    -   Show the current set of numbers\n");
  87.     printf("\tEnter (e)   -   Input the data to compute\n");
  88.     printf("\tCompute (c) -   Perform the computation on the current data set\n");
  89.     printf("\tReset (r)   -   Erases the current data set\n");
  90.     printf("\tQuit (q)    -   Shut down the program\n");
  91.     printf("\tLegend (l)  -   Show the extended shortcut legend\n\n");
  92. }
  93.  
  94. char navigationPrompt()
  95. {
  96.     char navigationPromptMenuInput;
  97.    
  98.     printf("\tWhich menu would you like to enter? Use (v, e, c, r, q) or \"l\" to show shortcut legend  -  ");
  99.     scanf(" %c", &navigationPromptMenuInput);
  100.    
  101.     while(navigationPromptMenuInput != 'v' && navigationPromptMenuInput != 'e'
  102.     && navigationPromptMenuInput != 'c' && navigationPromptMenuInput != 'r'
  103.     && navigationPromptMenuInput != 'q' && navigationPromptMenuInput != 'l')
  104.     {
  105.         printf("\n\tPlease input a valid key (v, e, c, r, q) or use \"l\" to show the shortcut legend  -  ");
  106.         scanf(" %c", &navigationPromptMenuInput);
  107.     }
  108.    
  109.     return navigationPromptMenuInput;
  110. }
  111.  
  112. void viewSubMenu(unsigned int array[], int arrayLength)
  113. {
  114.     if(arrayLength == 0)
  115.     {
  116.         printf("\n\t[  No data stored  ]\n\n");
  117.     }
  118.     else
  119.     {
  120.         printf("\n\t[  ");
  121.        
  122.         for(int i = 0; i < arrayLength; i++)
  123.         {
  124.             printf("%u  ", array[i]);
  125.         }
  126.         if(arrayLength >= 9)
  127.         {
  128.             printf("%u  ", array[9]);
  129.         }
  130.         else{
  131.         }
  132.        
  133.         printf("]\n\n");
  134.     }
  135. }
  136.  
  137. int enterArraySubMenu(unsigned int array[], int arrayLength)
  138. {
  139.     int arrayIndex = arrayLength;
  140.    
  141.     if(arrayLength < 9)
  142.     {
  143.         for(int i = 9; i >= arrayLength; i = i - 1)
  144.         {
  145.             printf("\n\tEnter value # %d: ", arrayIndex + 1);
  146.             scanf(" %u", &array[arrayIndex]);
  147.             arrayIndex = arrayIndex + 1;
  148.        
  149.             if(array[arrayIndex - 1] == 0 || arrayIndex > 9)
  150.             {
  151.                 arrayIndex = arrayIndex - 1;
  152.                 printf("\n");
  153.                 break;
  154.             }
  155.         }
  156.     }
  157.     else
  158.     {
  159.         printf("\n\tData set is full, to input new numbers, please reset it using (r) from the main menu\n\n");
  160.     }
  161.     return arrayIndex;
  162. }
  163.  
  164. void createComputeArray(unsigned int arrayToCopyFrom[], unsigned int arrayToPasteTo[])
  165. {
  166.     for(int i = 0; i < 10; i++)
  167.     {
  168.         arrayToPasteTo[i] = arrayToCopyFrom[i];
  169.     }
  170. }
  171.  
  172. void computeArraySubMenu(unsigned int array[], int arrayLength)
  173. {
  174.     unsigned int highestValue = 0;
  175.     unsigned int lowestValue = 0;
  176.     double meanValue = 0;
  177.    
  178.     highestValue = getMaxValueInArray(array, arrayLength);
  179.     lowestValue = getMinValueInArray(array, arrayLength);
  180.     meanValue = getMeanValueInArray(array, arrayLength);
  181.     getNormalizedValueInArray(array, arrayLength, meanValue);
  182.     computeArraySubMenuResultDisplay(highestValue, lowestValue, meanValue, array, arrayLength);
  183. }
  184.  
  185. unsigned int getMaxValueInArray(unsigned int array[], int arrayLength)
  186. {
  187.     unsigned int maxValue = 0;
  188.    
  189.     for(int i = 0; i < arrayLength; i++)
  190.     {
  191.         if(maxValue < array[i])
  192.         {
  193.             maxValue = array[i];
  194.         }
  195.     }
  196.     return maxValue;
  197. }
  198.  
  199. unsigned int getMinValueInArray(unsigned int array[], int arrayLength)
  200. {
  201.     unsigned int minValue = UINT_MAX;
  202.    
  203.     for(int i = 0; i < arrayLength; i++)
  204.     {
  205.         if(minValue > array[i])
  206.         {
  207.             minValue = array[i];
  208.         }
  209.     }
  210.     return minValue;
  211. }
  212.  
  213. double getMeanValueInArray(unsigned int array[], int arrayLength)
  214. {
  215.     double meanValue = 0., sumOfArrayElements = 0.;
  216.    
  217.     if(arrayLength == 0)
  218.     {
  219.         arrayLength = 1;
  220.     }
  221.    
  222.     for(int i = 0; i < arrayLength; i++)
  223.     {
  224.         sumOfArrayElements = sumOfArrayElements + array[i];
  225.     }
  226.     return (sumOfArrayElements / arrayLength);
  227. }
  228.  
  229. void getNormalizedValueInArray(unsigned int array[], int arrayLength, double arrayMeanValue)
  230. {
  231.     for(int i = 0; i < arrayLength; i++)
  232.     {
  233.         array[i] = array[i] - arrayMeanValue;
  234.     }
  235. }
  236.  
  237. void computeArraySubMenuResultDisplay(unsigned int maxValueInArray, unsigned int minValueInArray, double meanValueInArray, unsigned int array[], int arrayLength)
  238. {
  239.     double normalizedArray[10] = {0}    //KOLLA UPP!!!
  240.    
  241.     if(arrayLength == 0)
  242.     {
  243.         printf("\n\t[  No data stored  ]\n\n");
  244.     }
  245.     else{
  246.    
  247.         printf("\n\tMaximum value from data: %u\n", maxValueInArray);
  248.         printf("\tMinimum value from data: %u\n", minValueInArray);
  249.         printf("\tMean value from data: %.2f\n", meanValueInArray);
  250.        
  251.         printf("\tNormalized value(s) from data:\n");
  252.         printf("\t[  ");
  253.        
  254.         for(int i = 0; i < arrayLength; i++)
  255.         {
  256.             printf("%u  ", array[i]);
  257.         }
  258.         if(arrayLength >= 9)
  259.         {
  260.             printf("%u  ", array[9]);
  261.         }
  262.         else{
  263.         }
  264.        
  265.         printf("]\n\n");
  266.     }
  267. }
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274. //  Temporary functions:
  275. void resetArraySubMenu()
  276. {
  277.    
  278. }
  279. int quitSubMenu()
  280. {
  281.     char userQuitMenuInput;
  282.    
  283.     printf("\n\tAre you sure you want to quit? All changes will be deleted!\n");
  284.     printf("\n\tUse \"y\" to exit, \"n\" to return  (y/n)  -  ");
  285.     scanf(" %c", &userQuitMenuInput);
  286.    
  287.     while(userQuitMenuInput != 'y' && userQuitMenuInput != 'n')
  288.     {
  289.         printf("\n\tPlease input a valid key (y, n). Use \"y\" to exit and \"n\" to resume using the program  -  ");
  290.         scanf(" %c", &userQuitMenuInput);
  291.     }
  292.    
  293.     printf("\n");
  294.    
  295.     if(userQuitMenuInput == 'y')
  296.     {
  297.         return 0;
  298.     }
  299.     else
  300.    
  301.         return 1;
  302.    
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement