Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Foundations of Algorithms (COMP10002_2021_SM1)

Question 1

4 pts


Write a function

double average_word_length(word_t words[], int n)

that takes an array words of n (n > 0) English word records as the input and          returns the average length of the English word strings (the word_str component of the records) in the array.

For example, given n = 3 and:

words[0] = {"allocate", "v", {"allocated", "allocated", "allocating", NULL}}; words[1] = {"day", "n", {NULL, NULL, NULL, "days"}};

words[2] = {"sell", "v n", {"sold", "sold", "selling", "sells"}};

The function should return 5.0 (5.0 = (8+3+4)/3).

If you make use of library functions, you must add suitable #include lines at the start of your answer.


Instructions on how to enter code in the answer box (also applies to the other coding questions):

The following instructions are intended to make entering code easier. Code formatting and indentation will not be marked.

Click the "Paragraph" button below and choose the "Preformatted" option. Then, continue with any of the following options:

Option A: Type your answer in the answer box directly, and use whitespaces

for indentation.

Option B: Write code in Grok/jEdit/any other code editor. Paste your answer

back to the answer box when you are done.

The indentation may get lost. No need to worry about it. If you want to fix

the indentation, select all code that you just pasted in the answer box, click the "Paragraph" button and choose "Preformatted" again. If this does not fix it, ignore the indentation and move on to the other questions.

Option C [This option is highly discouraged if you have not tried it out with

our sample quiz. Uploading a photo may cause delays. This will not serve as grounds for special consideration]: Write code on paper, take a photo of your answer, and upload the photo by:

Click the "More options" button below (the rightmost button, with three

vertical dots).

In the expanded menu, click the Images icon.

Choose the "Upload Image" option to upload your answer photo. If the

upload is successful, you should be able to see the uploaded photo in the answer box.

Edit    View    Insert    Format    Tools    Table

12pt         Paragraph


Question 2

6 pts


Write a function

double average_num_pos(word_t words[], int n)

that takes an array words of n (n > 0) English word records as the input and returns the average number of POS tags per word for the words in the array.

For example, given n = 4 and:

words[0] = {"allocate", "v", {"allocated", "allocated", "allocating", NULL}}; words[1] = {"day", "n", {NULL, NULL, NULL, "days"}};

words[2] = {"sell", "v n", {"sold", "sold", "selling", "sells"}};

words[3] = {"softly", "adv", {NULL, NULL, NULL, NULL}};

The function should return 1.25 (1.25 = 1+1+2+1/4).

If you make use of library functions, you must add suitable #include lines at the start of your answer.

Edit    View Insert Format Tools Table

12pt Paragraph


Question 3

7 pts


Given a word string word_str, its Form 2 variation is generated following the rules below:

If word_str ends with an 'e', we remove the ending 'e' and append "ing" to the

end of the word. For example, Form 2 of "allocate" is "allocating" .

Otherwise, we just append "ing" to the end of the word. For example, Form 2

of "sell" is "selling".

Write a function

char *generate_form_2(char word_str[], int form)

that takes a string word_str as the input, generates its Form 2 variation following the rules above, and returns a pointer to the variation form generated.

For example, generate_form_2("allocate") should return a pointer to a new string that stores "allocating"; generate_form_2("sell") should return a pointer to a       new string that stores "selling".