Getting further, but still not working

+- PA03_Main
+- StateUtilities_LogN.c
This commit is contained in:
Charles Descamps 2023-09-17 13:50:23 -07:00
parent cabae23450
commit bbf588ee8f
2 changed files with 200 additions and 41 deletions

Binary file not shown.

View file

@ -9,7 +9,7 @@ const int ITEM_BLOCK_SIZE = 6;
const int MAJORITY_BLOCK_SIZE = 10; const int MAJORITY_BLOCK_SIZE = 10;
const char EQUALS = '='; const char EQUALS = '=';
const char PLUS = '+'; const char PLUS = '+';
const int TITLE_LENGTH = 38; const int TITLE_LENGTH = 34;
// all functions // all functions
@ -32,24 +32,17 @@ void addElementFromData( StateArrayType *stateArr,
const char *stateName, int statePop, const char *stateName, int statePop,
double stateGrowth, char politicalMajority ) 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 // check to see if the size will go over the capacity
if(stateArr.size < stateArr.capacity) checkForResize(stateArr);
{ // put in the data to the array
printf("\nInside IF\n"); // stateArr->array[stateArr.size] = parameter
// put in the data to the array stateArr->array[stateArr->size].population = statePop;
// stateArr->array[stateArr.size] = parameter stateArr->array[stateArr->size].growth = stateGrowth;
stateArr->array[stateArr.size].population = statePop; stateArr->array[stateArr->size].majority = politicalMajority;
stateArr->array[stateArr.size].growth = stateGrowth; // use strcpy(stateArr->array[stateArr.size].name, stateName)
stateArr->array[stateArr.size].majority = politicalMajority; strcpy(stateArr->array[stateArr->size].name, stateName);
// use strcpy(stateArr->array[stateArr.size].name, stateName) // increment size (stateArr.size = stateArr.size + 1)
strcpy(stateArr->array[stateArr.size].name, stateName); stateArr->size = stateArr->size + 1;
// increment size (stateArr.size = stateArr.size + 1)
stateArr->size = stateArr->size + 1;
printf("\nDONE IF\n");
}
// no return, void // no return, void
} }
@ -68,10 +61,8 @@ Dependencies: addElementFromData - function uses one code statement
void addElementFromElement( StateArrayType *stateArr, void addElementFromElement( StateArrayType *stateArr,
const StateDataType state ) const StateDataType state )
{ {
printf("\nAdding Element\n");
addElementFromData(stateArr, state.name, state.population, addElementFromData(stateArr, state.name, state.population,
state.growth, state.majority); state.growth, state.majority);
printf("\nAdded Element\n");
// no return, void // no return, void
} }
@ -92,6 +83,17 @@ Dependencies: as needed, including malloc, free, keyword sizeof
*/ */
void checkForResize( StateArrayType *stateArray ) 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 // no return, void
} }
@ -109,13 +111,9 @@ Dependencies: as needed, including free
*/ */
StateArrayType *clearStateArrayType( StateArrayType *stateArray ) StateArrayType *clearStateArrayType( StateArrayType *stateArray )
{ {
int stateIterator; // free the array from memory
for(stateIterator = 0; stateIterator < stateArray->size; stateIterator++) free(&stateArray->array);
{ // free the struct from memory
// free the states from memory
free(&stateArray->array[stateIterator]);
}
// free the array in the stateArray
free(stateArray); free(stateArray);
// return NULL once complete // return NULL once complete
return NULL; return NULL;
@ -133,6 +131,17 @@ Dependencies: as needed
*/ */
void copyArrayType( StateArrayType *dest, const StateArrayType src ) 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 // no return, void
} }
@ -150,8 +159,15 @@ Dependencies: as needed, including function malloc and keyword sizeof
*/ */
StateArrayType *createStateArrayType( int initialCapacity ) StateArrayType *createStateArrayType( int initialCapacity )
{ {
// return NULL (temporarily) StateArrayType *stateArrayCreation;
return NULL; // 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 ) 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 // no return, void
} }
@ -186,7 +209,136 @@ Dependencies: as needed
*/ */
void displayData( const StateArrayType arrayType, const char *subTitle ) 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 ) void reverseArray( StateArrayType *stateArray )
{ {
// reverse in place? could use swap elements to make this happen.
// use a for loop?
// no return, void // no return, void
} }
@ -335,7 +489,7 @@ Dependencies: none
void setEmptyElementData( StateDataType *element ) void setEmptyElementData( StateDataType *element )
{ {
// set the element's name to "" // set the element's name to ""
strcpy(element->name, ""); element->name[0] = NULL_CHAR;
// set the element's population and growth to 0 // set the element's population and growth to 0
element->population = 0; element->population = 0;
element->growth = 0.0; element->growth = 0.0;
@ -361,7 +515,7 @@ void showTitle()
printString("Data Management with Log N Sorting"); printString("Data Management with Log N Sorting");
// print endline // print endline
printEndline(); printEndline();
// use console io utilities to display 38 '=' // use console io utilities to display 34 '='
printCharacters(TITLE_LENGTH, EQUALS); printCharacters(TITLE_LENGTH, EQUALS);
// print an endline // print an endline
printEndline(); printEndline();
@ -381,6 +535,15 @@ Dependencies: as needed
*/ */
void swapElements( StateDataType *array, int leftIndex, int rightIndex ) 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 // no return, void
} }
@ -400,18 +563,15 @@ Dependencies: as needed
*/ */
bool uploadStateData( StateArrayType *arrayType, const char *fileName ) bool uploadStateData( StateArrayType *arrayType, const char *fileName )
{ {
// setup a counter and other variables // setup a temporary state and other variables
int stateNum = 0;
StateDataType stateUpload; StateDataType stateUpload;
bool fileOpenCheck;
char currentString[STD_STR_LEN]; char currentString[STD_STR_LEN];
// int populationUpload; // int populationUpload;
// double growthUpload; // double growthUpload;
// char majorityUpload; // char majorityUpload;
// open file // open file
setEmptyElementData(&stateUpload); //setEmptyElementData(&stateUpload);
fileOpenCheck = openInputFile(fileName); if(openInputFile(fileName))
if(fileOpenCheck)
{ {
// read in the four strings/starting line // read in the four strings/starting line
// (use readStringToLineEndFromFile) // (use readStringToLineEndFromFile)
@ -431,12 +591,11 @@ bool uploadStateData( StateArrayType *arrayType, const char *fileName )
readCharacterFromFile(); readCharacterFromFile();
stateUpload.majority = readCharacterFromFile(); stateUpload.majority = readCharacterFromFile();
// use the addElementFromElement function to add the data to array // use the addElementFromElement function to add the data to array
printf("\nElement: %d uploaded\n", stateNum);
stateNum = stateNum + 1;
addElementFromElement(arrayType, stateUpload); addElementFromElement(arrayType, stateUpload);
// do until end of the file is reached. // 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 true;
} }
// return false // return false