Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DataTool 0.3
- //
- // Calculates 0 -> 10 inputed numbers
- // 20/9-2018
- // Todo:
- // 1) All the menus with functions and subfunctions
- // 2) Arrange all functions in groups corresponding to their use
- // 3) ALWAYS remember to put that f*eaking & in scanf(); functions, that's what got me to 0.2
- // 4) Remember to use the long form for scanning the array numbers, as they can't be negavite
- #include <stdio.h>
- #include <limits.h>
- void introMessage();
- void longLegendSubMenu();
- char navigationPrompt();
- void viewSubMenu(unsigned int array[], int arrayLength);
- int enterArraySubMenu(unsigned int array[], int arrayLength);
- void createComputeArray(unsigned int arrayToCopyFrom[], unsigned int arrayToPasteTo[]);
- void computeArraySubMenu(unsigned int array[], int arrayLength);
- unsigned int getMaxValueInArray(unsigned int array[], int arrayLength);
- unsigned int getMinValueInArray(unsigned int array[], int arrayLength);
- double getMeanValueInArray(unsigned int array[], int arrayLength);
- void getNormalizedValueInArray(unsigned int array[], int arrayLength, double arrayMeanValue);
- void computeArraySubMenuResultDisplay(unsigned int maxValueInArray, unsigned int minValueInArray, double meanValueInArray, unsigned int array[], int arrayLength);
- // Temporary declarations of functions:
- void resetArraySubMenu();
- int quitSubMenu();
- int main(void)
- {
- unsigned int numberArray[10] = {0}, computeArray[10];
- int numberArrayNonZeroElements = 0, programKeepOnSwitch = 1;
- char userNavigationPromptMenuInput;
- introMessage();
- longLegendSubMenu();
- while(programKeepOnSwitch == 1)
- {
- userNavigationPromptMenuInput = navigationPrompt();
- if(userNavigationPromptMenuInput == 'v')
- {
- viewSubMenu(numberArray, numberArrayNonZeroElements);
- }
- else if(userNavigationPromptMenuInput == 'e')
- {
- numberArrayNonZeroElements = enterArraySubMenu(numberArray, numberArrayNonZeroElements);
- }
- else if(userNavigationPromptMenuInput == 'c')
- {
- createComputeArray(numberArray, computeArray);
- computeArraySubMenu(computeArray, numberArrayNonZeroElements);
- }
- else if(userNavigationPromptMenuInput == 'r')
- {
- resetArraySubMenu();
- }
- else if(userNavigationPromptMenuInput == 'q')
- {
- programKeepOnSwitch = quitSubMenu();
- }
- else
- longLegendSubMenu();
- }
- return 0;
- }
- void introMessage()
- {
- printf("\n\tDataTool 0.3:\n\n");
- printf("\tInput and calculate maximum, minimum, mean and normalized values from a set\n");
- printf("\tof numbers ranging from 0 to 10 total (non-negative integer) number(s)\n");
- }
- void longLegendSubMenu()
- {
- printf("\n\tShortcut legend:\n\n");
- printf("\tView (v) - Show the current set of numbers\n");
- printf("\tEnter (e) - Input the data to compute\n");
- printf("\tCompute (c) - Perform the computation on the current data set\n");
- printf("\tReset (r) - Erases the current data set\n");
- printf("\tQuit (q) - Shut down the program\n");
- printf("\tLegend (l) - Show the extended shortcut legend\n\n");
- }
- char navigationPrompt()
- {
- char navigationPromptMenuInput;
- printf("\tWhich menu would you like to enter? Use (v, e, c, r, q) or \"l\" to show shortcut legend - ");
- scanf(" %c", &navigationPromptMenuInput);
- while(navigationPromptMenuInput != 'v' && navigationPromptMenuInput != 'e'
- && navigationPromptMenuInput != 'c' && navigationPromptMenuInput != 'r'
- && navigationPromptMenuInput != 'q' && navigationPromptMenuInput != 'l')
- {
- printf("\n\tPlease input a valid key (v, e, c, r, q) or use \"l\" to show the shortcut legend - ");
- scanf(" %c", &navigationPromptMenuInput);
- }
- return navigationPromptMenuInput;
- }
- void viewSubMenu(unsigned int array[], int arrayLength)
- {
- if(arrayLength == 0)
- {
- printf("\n\t[ No data stored ]\n\n");
- }
- else
- {
- printf("\n\t[ ");
- for(int i = 0; i < arrayLength; i++)
- {
- printf("%u ", array[i]);
- }
- if(arrayLength >= 9)
- {
- printf("%u ", array[9]);
- }
- else{
- }
- printf("]\n\n");
- }
- }
- int enterArraySubMenu(unsigned int array[], int arrayLength)
- {
- int arrayIndex = arrayLength;
- if(arrayLength < 9)
- {
- for(int i = 9; i >= arrayLength; i = i - 1)
- {
- printf("\n\tEnter value # %d: ", arrayIndex + 1);
- scanf(" %u", &array[arrayIndex]);
- arrayIndex = arrayIndex + 1;
- if(array[arrayIndex - 1] == 0 || arrayIndex > 9)
- {
- arrayIndex = arrayIndex - 1;
- printf("\n");
- break;
- }
- }
- }
- else
- {
- printf("\n\tData set is full, to input new numbers, please reset it using (r) from the main menu\n\n");
- }
- return arrayIndex;
- }
- void createComputeArray(unsigned int arrayToCopyFrom[], unsigned int arrayToPasteTo[])
- {
- for(int i = 0; i < 10; i++)
- {
- arrayToPasteTo[i] = arrayToCopyFrom[i];
- }
- }
- void computeArraySubMenu(unsigned int array[], int arrayLength)
- {
- unsigned int highestValue = 0;
- unsigned int lowestValue = 0;
- double meanValue = 0;
- highestValue = getMaxValueInArray(array, arrayLength);
- lowestValue = getMinValueInArray(array, arrayLength);
- meanValue = getMeanValueInArray(array, arrayLength);
- getNormalizedValueInArray(array, arrayLength, meanValue);
- computeArraySubMenuResultDisplay(highestValue, lowestValue, meanValue, array, arrayLength);
- }
- unsigned int getMaxValueInArray(unsigned int array[], int arrayLength)
- {
- unsigned int maxValue = 0;
- for(int i = 0; i < arrayLength; i++)
- {
- if(maxValue < array[i])
- {
- maxValue = array[i];
- }
- }
- return maxValue;
- }
- unsigned int getMinValueInArray(unsigned int array[], int arrayLength)
- {
- unsigned int minValue = UINT_MAX;
- for(int i = 0; i < arrayLength; i++)
- {
- if(minValue > array[i])
- {
- minValue = array[i];
- }
- }
- return minValue;
- }
- double getMeanValueInArray(unsigned int array[], int arrayLength)
- {
- double meanValue = 0., sumOfArrayElements = 0.;
- if(arrayLength == 0)
- {
- arrayLength = 1;
- }
- for(int i = 0; i < arrayLength; i++)
- {
- sumOfArrayElements = sumOfArrayElements + array[i];
- }
- return (sumOfArrayElements / arrayLength);
- }
- void getNormalizedValueInArray(unsigned int array[], int arrayLength, double arrayMeanValue)
- {
- for(int i = 0; i < arrayLength; i++)
- {
- array[i] = array[i] - arrayMeanValue;
- }
- }
- void computeArraySubMenuResultDisplay(unsigned int maxValueInArray, unsigned int minValueInArray, double meanValueInArray, unsigned int array[], int arrayLength)
- {
- double normalizedArray[10] = {0} //KOLLA UPP!!!
- if(arrayLength == 0)
- {
- printf("\n\t[ No data stored ]\n\n");
- }
- else{
- printf("\n\tMaximum value from data: %u\n", maxValueInArray);
- printf("\tMinimum value from data: %u\n", minValueInArray);
- printf("\tMean value from data: %.2f\n", meanValueInArray);
- printf("\tNormalized value(s) from data:\n");
- printf("\t[ ");
- for(int i = 0; i < arrayLength; i++)
- {
- printf("%u ", array[i]);
- }
- if(arrayLength >= 9)
- {
- printf("%u ", array[9]);
- }
- else{
- }
- printf("]\n\n");
- }
- }
- // Temporary functions:
- void resetArraySubMenu()
- {
- }
- int quitSubMenu()
- {
- char userQuitMenuInput;
- printf("\n\tAre you sure you want to quit? All changes will be deleted!\n");
- printf("\n\tUse \"y\" to exit, \"n\" to return (y/n) - ");
- scanf(" %c", &userQuitMenuInput);
- while(userQuitMenuInput != 'y' && userQuitMenuInput != 'n')
- {
- printf("\n\tPlease input a valid key (y, n). Use \"y\" to exit and \"n\" to resume using the program - ");
- scanf(" %c", &userQuitMenuInput);
- }
- printf("\n");
- if(userQuitMenuInput == 'y')
- {
- return 0;
- }
- else
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement