Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

getMax function: int getMax ( int *list, int n ) { int i, maxValueIndex, maxValu

ID: 3777872 • Letter: G

Question

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;
}

Write a C program sort.c that prompts the user to enter a list of words and stores them in an array until the user enters "0" (not to be included in the array. 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 discussed in class to compare two strings alphabetically. The prototype of the sort function must look like: void sort wordT words, int numwords You must use the following type definition: typedef struct char label 201 h WordT

Explanation / Answer

#include <iostream>

                #include <cstring>

               

                using namespace std;

                const int MAXIMUM_LENGTH = 100;

               

                int main()

                {

                                char first_string[MAXIMUM_LENGTH];

                                char second_string[MAXIMUM_LENGTH];

               

                                cout << "Enter first string: ";

                                cin.getline(first_string,MAXIMUM_LENGTH);

                                cout << "Enter second string: ";

                                cin.getline(second_string,MAXIMUM_LENGTH);

               

                                cout << "Before copying the strings were ";

                                if (strcmp(first_string,second_string))

                                                cout << "not ";

                                cout << "the same. ";

               

                                strcpy(first_string,second_string);

               

                                cout << "After copying the strings were ";

                                if (strcmp(first_string,second_string))

                                                cout << "not ";

                                cout << "the same. ";

               

                                strcat(first_string,second_string);

               

                                cout << "After concatenating, the first string is: ";

                                cout << first_string;

               

                                return 0;

                }