diff --git a/PA03-0920/PA03_Main b/PA03-0920/PA03_Main index 23a3a06..1c90e4f 100755 Binary files a/PA03-0920/PA03_Main and b/PA03-0920/PA03_Main differ diff --git a/PA03-0920/StateUtilities_LogN.c b/PA03-0920/StateUtilities_LogN.c index be94d63..4b9977d 100644 --- a/PA03-0920/StateUtilities_LogN.c +++ b/PA03-0920/StateUtilities_LogN.c @@ -9,7 +9,7 @@ const int ITEM_BLOCK_SIZE = 6; const int MAJORITY_BLOCK_SIZE = 10; const char EQUALS = '='; const char PLUS = '+'; -const int TITLE_LENGTH = 38; +const int TITLE_LENGTH = 34; // all functions @@ -32,24 +32,17 @@ void addElementFromData( StateArrayType *stateArr, const char *stateName, int statePop, double stateGrowth, char politicalMajority ) { - printf("\nAdding Element(from data)\n"); - printf("\nSize is: %d\n", stateArr.size); - printf("\nCapacity is: %d\n", stateArr.capacity); // check to see if the size will go over the capacity - if(stateArr.size < stateArr.capacity) - { - printf("\nInside IF\n"); - // put in the data to the array - // stateArr->array[stateArr.size] = parameter - stateArr->array[stateArr.size].population = statePop; - stateArr->array[stateArr.size].growth = stateGrowth; - stateArr->array[stateArr.size].majority = politicalMajority; - // use strcpy(stateArr->array[stateArr.size].name, stateName) - strcpy(stateArr->array[stateArr.size].name, stateName); - // increment size (stateArr.size = stateArr.size + 1) - stateArr->size = stateArr->size + 1; - printf("\nDONE IF\n"); - } + checkForResize(stateArr); + // put in the data to the array + // stateArr->array[stateArr.size] = parameter + stateArr->array[stateArr->size].population = statePop; + stateArr->array[stateArr->size].growth = stateGrowth; + stateArr->array[stateArr->size].majority = politicalMajority; + // use strcpy(stateArr->array[stateArr.size].name, stateName) + strcpy(stateArr->array[stateArr->size].name, stateName); + // increment size (stateArr.size = stateArr.size + 1) + stateArr->size = stateArr->size + 1; // no return, void } @@ -68,10 +61,8 @@ Dependencies: addElementFromData - function uses one code statement void addElementFromElement( StateArrayType *stateArr, const StateDataType state ) { - printf("\nAdding Element\n"); addElementFromData(stateArr, state.name, state.population, state.growth, state.majority); - printf("\nAdded Element\n"); // no return, void } @@ -92,6 +83,17 @@ Dependencies: as needed, including malloc, free, keyword sizeof */ void checkForResize( StateArrayType *stateArray ) { + StateArrayType *biggerStateArray; + // do a similiar check to the one that was done in PA02 for size < capacity + if(stateArray->size + 1 > stateArray->capacity) + { + // create bigger array, use createStateArrayType(2x old capacity) + biggerStateArray = createStateArrayType(stateArray->capacity * 2); + // copy the old array stuff into the new array, use copyArrayType + copyArrayType(biggerStateArray, *stateArray); + // copy the new array stuff into the old space + copyArrayType(stateArray, *biggerStateArray); + } // no return, void } @@ -109,13 +111,9 @@ Dependencies: as needed, including free */ StateArrayType *clearStateArrayType( StateArrayType *stateArray ) { - int stateIterator; - for(stateIterator = 0; stateIterator < stateArray->size; stateIterator++) - { - // free the states from memory - free(&stateArray->array[stateIterator]); - } - // free the array in the stateArray + // free the array from memory + free(&stateArray->array); + // free the struct from memory free(stateArray); // return NULL once complete return NULL; @@ -133,6 +131,17 @@ Dependencies: as needed */ void copyArrayType( StateArrayType *dest, const StateArrayType src ) { + int currentSize; + // use a for loop to iterate through source array + // (start at zero, go to srcSize) + for(currentSize = 0; currentSize < src.size; currentSize++) + { + // copy each element from the array to the new array. + // uses deepCopy to do this. + deepCopy(&dest->array[currentSize], src.array[currentSize]); + } + dest->size = src.size; + dest->capacity = src.capacity; // no return, void } @@ -150,8 +159,15 @@ Dependencies: as needed, including function malloc and keyword sizeof */ StateArrayType *createStateArrayType( int initialCapacity ) { - // return NULL (temporarily) - return NULL; + StateArrayType *stateArrayCreation; + // initialize array type + stateArrayCreation = malloc(sizeof(StateArrayType)); + stateArrayCreation->size = 0; + stateArrayCreation->capacity = initialCapacity; + // initialize array + stateArrayCreation->array = malloc(sizeof(StateDataType) * initialCapacity); + // return stateArrayCreation + return stateArrayCreation; } /* @@ -167,6 +183,13 @@ 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 } @@ -186,7 +209,136 @@ Dependencies: as needed */ void displayData( const StateArrayType arrayType, const char *subTitle ) { - // no return, void + int countStates; + // iterate through array to size, check if zero. + if(arrayType.size != 0) + { + printEndline(); + printString("Display Data - "); + printString(subTitle); + printEndline(); + printEndline(); + // 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 < arrayType.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(arrayType.array[countStates].name, + NAME_BLOCK_SIZE, "LEFT"); + // print PIPE character + printCharacter(PIPE); + // print population justified right + printIntegerJustified(arrayType.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(arrayType.array[countStates].growth * 100.00, + 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(SPACE); + printCharacter(arrayType.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); + // print endline + printEndline(); + } + } + else + { + //print out "Data not found - Display aborted". + printString("Data not found - Display aborted"); + printEndline(); + } + // no return, void } /* @@ -201,6 +353,8 @@ Dependencies: as needed */ void reverseArray( StateArrayType *stateArray ) { + // reverse in place? could use swap elements to make this happen. + // use a for loop? // no return, void } @@ -335,7 +489,7 @@ Dependencies: none void setEmptyElementData( StateDataType *element ) { // set the element's name to "" - strcpy(element->name, ""); + element->name[0] = NULL_CHAR; // set the element's population and growth to 0 element->population = 0; element->growth = 0.0; @@ -361,7 +515,7 @@ void showTitle() printString("Data Management with Log N Sorting"); // print endline printEndline(); - // use console io utilities to display 38 '=' + // use console io utilities to display 34 '=' printCharacters(TITLE_LENGTH, EQUALS); // print an endline printEndline(); @@ -381,6 +535,15 @@ Dependencies: as needed */ void swapElements( StateDataType *array, int leftIndex, int rightIndex ) { + // create StateDataType temp to store swapped element + StateDataType storeElement; + // use deepCopy to copy out swapped element + deepCopy(&storeElement, array[leftIndex]); + // use deepCopy to move the other element, + deepCopy(&array[leftIndex], array[rightIndex]); + // use deepCopy to copy the temp element to the other element + deepCopy(&array[rightIndex], storeElement); + // updated the array // no return, void } @@ -400,18 +563,15 @@ Dependencies: as needed */ bool uploadStateData( StateArrayType *arrayType, const char *fileName ) { - // setup a counter and other variables - int stateNum = 0; + // setup a temporary state and other variables StateDataType stateUpload; - bool fileOpenCheck; char currentString[STD_STR_LEN]; // int populationUpload; // double growthUpload; // char majorityUpload; // open file - setEmptyElementData(&stateUpload); - fileOpenCheck = openInputFile(fileName); - if(fileOpenCheck) + //setEmptyElementData(&stateUpload); + if(openInputFile(fileName)) { // read in the four strings/starting line // (use readStringToLineEndFromFile) @@ -431,12 +591,11 @@ bool uploadStateData( StateArrayType *arrayType, const char *fileName ) readCharacterFromFile(); stateUpload.majority = readCharacterFromFile(); // use the addElementFromElement function to add the data to array - printf("\nElement: %d uploaded\n", stateNum); - stateNum = stateNum + 1; addElementFromElement(arrayType, stateUpload); // do until end of the file is reached. } - // return true when completely finished + // return true when completely finished, close file before completion + closeInputFile(); return true; } // return false