Advertisement
Guest User

Untitled

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