CMPT125 Lab exam solution

$29.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (7 votes)

Question 1 [6 points]
Write the function that gets a string and returns the most frequent char in the string.
If there is more than one most frequent chars, the function returns the one that appears
first in the array
If str is the empty string, the function returns ‘\0’. For example:
– most_frequent_char(“ABCDAA”) returns ‘A’ – ‘A’ appears three times
– most_frequent_char(“EAACDEEDE”) returns ‘E’ – ‘E’ appears four times
– most_frequent_char(“ABCDCDB”) returns ‘B’. – several chars appear twice,
and ‘B’ is the first among them.
// the function gets a string and returns the most frequent char in it.
// if there are multiple chars, returns the one that appears first
// if str is the empty string, the function returns ‘\0’
char most_frequent_char(const char* str);
Question 2 [7 points]
Write a function that gets an array of length n and a function foo, and counts the
number of indices i such that foo(i)==ar[i]. For example:
– count_foo([0,3,-4,0,9,7], n=6, plus_two) returns 2, because plus_two(1)=3, and
plus_two(5)=7.
– count_foo([0,1,4,9,16,25,36], n=7, square) returns 7, because square(0)=0,
square(1)=1, square(2)=4,…square(6)=36.
// the function gets an array of length n and a function foo
// it returns the number of indices i such that foo(i)==ar[i]
int count_foo(int* ar, int n, int(*foo)(int));
Question 3 [7 points]
Write a function that gets a linked list and a node. If node is in the list, the function
deletes the node from the linked list, releases the memory of the node and returns 1.
If node is not in the list, the function does not change the list and returns 0.
See lib/LL.c and lib/LL.h for details.
// gets a linked list and a node
// it deletes the node from the linked list, frees the node and returns 1
// otherwise, the function does not change the list and returns 0
int LL_delete_node(LL_t* list, node_t* node);