2021/8/1 PQ5: Attempt review
https://moodle-courses2122.wolfware.ncsu.edu/mod/quiz/review.php?attempt=159707&cmid=111792 1/7
WolfWare / Dashboard / My courses / ECE 209 (051) SUM1 2021 / Practice Quizzes / PQ5
Started on Friday, July 30, 2021, 9:01 PM
State Finished
Completed on Sunday, August 1, 2021, 12:08 AM
Time taken 1 day 3 hours
Question 1
Not answered
Points out of 1.00
Define a C function named removeVals that removes all occurrences of a specific set of values from a linked list. The declaration of the
function is:
struct node * removeVals(struct node *list, const int vals[], int n);
The first parameter is a linked list, built with the struct shown below. The length of the list is unknown, and it may be empty. The second
parameter is an array of integers. The third parameter is the number of values in the array. The return value is the new linked list. All copies
of any value that appears in the array must be removed from the list. The return list may be empty.
struct node {
int data;
struct node *next;
};
Do not call free. Do not use any standard C library functions.
https://wolfware.ncsu.edu/
https://moodle-courses2122.wolfware.ncsu.edu/my/
https://moodle-courses2122.wolfware.ncsu.edu/course/view.php?id=992
https://moodle-courses2122.wolfware.ncsu.edu/course/view.php?id=992#section-2
https://moodle-courses2122.wolfware.ncsu.edu/mod/quiz/view.php?id=111792
2021/8/1 PQ5: Attempt review
https://moodle-courses2122.wolfware.ncsu.edu/mod/quiz/review.php?attempt=159707&cmid=111792 2/7
Question 2
Not answered
Points out of 1.00
Suppose the struct candidate type is defined as follows:
struct candidate {
char name[30];
int votes;
};
Write the definition of the C function declared below. It returns true if str matches any part of the candidate’s name, and false otherwise.
For example, if the candidate’s name is “Bob Roberts”, the function will return true for “Bob” or “Rob” or “Robert” or “Bob Roberts” or “o”, but
will return false for “Smith”. Return false if str is an empty string.
bool matchName(const struct candidate *c, const char * str);
You may use any standard library function that appears on the C Reference Sheet.
2021/8/1 PQ5: Attempt review
https://moodle-courses2122.wolfware.ncsu.edu/mod/quiz/review.php?attempt=159707&cmid=111792 3/7
Question 3
Not answered
Points out of 1.00
Define a C function that opens a file of integer values, and returns an array with the largest K values in the file. The file contains only
integers, and every integer is followed by one or more space or linefeed characters. There may be multiple instances of an integer value in
the file. There is no limit to the size of the file. The array of values must be sorted in decreasing order. The array must not contain any
duplicate values.
int maxK(const char *filename, int *vals, int k);
The first parameter is string that is the name of the file to be opened. The second parameter is an array large enough to hold k integers.
The third parameter is the maximum number of integers to return.
The return value is the actual number of integers returned. (Suppose we ask for the 10 largest values, but there are only 5 integer values in
the file. The return value will be 5.) When the function returns, the vals array must hold the k largest values, sorted in decreasing order.
You may use any standard library function that appears on the C Reference Sheet.
2021/8/1 PQ5: Attempt review
https://moodle-courses2122.wolfware.ncsu.edu/mod/quiz/review.php?attempt=159707&cmid=111792 4/7
Question 4
Not answered
Points out of 1.00
An interval [start,end] is the sequence of integers between and including start and end. In other words, the interval represents all integers n
such that start ≤ n ≤ end. You can assume that start will always be less than or equal to end. You are given the following struct to represent
an interval:
struct interval {
int start; // lowest value of interval
int end; // highest value of interval
};
(1) Define a struct that can be used to create a linked list of intervals.
(2) Define a function that adds a new interval into a list of intervals. The intervals must be in order, such that the start of each interval is
less than or equal to the start of the next interval. If two intervals start with the same value, order doesn’t matter. The function must return
the head of the modified list.
// Part (1)
struct inode {
struct interval iv; // note: can’t use “int” as the name of the interval
struct inode * next;
};
// Part (2)
struct inode * addInterval(struct inode *h, struct interval i) {
struct inode * new = malloc(sizeof(struct inode));
new->iv = i;
new->next = NULL;
struct inode *p = h;
struct inode *prev = NULL;
while (p && p->iv.start < i.start) {
prev = p; p = p->next;
}
new->next = p;
if (prev) prev->next = new;
else h = new;
return h;
}
2021/8/1 PQ5: Attempt review
https://moodle-courses2122.wolfware.ncsu.edu/mod/quiz/review.php?attempt=159707&cmid=111792 5/7
Question 5
Not answered
Points out of 1.00
Suppose we have a linked list, and each element of the list is an array of integers. The list is built using the following struct:
struct arrayNode {
int *data; // array of integers
int size; // number of integers in the array
struct arrayNode *next;
};
Define a C function that computes the average (mean) of all integers in the list. The declaration of the function is:
double avg(struct arrayNode *list);
If the list is empty, then the function must return 0.
double avg(struct arrayNode *list) {
struct arrayNode *p;
double sum = 0.0;
int count = 0;
int i;
for (p = list; p; p = p->next) {
for (i=0; i < p->size; i++) sum += p->data[i];
count += p->size;
}
if (count == 0) return 0.0;
return sum / count;
}
2021/8/1 PQ5: Attempt review
https://moodle-courses2122.wolfware.ncsu.edu/mod/quiz/review.php?attempt=159707&cmid=111792 6/7
Question 6
Not answered
Points out of 1.00
Question 7
Not answered
Points out of 6.00
Given a variable h that points to the first node in a linked list of integers, write one or more statements to change each node’s value to be
the sum of its current value plus its position in the list. Add 1 to the first node; add 2 to the second node; add 3 to the third node, etc. The
value of h must not change. Declare any other variables that are needed. The struct used to build the linked list is shown below:
struct node {
int data;
struct node * next;
};
struct node *p = h; // value of h must not change
int position = 0;
while (p) { // traverse list
p->data += (++position); // parentheses are not necessary, just for clarity
p = p->next;
}
// alternate version using for loop
int position = 0;
struct node * p;
for (p = h; p; p = p->next) {
p->data += ++position;
}
For each statement below, tell whether the statement is true or false.
A linked list can dynamically grow or shrink to any size, limited only by available memory.
The size of an array is fixed when the array is allocated.
A linked list of N items will always require more space than an array of N items (of the same type).
The number of elements of an array can be determined by looking at the elements in the array.
The number of nodes in a linked list can be determined by looking at the nodes of the linked list.
It takes the same amount of time (measured in CPU cycles) to allocate and initialize an array of N items and a linked list of
N items.
2021/8/1 PQ5: Attempt review
https://moodle-courses2122.wolfware.ncsu.edu/mod/quiz/review.php?attempt=159707&cmid=111792 7/7
◄ PQ4
Jump to…
Quiz 1 Topics ►
https://moodle-courses2122.wolfware.ncsu.edu/mod/quiz/view.php?id=111791&forceview=1
https://moodle-courses2122.wolfware.ncsu.edu/mod/url/view.php?id=111793&forceview=1