Complete PA02

+- PA02_Main
+- PA02_Main.c
+- StateUtilities.c
++ outRelease (precompiled for UNIX systems)
This commit is contained in:
Charles Descamps 2023-09-12 23:06:01 -07:00
parent 2cc7eca6dd
commit 1169a2607e
4 changed files with 61 additions and 33 deletions

View file

@ -36,7 +36,7 @@ bool addElementFromData( StateDataType *stateArr, int *size, int capacity,
double stateGrowth, char politicalMajority )
{
// check to see if the size will go over the capacity
if(*size <= capacity)
if(*size < capacity)
{
// put in the data to the array
// array[*size] = parameter
@ -112,7 +112,7 @@ void copyArray( StateDataType *dest, const StateDataType *src, int srcSize )
{
// copy each element from the array to the new array.
// uses deepCopy to do this.
deepCopy(dest, *src);
deepCopy(&dest[currentSize], src[currentSize]);
}
// no return, void
}
@ -235,7 +235,7 @@ void displayData( const StateDataType *array, int size, const char *subTitle )
printCharacter(PIPE);
// print new line
printEndline();
for(countStates = 0; countStates < size - 1; countStates++)
for(countStates = 0; countStates < size; countStates++)
{
// use the constants to format the table
// print PIPE character
@ -362,19 +362,16 @@ Dependencies: as needed
bool removeStateByName( StateDataType *removedElement,
StateDataType *array, int *size, const StateDataType searchVal )
{
bool stateNameFound;
int removeCount;
bool stateNameFoundFlag = false;
// use the findStateByName to search
stateNameFound = findStateByName(removedElement, array, *size, searchVal);
// iterate through array
if(stateNameFound)
if(findStateByName(removedElement, array, *size, searchVal))
{
//
for(removeCount = 0; removeCount < *size; removeCount++)
{
if(strcmp(array[removeCount].name, removedElement->name) == 0 ||
stateNameFoundFlag == 1)
stateNameFoundFlag)
{
// set the flag to signify found value
stateNameFoundFlag = true;
@ -406,29 +403,19 @@ void runBubbleSortByName( StateDataType *array, int size )
{
int bubbleCount;
int arrayCount;
printf("\nArray @ 0: %s\n", array[0].name);
printf("\nArray @ 1: %s\n", array[1].name);
printf("\nArray @ 2: %s\n", array[2].name);
printf("\nArray @ 3: %s\n", array[3].name);
printf("\nArray @ 4: %s\n", array[4].name);
printf("\nArray @ size: %s\n", array[size].name);
// use two for loops to iterate through array
// for(bubbleCount = 0; bubbleCount < size - 1; bubbleCount++)
// {
// for(arrayCount = bubbleCount + 1; arrayCount < size; arrayCount++)
// {
// printf("\nValue of bubbleCount: %d\n", bubbleCount);
// printf("\nValue of arrayCount: %d\n", arrayCount);
// //printf("\nValue of array[arrayCount].name: %s", array[0].name);
// //printf("\nValue of array[arrayCount + 1].name: %s\n", array[1].name);
// // check the current index vs the next one.
// if(strcmp(array[arrayCount].name, array[arrayCount + 1].name) > 0)
// {
// // if current > next, swap. AKA: strcmp(current, next) > 0
// swapElements(array, arrayCount, arrayCount + 1);
// }
// }
// }
for(bubbleCount = 0; bubbleCount < size - 1; bubbleCount++)
{
for(arrayCount = 0; arrayCount < size - bubbleCount - 1; arrayCount++)
{
// check the current index vs the next one.
if(strcmp(array[arrayCount].name, array[arrayCount + 1].name) > 0)
{
// if current > next, swap. AKA: strcmp(current, next) > 0
swapElements(array, arrayCount, arrayCount + 1);
}
}
}
// no return, void
}
@ -444,6 +431,28 @@ Dependencies: as needed, swap may not be used
*/
void runInsertionSortByPopulation( StateDataType *array, int size )
{
// define variables
int insertCount, insertIterator;
StateDataType insertStoreState;
// use a for loop to move through the array
for(insertCount = 0; insertCount < size; insertCount++)
{
// start the iterator at the same place where insertCount starts
insertIterator = insertCount;
// use a while loop to do the insert sort
while(insertIterator > 0 && (array[insertIterator - 1].population >=
array[insertIterator].population))
{
// store the element
deepCopy(&insertStoreState, array[insertIterator - 1]);
// move the next element to the last location
deepCopy(&array[insertIterator - 1], array[insertIterator]);
// put the element into the array
deepCopy(&array[insertIterator], insertStoreState);
// move the iterator to the previous index
insertIterator = insertIterator - 1;
}
}
// no return, void
}
@ -459,6 +468,25 @@ Dependencies: as needed
*/
void runSelectionSortByGrowth( StateDataType *array, int size )
{
int selectCount, selectIterator, smallestGrowth;
// use for loop to run through the whole array
for(selectCount = 0; selectCount < size; selectCount++)
{
// start at the not sorted part of the array
smallestGrowth = selectCount;
// use another for loop to find what the smallest growth is
for(selectIterator = selectCount + 1; selectIterator < size; selectIterator++)
{
// select the smallest growth
if(array[selectIterator].growth < array[smallestGrowth].growth)
{
// find the smallest growth of the array.
smallestGrowth = selectIterator;
}
}
// swap the elements of the array
swapElements(array, selectCount, smallestGrowth);
}
// no return, void
}