Refined all the Functions and stuff
+- PA03_Main +- StateUtilities_LogN.c
This commit is contained in:
parent
bbf588ee8f
commit
82bf795c15
2 changed files with 22 additions and 13 deletions
Binary file not shown.
|
|
@ -83,16 +83,25 @@ Dependencies: as needed, including malloc, free, keyword sizeof
|
||||||
*/
|
*/
|
||||||
void checkForResize( StateArrayType *stateArray )
|
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
|
// 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)
|
// 'create' bigger array
|
||||||
biggerStateArray = createStateArrayType(stateArray->capacity * 2);
|
stateArray->capacity = stateArray->capacity * 2;
|
||||||
// copy the old array stuff into the new array, use copyArrayType
|
// allocate the bigger array
|
||||||
copyArrayType(biggerStateArray, *stateArray);
|
biggerStateArray = (StateDataType *)malloc(sizeof(StateDataType) * stateArray->capacity);
|
||||||
// copy the new array stuff into the old space
|
// copy the new array stuff into the old space, use loop
|
||||||
copyArrayType(stateArray, *biggerStateArray);
|
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
|
// no return, void
|
||||||
}
|
}
|
||||||
|
|
@ -112,7 +121,7 @@ Dependencies: as needed, including free
|
||||||
StateArrayType *clearStateArrayType( StateArrayType *stateArray )
|
StateArrayType *clearStateArrayType( StateArrayType *stateArray )
|
||||||
{
|
{
|
||||||
// free the array from memory
|
// free the array from memory
|
||||||
free(&stateArray->array);
|
free(stateArray->array);
|
||||||
// free the struct from memory
|
// free the struct from memory
|
||||||
free(stateArray);
|
free(stateArray);
|
||||||
// return NULL once complete
|
// return NULL once complete
|
||||||
|
|
@ -134,14 +143,14 @@ void copyArrayType( StateArrayType *dest, const StateArrayType src )
|
||||||
int currentSize;
|
int currentSize;
|
||||||
// use a for loop to iterate through source array
|
// use a for loop to iterate through source array
|
||||||
// (start at zero, go to srcSize)
|
// (start at zero, go to srcSize)
|
||||||
|
dest->size = src.size;
|
||||||
|
dest->capacity = src.capacity;
|
||||||
for(currentSize = 0; currentSize < src.size; currentSize++)
|
for(currentSize = 0; currentSize < src.size; currentSize++)
|
||||||
{
|
{
|
||||||
// copy each element from the array to the new array.
|
// copy each element from the array to the new array.
|
||||||
// uses deepCopy to do this.
|
// uses deepCopy to do this.
|
||||||
deepCopy(&dest->array[currentSize], src.array[currentSize]);
|
deepCopy(&dest->array[currentSize], src.array[currentSize]);
|
||||||
}
|
}
|
||||||
dest->size = src.size;
|
|
||||||
dest->capacity = src.capacity;
|
|
||||||
// no return, void
|
// no return, void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -576,12 +585,11 @@ bool uploadStateData( StateArrayType *arrayType, const char *fileName )
|
||||||
// read in the four strings/starting line
|
// read in the four strings/starting line
|
||||||
// (use readStringToLineEndFromFile)
|
// (use readStringToLineEndFromFile)
|
||||||
readStringToLineEndFromFile(currentString);
|
readStringToLineEndFromFile(currentString);
|
||||||
|
readStringToDelimiterFromFile(COMMA, stateUpload.name);
|
||||||
while(!checkForEndOfInputFile())
|
while(!checkForEndOfInputFile())
|
||||||
{
|
{
|
||||||
// repeat these steps: (use readStringToDelimiterFromFile)
|
// repeat these steps: (use readStringToDelimiterFromFile)
|
||||||
// read in the state name, store
|
// read in the state name, store
|
||||||
readStringToDelimiterFromFile(COMMA, currentString);
|
|
||||||
strcpy(stateUpload.name, currentString);
|
|
||||||
// read in the population, store (cast from string to int)
|
// read in the population, store (cast from string to int)
|
||||||
stateUpload.population = readIntegerFromFile();
|
stateUpload.population = readIntegerFromFile();
|
||||||
readCharacterFromFile();
|
readCharacterFromFile();
|
||||||
|
|
@ -593,6 +601,7 @@ bool uploadStateData( StateArrayType *arrayType, const char *fileName )
|
||||||
// use the addElementFromElement function to add the data to array
|
// use the addElementFromElement function to add the data to array
|
||||||
addElementFromElement(arrayType, stateUpload);
|
addElementFromElement(arrayType, stateUpload);
|
||||||
// do until end of the file is reached.
|
// do until end of the file is reached.
|
||||||
|
readStringToDelimiterFromFile(COMMA, stateUpload.name);
|
||||||
}
|
}
|
||||||
// return true when completely finished, close file before completion
|
// return true when completely finished, close file before completion
|
||||||
closeInputFile();
|
closeInputFile();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue