. Is this correct? Write a C program wordsort.c that prompts the user to enter a
ID: 3819099 • Letter: #
Question
. Is this correct?
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 <stdio.h>
/**
* 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;
}
My answer:
#include <stdio.h>
#include <string.h>
//getMax function
int getMax (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;
}
typedef struct
{
char label[20];
}WordT;
void sort(WordT *words, int numOfWords)
{
for(int i = 0; i < numOfWords; i++)
for(int j = 0; j < numOfWords-i-1; j++)
if(strcmp((words+j)->label, (words+j+1)->label) > 0)
{
WordT temp;
temp = *(words + j);
*(words + j) = *(words + j + 1);
*(words + j + 1) = temp;
}
}
int main()
{
WordT words[10];
int n = 0;
printf("Enter the words ("0" to stop): ");
scanf("%s", words[n].label);
while(strcmp(words[n].label, "0") != 0)
{
n++;
scanf("%s", words[n].label);
}
printf("Before sorting the words are: ");
for(int i = 0; i < n; i++)
printf("%s ", words[i].label);
sort(words, n);
printf("After sorting the words are: ");
for(int i = 0; i < n; i++)
printf("%s ", words[i].label);
}
Explanation / Answer
Your answer is Not correct as it doesn’t adapt the above given getMaxLocation function and sorting function though your program sorts the given words.
Your mistakes
1. You have just copied the getMaxLocation as it is and renaming as getMax.
2. You have used your own sort function and didn't adapt the given sort instructions.
Actually in the given program(not your program) the sort instructions are given in the main program itself i,e.,
while (amount > 1) {
maxLocation = getMaxLocation(numbers, amount);
temp = numbers[amount - 1];
numbers[amount - 1] = numbers[maxLocation];
numbers[maxLocation] = temp;
amount--;
}
This instructions sorts the array of numbers. and they included it in main (but not taken as a separate function).
The changes you need to do is
1. Change the Sorting function that you wrote on your own and write accordingly to the above mentioned sorting instructions.
// Working of given sorting instructions (if you didn't understand)
// getting the index of the maximum word using getMaxLocation function from the word array of length n
//swapping that word with the last word of the array and deecrementing the length of array value by 1.
// repeat until the array size becomes 1 or 0.
2. Change the getMax function according to your requirements and use it in sort function i,e.,
The given getMaxLocation function has
input(arguments) - integer array and length of array.
output (or return value) - index of the largest number present in that array.
Your getMaxLocation function needs
input (or arguments) - Word array and length of array.
output (or return value) - index of the largest word present in that array
Change accordingly to this
Hope my answer helps you.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.