Question: Write a C program wordsort.c that prompts the user to enter a list of words (you can use scanf with %s to read words) and

Write a C program wordsort.c that prompts the user to enter a list of words (you can use scanf with "%s" to read words) and stores them in an array "words" until the user enters 0 (not to be included in the array). Your list of words will be an array of the WordT struct. The program then sorts the words in the array alphabetically and prints the sorted list to the screen. You must use the previous sort() and getMax() functions from project 3 and adapt them to handle strings. You may use strcmp() to compare two strings alphabetically (to replace the < and > check in the original sorting algorithm). The prototype of the sort function must look like: void sort(WordT *words, int numWords); You must use the following type definition to store your word: typedef struct { char label[20]; } WordT;

This is where getMax comes from

#include

/** * Finds the largest value in a list. * list - an array of numbers * n - the size of the array * * Returns the index where the largest value is stored * or -1 if there are no array elements. */ int getMaxLocation(int *list, int n) { int i, maxValueIndex, maxValue;

if (n == 0) return -1;

maxValueIndex = 0; maxValue = list[0]; for ( i=1; i < n; i++ ) if ( maxValue < list[i] ) { maxValue = list[i]; maxValueIndex = i; }

return maxValueIndex; }

int main() { int n;

printf("Enter the number of elements you want to sort: "); scanf("%d", &n);

int numbers[n]; int i;

for (i=0; i < n; i++) { printf("Enter a value: "); scanf("%d", &numbers[i]); }

int maxLocation, amount = n; int temp; while (amount > 1) { maxLocation = getMaxLocation(numbers, amount); temp = numbers[amount - 1]; numbers[amount - 1] = numbers[maxLocation]; numbers[maxLocation] = temp; amount--; }

for (i=0; i < n; i++) printf("%d ", numbers[i]); printf(" ");

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!