Refined all the Functions and stuff

+- PA03_Main
+- StateUtilities_LogN.c
This commit is contained in:
Charles Descamps 2023-09-19 22:19:01 -07:00
parent bbf588ee8f
commit 82bf795c15
2 changed files with 22 additions and 13 deletions

Binary file not shown.

View file

@ -83,16 +83,25 @@ Dependencies: as needed, including malloc, free, keyword sizeof
*/
void checkForResize( StateArrayType *stateArray )
{
StateArrayType *biggerStateArray;
StateDataType *biggerStateArray;
int resizeIterator;
// do a similiar check to the one that was done in PA02 for size < capacity
if(stateArray->size + 1 > stateArray->capacity)
if(stateArray->size == 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);
// 'create' bigger array
stateArray->capacity = stateArray->capacity * 2;
// allocate the bigger array
biggerStateArray = (StateDataType *)malloc(sizeof(StateDataType) * stateArray->capacity);
// copy the new array stuff into the old space, use loop
for(resizeIterator = 0; resizeIterator < stateArray->size; resizeIterator++)
{
// use deep copy to copy the pieces of smaller array
deepCopy(&biggerStateArray[resizeIterator], stateArray->array[resizeIterator]);
}
// free the old array
free(stateArray->array);
// point the stateArray to the old array
stateArray->array = biggerStateArray;
}
// no return, void
}
@ -112,7 +121,7 @@ Dependencies: as needed, including free
StateArrayType *clearStateArrayType( StateArrayType *stateArray )
{
// free the array from memory
free(&stateArray->array);
free(stateArray->array);
// free the struct from memory
free(stateArray);
// return NULL once complete
@ -134,14 +143,14 @@ void copyArrayType( StateArrayType *dest, const StateArrayType src )
int currentSize;
// use a for loop to iterate through source array
// (start at zero, go to srcSize)
dest->size = src.size;
dest->capacity = src.capacity;
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
}
@ -576,12 +585,11 @@ bool uploadStateData( StateArrayType *arrayType, const char *fileName )
// read in the four strings/starting line
// (use readStringToLineEndFromFile)
readStringToLineEndFromFile(currentString);
readStringToDelimiterFromFile(COMMA, stateUpload.name);
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)
stateUpload.population = readIntegerFromFile();
readCharacterFromFile();
@ -593,6 +601,7 @@ bool uploadStateData( StateArrayType *arrayType, const char *fileName )
// use the addElementFromElement function to add the data to array
addElementFromElement(arrayType, stateUpload);
// do until end of the file is reached.
readStringToDelimiterFromFile(COMMA, stateUpload.name);
}
// return true when completely finished, close file before completion
closeInputFile();