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

C++ code only. The program functions as follows: given a wordlist, a collection

ID: 3673204 • Letter: C

Question

C++ code only. The program functions as follows: given a wordlist, a collection of letters, and the length of one of the words, find all possible two words "answers" that use exactly the collection of letters given. The default word list is "wordlist.txt" (provided).

Note: the program either uses all the defaults or all three parameters must be specified.

The program functions as follows: given a wordlist, a collection of letters, and the length of one of the words, find all possible two words "answers" that use exactly the collection of letters given The default word list is "wordlist.txt" (provided) and the default collection of letters aadekmmnortww. The default word length is 7 The "wordlist.txt" word list is also provided here in this directory Note: the program either uses all the defaults or all three parameters must be specified Here are some examples of correct outputs > twowords awkward moment > ./twowords wordlist.txt testifythesis 6 feisty theists heists testify shiest testify thesis testify >./twowords wordlist.txt organpizza 5 argon pizza groan pizza orang pizza organ pizza pirog zanza

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Word
{
char* str;
int index;
};

struct DupArray
{
struct Word* array;
int size;
};

struct DupArray* createDupArray(char* str[], int size)
{
struct DupArray* dupArray =
(struct DupArray*) malloc( sizeof(struct DupArray) );
dupArray->size = size;
dupArray->array =
(struct Word*) malloc( dupArray->size * sizeof(struct Word) );

int i;
for (i = 0; i < size; ++i)
{
dupArray->array[i].index = i;
dupArray->array[i].str = (char*) malloc( strlen(str[i]) + 1 );
strcpy(dupArray->array[i].str, str[i] );
}

return dupArray;
}
int compChar(const void* a, const void* b)
{
return *(char*)a - *(char*)b;
}

int compStr(const void* a, const void* b)
{
struct Word* a1 = (struct Word *)a;
struct Word* b1 = (struct Word *)b;
return strcmp(a1->str, b1->str);
}
void printAnagramsTogether(char* wordArr[], int size)
{
// Step 1: Create a copy of all words present in given wordArr.
  
struct DupArray* dupArray = createDupArray(wordArr, size);

// Step 2: Iterate through all words in dupArray and sort
  
int i;
for (i = 0; i < size; ++i)
qsort(dupArray->array[i].str,
strlen(dupArray->array[i].str), sizeof(char), compChar);

// Step 3: Now sort the array of words in dupArray
qsort(dupArray->array, size, sizeof(dupArray->array[0]), compStr);

// Step 4: Now all words in dupArray are together, but these
  
for (i = 0; i < size; ++i)
printf("%s ", wordArr[dupArray->array[i].index]);
}

int main()
{
char* wordArr[] = {"cat", "dog", "god",};
int size = sizeof(wordArr) / sizeof(wordArr[0]);
printAnagramsTogether(wordArr, size);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote