diff --git a/PA02-0913/StateUtilities.c b/PA02-0913/StateUtilities.c index 0234d21..f7ace2c 100644 --- a/PA02-0913/StateUtilities.c +++ b/PA02-0913/StateUtilities.c @@ -7,6 +7,8 @@ const int GROWTH_BLOCK_SIZE = 10; // missing constants from assignment file const int ITEM_BLOCK_SIZE = 6; const int MAJORITY_BLOCK_SIZE = 10; +const char EQUALS = '='; +const char PLUS = '+'; // functions @@ -33,12 +35,21 @@ bool addElementFromData( StateDataType *stateArr, int *size, int capacity, double stateGrowth, char politicalMajority ) { // check to see if the size will go over the capacity - // put in the data to the array - // array[*size] = parameter - // string is a little different. - // use strcpy(array[*size].name, stateName) - // increment size (*size = *size + 1) - // return true + if(*size <= capacity) + { + // put in the data to the array + // array[*size] = parameter + stateArr[*size].population = statePop; + stateArr[*size].growth = stateGrowth; + stateArr[*size].majority = politicalMajority; + // string is a little different. + // use strcpy(array[*size].name, stateName) + strcpy(stateArr[*size].name, stateName); + // increment size (*size = *size + 1) + *size = *size + 1; + // return true + return true; + } // otherwise, return false. return false; } @@ -77,6 +88,7 @@ Dependencies: as needed, including free StateDataType *clearStateArray( StateDataType *array ) { // use free to deallocate the array + free(array); return NULL; } @@ -98,7 +110,7 @@ void copyArray( StateDataType *dest, const StateDataType *src, int srcSize ) // no return, void } -/* COMPLETE +/* Name: createStateArray Process: dynamically allocates array memory Function input/parameters: initial capacity (int) @@ -111,10 +123,12 @@ Dependencies: as needed, including function malloc and keyword sizeof StateDataType *createStateArray( int initialCapacity ) { // define variables (integer, StateDataType) + StateDataType *stateArray; // use malloc to dynamically allocate the array, // use the initialCapacity as the size to multiply by. + stateArray = malloc(sizeof(StateDataType) * initialCapacity); // return the pointer to the array. - return NULL; + return stateArray; } /* @@ -131,8 +145,12 @@ Dependencies: as needed, including strcpy void deepCopy( StateDataType *dest, const StateDataType src ) { // *dest.population = src.population (for each piece) + dest->population = src.population; + dest->growth = src.growth; + dest->majority = src.majority; // strcpy for the string // strcpy(*dest.name, src.name) + strcpy(dest->name, src.name); // no return, void } @@ -151,6 +169,122 @@ Dependencies: as needed */ void displayData( const StateDataType *array, int size, const char *subTitle ) { + int countStates; + // iterate through array to size, check if zero. + if(size != 0) + { + // create a table + // print PIPE character + printCharacter(PIPE); + // print ITEM justified center + printStringJustified("Item", ITEM_BLOCK_SIZE, "CENTER"); + // print PIPE character + printCharacter(PIPE); + // print STATE NAME justified center + printStringJustified("State Name", NAME_BLOCK_SIZE, "CENTER"); + // print PIPE character + printCharacter(PIPE); + // print POPULATION justified center + printStringJustified("Population", POPULATION_BLOCK_SIZE, "CENTER"); + // print PIPE character + printCharacter(PIPE); + // print GROWTH justified center + printStringJustified("Growth", GROWTH_BLOCK_SIZE, "CENTER"); + // print PIPE character + printCharacter(PIPE); + // print MAJORITY justified center + printStringJustified("Majority", MAJORITY_BLOCK_SIZE, "CENTER"); + // print PIPE character + printCharacter(PIPE); + // print new line + printEndline(); + // printing the top border piece + // print PIPE character + printCharacter(PIPE); + // print amount of = for ITEM + printCharacters(ITEM_BLOCK_SIZE, EQUALS); + // print PLUS (needs constant) character + printCharacter(PLUS); + // print amount of = for STATE NAME + printCharacters(NAME_BLOCK_SIZE, EQUALS); + // print PLUS character + printCharacter(PLUS); + // print amount of = for POPULATION + printCharacters(POPULATION_BLOCK_SIZE, EQUALS); + // print PLUS character + printCharacter(PLUS); + // print amount of = for GROWTH + printCharacters(GROWTH_BLOCK_SIZE, EQUALS); + // print PLUS character + printCharacter(PLUS); + // print amount of = for MAJORITY + printCharacters(MAJORITY_BLOCK_SIZE, EQUALS); + // print PIPE character + printCharacter(PIPE); + // print new line + printEndline(); + for(countStates = 0; countStates < size; countStates++) + { + // use the constants to format the table + // print PIPE character + printCharacter(PIPE); + // print item justified right (size) + printIntegerJustified(countStates + 1, ITEM_BLOCK_SIZE, "RIGHT"); + // print PIPE character + printCharacter(PIPE); + // print state name justified left + printStringJustified(array[countStates].name, NAME_BLOCK_SIZE, "LEFT"); + // print PIPE character + printCharacter(PIPE); + // print population justified right + printIntegerJustified(array[countStates].population, POPULATION_BLOCK_SIZE, "RIGHT"); + // print PIPE character + printCharacter(PIPE); + // print growth justified right, need a % sign too. (requires some math trickery to make work) + printDoubleJustified(array[countStates].growth, PRECISION_2, GROWTH_BLOCK_SIZE - 2, "RIGHT"); + printCharacter('%'); + printCharacter(SPACE); + // print PIPE character + printCharacter(PIPE); + // print majority justified center, also requires some math trickery to make work + printCharacters((MAJORITY_BLOCK_SIZE - 1) / 2, SPACE); + printCharacter(array[countStates].majority); + printCharacters((MAJORITY_BLOCK_SIZE - 1) / 2, SPACE); + // print PIPE character + printCharacter(PIPE); + // print endline + printEndline(); + // printing for the spacer lines + // print PIPE character + printCharacter(PIPE); + // print amount of DASH for item + printCharacters(ITEM_BLOCK_SIZE, DASH); + // print PLUS character + printCharacter(PLUS); + // print amount of DASH for state name + printCharacters(NAME_BLOCK_SIZE, DASH); + // print PLUS character + printCharacter(PLUS); + // print amount of DASH for population + printCharacters(POPULATION_BLOCK_SIZE, DASH); + // print PLUS character + printCharacter(PLUS); + // print amount of DASH for growth + printCharacters(GROWTH_BLOCK_SIZE, DASH); + // print PLUS character + printCharacter(PLUS); + // print amount of DASH for majority + printCharacters(MAJORITY_BLOCK_SIZE, DASH); + // print PIPE character + printCharacter(PIPE); + } + } + else + { + //print out "Data no found - Display aborted". + printString("Data not found - Display aborted"); + printEndline(); + } // no return, void } @@ -172,8 +306,12 @@ Dependencies: as needed bool findStateByName( StateDataType *foundElement, const StateDataType *array, int size, const StateDataType searchValue ) { - // - return NULL; + // iterate through array + // check to see if the state name is found (use searchValue) + // copy the element out of the array, put in foundElement + // return true + // otherwise, return false + return false; } /* @@ -195,7 +333,12 @@ Dependencies: as needed bool removeStateByName( StateDataType *removedElement, StateDataType *array, int *size, const StateDataType searchVal ) { - // + // iterate through array + // use the findStateByName to search + // remove the element by copying the next element into last slot until end + // reduce the size by one + // return true + // otherwise, return false return NULL; } @@ -211,8 +354,11 @@ Dependencies: as needed */ void runBubbleSortByName( StateDataType *array, int size ) { + // start iterator at size - 2? + // iterate through iterator + // check the current index vs the next one. + // if current > next, swap. // no return, void - // bubble sort the data by name of state } /* @@ -259,6 +405,13 @@ Dependencies: none */ void setEmptyElementData( StateDataType *element ) { + // set the element's name to "" + strcpy(element->name, ""); + // set the element's population and growth to 0 + element->population = 0; + element->growth = 0.0; + // set the element's majority to 'x' + element->majority = 'x'; // no return, void } @@ -275,6 +428,12 @@ Dependencies: as needed */ void showTitle() { + // use console io utilities to display the string + printString("Data Management with N-Squared Sorting"); + // use console io utilities to display 38 '=' + printCharacters(38, '='); + // print an endline + printEndline(); // no return, void } @@ -317,6 +476,55 @@ Dependencies: as needed int uploadStateData( StateDataType *StateDataArray, int capacity, const char *fileName ) { - // - return 0; + // setup a counter and other variables + int stateCount = 0; + StateDataType *stateUpload; + bool fileOpenCheck; + char *currentString; + int populationUpload; + double growthUpload; + char majorityUpload; + // initialize the stateUpload with empty data + setEmptyElementData(stateUpload); + // open file + fileOpenCheck = openInputFile(fileName); + if(fileOpenCheck) + { + // read in the four strings/starting line (use readStringToLineEndFromFile) + readStringToLineEndFromFile(currentString); + while(!checkForEndOfInputFile()) + { + // repeat these steps: (use readStringToDelimiterFromFile) + // read in the state name, store + readStringToDelimiterFromFile(COMMA, currentString); + strcpy(stateUpload->name, currentString); + // read in the population, store (cast from string to int) + readStringToDelimiterFromFile(COMMA, currentString); + // find a way to cast string to int, sscanf might work best. + sscanf(currentString, "%d", &populationUpload); + stateUpload->population = populationUpload; + // read in the growth, store (cast from string to double) + readStringToDelimiterFromFile(COMMA, currentString); + // find a way to cast string to double, using sscanf. + sscanf(currentString, "%lf", &growthUpload); + stateUpload->growth = growthUpload; + // read in the majority, store + readStringToDelimiterFromFile(COMMA, currentString); + // get the first character of currentString + majorityUpload = currentString[0]; + stateUpload->majority = majorityUpload; + // use the addElementFromData function to add the data to array + addElementFromData(StateDataArray, &stateCount, capacity, stateUpload->name, stateUpload->population, stateUpload->growth, stateUpload->majority); + // increment counter + stateCount = stateCount + 1; + // do until end of the file is reached. + } + } + else + { + // error out + printf("\nThe file you referenced either does not exist, or inadequate file permissions to access.\n"); + } + // return counter + return stateCount; } \ No newline at end of file diff --git a/PA02-0913/build/Debug/outDebug b/PA02-0913/build/Debug/outDebug index ec674b2..5dd1725 100755 Binary files a/PA02-0913/build/Debug/outDebug and b/PA02-0913/build/Debug/outDebug differ