100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

COS2611 Assignment 4 solutions

Rating
-
Sold
-
Pages
71
Uploaded on
30-09-2025
Written in
2025/2026

Question 1 Complete Mark 3.00 out of 3.00 Match each description with its correct data type. Represents a single character, usually 1 bytechar Single-precision floating-point numberfloat Double-precision floating-point numberdouble Boolean type, stores true or falsebool Typically stores integers in the range of -32,768 to 32,767 (2 bytes)short Typically stores integers in the range of -2,147,483,648 to 2,147,483,647int Extended size integer, at least 8 byteslong longQuestion 2 Complete Mark 2.00 out of 2.00 A linked list is a random access data structure True False Question 3 Complete Mark 2.00 out of 2.00 Suppose that the pointer head points to the first node in the list, and the link of the last node is nullptr. The first node of the linked list contains the address of the ____ node. d Question 4 Complete Mark 2.70 out of 3.00 Match each of the characteristics with the correct data structure (Queue or Stack). Follows First In, First Out (FIFO)Queue Uses push() to add an elementStack Access the element at the front using front()Queue Access the element at the top using top()Stack Follows Last In, First Out (LIFO)Stack Uses pop() to remove an elementBoth (Queue & Stack) Elements are processed in the order they were addedQueue The last element added is processed firstStack Allows direct access to any element by indexNeither Queue nor Stack Allows sorting of elementsNeither Queue nor StackQuestion 5 Complete Mark 3.00 out of 3.00 Study the incomplete code. Using a vector, the purpose of the code is to sort the elements of the queue in ascending order and then to display the sorted elements. Complete the code by dragging the correct option(s) from the list of available code blocks. void displaySortedQ(const queue<int>& q) { queue<int> tempQ = q; //create a copy of the queue vector<int> elements; //create a vector to save the elements while ( !tempQ.empty() ){ _back(tempQ.front()); tempQ.pop(); } sort((), ()); //sort the elements //Display the sorted elements for (int elem : elements) { cout << elem << " "; } } int main() { queue<int> q; //initialise the queue //add elements (3); (1); (4); (2); (5); displaySortedQ(q); //call the function return 0; } not(tempQ.empty()) tempQ.empty() _back(tempQ.last()); (tempQ.front()); tempQ.last(); tempQ.next(); tempQ.first(); tempQ.empty().notEmpty _back(tempQ);sort(elements); sort(begin(),end()); () for (elem in elements) { cout << elem << " "; } for (int elem in elements) { cout << elem << " "; } displaySortedQ(); for (int elem : elements) { cout << elemement[elem] << " "; } displaySortedQ; displaySortedQ(int q);Question 6 Complete Mark 2.00 out of 2.00 Study the following incomplete code. int val1 = 10; int val2 = 20; int val3 = 30; std::stack<int> numberStack; numberS(val1); numberS(val2); numberS(val3); ...(i)... Which of the following code (i) will correctly display the elements of numberStack in descending order? a. while (!numberS()) { std::cout << numberS() << std::endl; numberS(); } b. while (!numberS()) { std::cout << numberS().desc() << std::endl; } c. while (!numberS()) { numberS(); std::cout << numberS() << std::endl; } d. while (!numberS()) { std::cout << numberS() << std::endl; numberS(); } e. while (!numberS()) { numberS(); std::cout << numberS() << std::endl; } f. None of the options provided is correct.Question 7 Complete Mark 2.00 out of 2.00 The difference between a linear representation of a stack and a linked implementation of a stack is that in a linked implementation of stacks: a.Elements are stored as nodes with references/pointers to the next element. b.Elements are typically stored in a fixed-size array or a dynamic array. c.The stack operations are performed based on the indices of the elements within the array. d.None of the options given. Question 8 Complete Mark 2.00 out of 2.00 Study the incomplete code. The outcome of this code should be: 10 20 30 Complete the code by dragging the relevant code segments into the empty spaces in the code. Code: std::queue<int> numberQueue; // Adding elements to the queue numberQ(10); numberQ(20); numberQ(30); // Remove and display elements until the queue is empty while ( !numberQ() ) { std::cout << numberQ() << std::endl; numberQ(); // Remove the element after displaying it } numberQ() numberQ(); not(numberQ())Question 9 Complete Mark 0.00 out of 2.00 The algorithmic complexity of the given code fragment is O(n log n). for (int i = 0 ; i < n; i *=2) for (int j = 0; j < n; j++) sum++; Which of the following explanations accurately describes the algorithmic complexity? a.The code fragment contains nested loops, each iterating based on the input size 'n,' leading to linear time complexity. b.The code fragment contains a single loop that iterates from 0 to 'n,' resulting in quadratic time complexity. c.The code fragment performs a constant-time operation within a loop, making it O(1). d.The code fragment has a logarithmic relationship with the input size 'n,' resulting in O(n log n) time complexity. e.None of the provided options correctly describes the algorithmic complexity. Question 10 Complete Mark 0.00 out of 2.00 Suppose an algorithm has a time complexity of O( n ) and takes 10 seconds to process an input of size 1000. How long would it approximately take to process an input of size 200 if the input size and runtime are linearly related? a.2 seconds b.5 seconds c.10 seconds d.20 seconds e.None of the provided options. Question 11 Complete Mark 0.00 out of 2.00 Give the algorithmic complexity of the code fragment: for (int i = 1; i < n; i = i*2) sum++; a.O(1) b.O(log n) c.O(n2) d.O(n log n)Question 12 Complete Mark 2.00 out of 2.00 Which of the following best describes the time complexity of binary search compared to sequential search? a.Binary search has a time complexity of O(log n), while sequential search has a time complexity of O( n ). b.Binary search has a time complexity of O( n ), while sequential search has a time complexity of O(log n). c.Both binary search and sequential search have a time complexity of O( n ). d.Both binary search and sequential search have a time complexity of O(log n). Question 13 Complete Mark 2.00 out of 2.00 In bubble sort, after completing a pass through the array, which of the following statements is true? a.The largest element is guaranteed to be at its correct final position. b.The smallest element is guaranteed to be at its correct final position. c.The array is guaranteed to be sorted. d.The array is guaranteed to be partially sorted. Question 14 Complete Mark 2.00 out of 2.00 Consider an array of length n containing distinct integer values. Which of the following statements regarding the number of comparisons performed by the bubble sort algorithm is true? a.The number of comparisons is always exactly n. b.The number of comparisons is at most n-1. c.The number of comparisons is at least n. d.The number of comparisons is at most n(n−1) 2Question 15 Complete Mark 0.00 out of 2.00 Consider the following pseudo code for heap sort: HeapSort(array): n = length of array // Build max heap for i = n/2 - 1 down to 0 Heapify(array, n, i) // Extract elements from heap one by one for i = n - 1 down to 0 swap array[0] and array[i] Heapify(array, i, 0) Heapify(array, n, i): largest = i left = 2 * i + 1 right = 2 * i + 2 if left < n and array[left] > array[largest] largest = left if right < n and array[right] > array[largest] largest = right if largest != i swap array[i] and array[largest] Heapify(array, n, largest) Based on the provided pseudo code for heap sort, which of the following statements is correct? a.Heap sort has a worst-case time complexity of O(n^2). b.) Heap sort builds a min-heap during the heapification process. c.The main loop in heap sort iterates through each element in the array and performs heapify operation. d.Heap sort is an unstable sorting algorithm.Question 16 Complete Mark 2.00 out of 2.00 Consider the following pseudo code for the funtions MergeSort and Merge: Function MergeSort(arr, left, right): If left >= right: Return mid = (left + right) / 2 // Recursively sort first half MergeSort(arr, left, mid) // Recursively sort second half MergeSort(arr, mid + 1, right) // Merge the two halves Merge(arr, left, mid, right) Function Merge(arr, left, mid, right): n1 = mid - left + 1 n2 = right - mid // Create temporary arrays LeftArray = new array of size n1 RightArray = new array of size n2 // Copy data to temp arrays For i from 0 to n1 - 1: LeftArray[i] = arr[left + i] For j from 0 to n2 - 1: RightArray[j] = arr[mid + 1 + j] // Merge the temp arrays back into arr[left..right] i = 0 j = 0 k = left While i < n1 AND j < n2: If LeftArray[i] <= RightArray[j]: arr[k] = LeftArray[i] i = i + 1 Else: arr[k] = RightArray[j] j = j + 1 k = k + 1 // Copy any remaining elements of LeftArray While i < n1: arr[k] = LeftArray[i] i = i + 1 k = k + 1 // Copy any remaining elements of RightArray While j < n2: arr[k] = RightArray[j] j = j + 1 k = k + 1 Consider now an array, arr = [8,5,4,10,12,1,7] Below is the list of steps for splitting the array arr when MergeSort(arr,0,6) is called. Splitting steps:Split [0-6] into [0-3] and [4-6] Split [0-3] into [0-1] and [2-3] Split [0-1] into [0-0] and [1-1] → base cases Split [2-3] into [2-2] and [3-3] → base cases Split [4-6] into [4-5] and [6-6] Split [4-5] into [4-4] and [5-5] → base cases Indicate which of the following is the most correct to indicate the Merging steps Select one: a. Merge [0-0] and [1-1] → [5,8] Merge [2-2] and [3-3] → [4,10] Merge [0-1] and [2-3] → merge [5,8] and [4,10] → [4,5,8,10] Merge [4-4] and [5-5] → [12,1] → [1,12] Merge [4-5] and [6-6] → merge [1,12] and [7] → [1,7,12] Merge [0-3] and [4-6] → merge [4,5,8,10] and [1,7,12] → [1,4,5,7,8,10,12] b. Merge [0-0] and [1-1] → [5,8] Merge [2-2] and [3-3] → [4,10] Merge [0-1] and [2-3] → merge [5,8] and [4,10] → [5,8,4,10] Merge [4-4] and [5-5] → [12,1] → [12,1] Merge [4-5] and [6-6] → merge [12,1] and [7] → [12,1,7] Merge [0-3] and [4-6] → merge [4,5,8,10] and [1,7,12] → [1,4,5,7,8,10,12] c. Merge [0-0] and [2-3] → [4,5,8,10] Merge [4-5] and [6-6] → merge [1,12] and [7] → [1,7,12] Merge [0-3] and [4-6] → merge [4,5,8,10] and [1,7,12] → [1,4,5,7,8,10,12]Question 17 Complete Mark 4.00 out of 4.00 Consider the following array: array = [38, 27, 43, 3, 9, 82, 10]. Using merge sort – the first round of the merge sort algorithm will be [38] [27] [43] [3] [9] [82] [10]. What will the second round of the merge sort algorithm be: a.[27, 38], [3, 43], [9, 82], [10] b.[38, 27], [43, 3], [82, 9], [10] c.[38,27,43],[3,9,82] , [10] d.[3,27,38], [9,43,82,10] Question 18 Complete Mark 2.00 out of 2.00 Consider an array of length n containing integer values. Which of the following statements regarding the time complexity of the merge sort algorithm is true? a.Merge sort has a time complexity of O( n ) in all cases. b.Merge sort has a time complexity of O(n log n) in the worst-case scenario. c.Merge sort has a time complexity of O(n^2) in the best-case scenario. d.Merge sort has a time complexity of O(log n) in the average-case scenario.Question 19 Complete Mark 1.00 out of 1.00 The user created the following array: int [ ] numbers = {12, 23, 25, 38, 8, 62 , 6, 46} 1. He applied a binary search algorithm to determine the index for '23'. The following result is returned: Element 23 found at index 1 2. Using the same code, he then requested to determine the index for '8'. The following result is returned: Element not found in the array He is not sure why, in the second instance, '8' was not found. You explained to him that the reason is because ... a.To apply binary search on an array, the array must first be sorted. b.The element is not found, because the target element is not in the array. c.The time complexity is O(log n), which will result in the compiler going into an end-less loop if the element is not found. d. None of the options provided, is correct.Question 20 Complete Mark 3.00 out of 3.00 Consider the following and indicate what the purpose is of this code: #include <iostream> using namespace std; void Print(int arr[], int n) { if (n <= 0) return; cout << arr[n - 1] << " "; Print(arr, n - 1); } int main() { int arr[] = {1, 2, 3, 4, 5}; Print(arr, 5); return 0; } Select one: a.Prints array in original order b.Prints array in reverse order c.Prints only the first element d.Does not print anythingQuestion 21 Complete Mark 2.00 out of 2.00 Consider the following code and indicate what the output will be: #include <iostream> using namespace std; void func(int n) { if (n == 0) return; cout << n << " "; func(n - 1); cout << n << " "; } int main() { func(3); return 0; } Select one: a.321123 b.123321 c.321 d.123Question 22 Complete Mark 2.00 out of 2.00 The following snipit of code represents an pre-order traversal on a binary tree. void preOrderTraversal(Node* node) { if (node == nullptr) { return; } cout << node->data << " "; preOrderTraversal(node->left); preOrderTraversal(node->right); } Which of the following is the most correct explanation of the pre-order traversal? a. First, recursively visit the left subtree. Then, visit the root node. Finally, recursively visit the right subtree. b. First, recursively visit the left subtree. Then, recursively visit the right subtree. Finally, visit the root node. c. First, visit the root node Then, recursively visit the left subtree. Finally, recursively visit the right subtree. d. None of the options provided are correct.Question 23 Complete Mark 0.00 out of 2.00 Consider the following binary search tree (BST): If you want to delete the node with the value 7 while maintaining the BST properties, what would be the resulting tree? a. b.c. d.It is not possible to delete node from a BST e.None of the options provided are correctQuestion 24 Complete Mark 0.00 out of 2.00 Given the following sequence of numbers: 25,13,44,8,15.24,33 a binary search tree (BST) is constructed by inserting the numbers in the given order. Which of the following options correctly represents the structure of the binary search tree? a. b.c. d. e. None of the options provided are correct.Question 25 Complete Mark 0.00 out of 2.00 Consider the BST: If you perform a postorder traversal on this binary search tree, what would be the correct sequence of visited nodes? a.3 4 5 8 10 15 16 18 b.15 4 3 5 10 8 18 16 c.3 8 10 5 4 16 18 15 d. 5 4 3Question 26 Complete Mark 0.00 out of 2.00 Given the following: Vertex set: V(H)={0,1,2,3,4,5,6} for graph H. Indicate which of the following edge sets E(H) correctly represents the graph. a. b. c. d. e. E(H)={(0,2),(0,3),(1,0),(2,4),(3,4),(4,5),(5,6),(2,6)} E(H)= {(1,0),(0,2),(2,6)} or E(H)= {(1,0),(0,3),(3,4),(4,5),(5,6)} or E(H)={(1,0),(0,2),(2,4),(4,5),(5,6)} Unable to determine the edge set, as the starting and end points do not connect. E(H)={(0,2,3),(1,0),(2,4,6),(3,4,5,6)} E(H)={(6,4,2,0),(4,3,0),(0,1),(6,4,2),(4,3),(6,5,4),(6,5),(6,2)}Question 27 Complete Mark 0.00 out of 2.00 Given the following graph M with: Vertex set: V(M)={0,1,2,3,4,5} Edge set: E(M)={(0,1),(1,5),(5,0),(0,4),(4,3),(3,2),(3,1),(2,1)} Which of the following is the correct adjacency matrix for the graph M? Each row and column correspond to the vertices in the order {0,1,2,3,4,5} a. b. c.It is not a solvable Edge set - unable to draw a matrix d.Not one of the options availabe, is correct e.Question 28 Complete Mark 3.00 out of 3.00 Consider the following graph with six towns connected as follows: Edges and distances: T1 —10→ T2 T1 —15→ T3 T2 —1→ T3 T2 —12→ T4 T4 —2→ T5 T3 —5→ T5 T5 —5→ T6 T3 —10→ T6 vector<vector<int>> graph = { {0,10,15,0,0,0}, {10,0,1,12,0,0}, {15,1,0,0,5,10}, {0,12,0,0,2,0}, {0,0,5,2,0,5}, {0,0,10,0,5,0} }; Which of the following is a correct representation of finding the shortest distances and paths between towns using Dijkstra’s Algorithm starting at T1? Note: the representations refer to Vertexes (starting at 0). Select one: a. Vertex Distance from start Path 000 1100 -> 1 2110 -> 1 -> 2 3180 -> 1 -> 2 -> 4 -> 3 4160 -> 1 -> 2 -> 45 b. c. d. 21 Vertex Distance from start 0 -> 1 -> 2 -> 5 Path 1100 -> 1 2110 -> 1 -> 2 3180 -> 1 -> 2 -> 4 -> 3 4160 -> 1 -> 2 -> 4 5210 -> 1 -> 2 -> 5 Vertex Distance from start Path 100 2101 -> 2 3111 -> 2 -> 4 4221 -> 2 -> 4 5241 -> 2 -> 4 -> 5 6251 -> 3 -> 6 Vertex Distance from start 6 21 Path 0 --> 1 --> 2 --> 3 --> 5 --> 6 Question 29 Complete Mark 2.00 out of 2.00 Which of the following statements is true regarding the std::stack, std::queue, and std::priority_queue containers in C++? Select one: ::stack and std::queue are both FIFO (First-In-First-Out) data structures. ::priority_queue always retrieves the element with the highest priority first, regardless of insertion order. ::queue allows insertion and removal of elements at both ends. ::stack and std::priority_queue both follow the LIFO (Last-In-First-Out) order.Question 30 Complete Mark 6.00 out of 6.00 Consider the following code and select the most correct respresentaion of the outcome for this code: // Imagine you have a scooter with a topbox (vector) that you use for deliverying packages (elements). std::vector<std::string> topbox; std::cout << "1. Size of the topbox initially: " << () << "n"; std::cout << "2. Capacity of the topbox initially: " << ity() << "n"; //Adding stuff to the topbox _back("Helmet"); _back("Tools"); _back("Lunch box"); std::cout << "3. Size of the topbox after adding stuff: " << () << "n"; std::cout << "4. Capacity of the topbox at this stage: " << ity() << "n"; //You are worried that the topbox will become to full for all the stuff and added a limit to the capacity ve(5); std::cout << "5. Size of the topbox after adding a reserve: " << () << "n"; std::cout << "6. Capacity of the backpack at this stage: " << ity() << "n"; //Adding more stuff to the topbox _back("Book"); _back("Jacket"); _back("Laptop"); std::cout << "7. Size of the topbox after adding more stuff: " << () << "n"; std::cout << "8. Capacity of the topbox at this stage: " << ity() << "n"; //Removing stuff from the topbox _back(); _back(); std::cout << "9. Size of the topbox after removing stuff: " << () << "n"; std::cout << "10. Capacity of the topbox at this stage: " << ity() << "n"; //Resize the topbox e(()); std::cout << "11. Size of the topbox after resizing: " << () << "n"; std::cout << "12. Capacity of the topbox at this stage: " << ity() << "n"; a. 1. Size of the topbox initially: 0 2. Capacity of the topbox initially: 0 3. Size of the topbox after adding stuff: 3 4. Capacity of the topbox at this stage: 3 5. Size of the topbox after adding a reserve: 3 6. Capacity of the backpack at this stage: 5 7. Size of the topbox after adding more stuff: 6 8. Capacity of the topbox at this stage: 7 9. Size of the topbox after removing stuff: 4 10. Capacity of the topbox at this stage: 711. Size of the topbox after resizing: 4 12. Capacity of the topbox at this stage: 7 b. 1. Size of the topbox initially: 0 2. Capacity of the topbox initially: 0 3. Size of the topbox after adding stuff: 3 4. Capacity of the topbox at this stage: 3 5. Size of the topbox after adding a reserve: 3 Error at this stage as you cannot add a more elements to a vector's initial declaration c. 1. Size of the topbox initially: 0 2. Capacity of the topbox initially: 0 3. Size of the topbox after adding stuff: 3 4. Capacity of the topbox at this stage: 3 5. Size of the topbox after adding a reserve: 3 6. Capacity of the backpack at this stage: 6 7. Size of the topbox after adding more stuff: 6 8. Capacity of the topbox at this stage: 6 9. Size of the topbox after removing stuff: 4 10. Capacity of the topbox at this stage: 6 11. Size of the topbox after resizing: 4 12. Capacity of the topbox at this stage: 6 d.None of the options e.I don't know the solution Question 31 Complete Mark 6.00 out of 6.00 Match each C++ Standard Library container to its correct category: std::vectorSequence Container std::mapAssociative Container std::stackContainer Adapter std::listSequence Container std::setAssociative Container std::queueContainer AdapterQuestion 32 Complete Mark 2.00 out of 2.00 Which of the following operations is NOT supported by the std::stack container in C++? Select one: a.Accessing the top element b.Inserting an element at the front c.Removing the top element d.Checking if the stack is empty Question 33 Complete Mark 2.00 out of 2.00 Consider the following statement and select the correct output of the code when executed. std::vector<int> numbers; _back(5); _back(10); _back(15); if (!()) { (() + 1); } std::cout << "n Remaining elements in the vector: "; for (int num : numbers) { std::cout << num << " "; } Select one: a.The code will not compile due to an error. b.The code will compile and execute, but it will result in a runtime error. c.The output will be: "Remaining elements in the vector: 10 15". d.The output will be: "Remaining elements in the vector: 5 10 15". e.The output will be: "Remaining elements in the vector: ". f.The output will be: "Remaining elements in the vector: 5 15".Question 34 Complete Mark 5.00 out of 5.00 Match the following expressions with their corresponding actions on a deque object: Add the value 30 to the back of the _back(30); Remove the first element from the front of the _front(); Add the value 25 to the front of the _front(25); Retrieve the value of the last element in the (); Retrieve the value of the first element in the ();Question 35 Complete Mark 2.00 out of 2.00 Study the following code and indicate which of the trace diagrams correctly describes the different steps in the code. #include <iostream> #include <deque> int main() { // Create a deque of integers std::deque<int> dq; // Add elements to the front and back _back(1); _back(2); _front(0); _front(-1); // Print the contents of the deque std::cout << "Deque contents: "; for (const int &elem : dq) { std::cout << elem << " "; } std::cout << std::endl; // Access elements std::cout << "First element: " << () << std::endl; std::cout << "Last element: " << () << std::endl; // Remove elements from the front and back _front(); // Removes -1 _back(); // Removes 2 // Print the contents after removals std::cout << "Deque contents after pop operations: "; for (const int &elem : dq) { std::cout << elem << " "; } std::cout << std::endl; // Check the size of the deque std::cout << "Deque size: " << () << std::endl; return 0; } Select one: a. Initial State: Empty deque. After push_back(1): Deque contains [1]. After push_back(2): Deque contains [1, 2]. After push_front(0): Deque contains [0, 1, 2]. After push_front(-1): Deque contains [-1, 0, 1, 2]. After pop_front(): Deque contains [0, 1, 2]. After pop_back(): Deque contains [0, 1]. Final Size: 2. b. Initial State: Empty deque. After push_back(1): Deque contains [1]. After push_back(2): Deque contains [2,1]. After push_front(0): Deque contains [0, 2, 1]. After push_front(-1): Deque contains [-1, 0, 2, 1]. After pop_front(): Deque contains [0, 2, 1].After pop_back(): Deque contains [0, 2]. Final Size: 2. c. Initial State: Empty deque. After push_back(1): Deque contains [1]. After push_back(2): Deque contains [2,1]. After push_front(0): Deque contains [2,1,0]. After push_front(-1): Deque contains [2,1,0.-1]. After pop_front(): Deque contains [1,0,-1]. After pop_back(): Deque contains [1,0]. Final Size: 2. d. None of the options provided is correct. Question 36 Complete Mark 4.00 out of 4.00 Match the following expressions with their corresponding actions on a list container: Add the value 30 to the back of the _back(30); Remove the first element from the front of the _front(); Add the value 25 to the front of the _front(25); Retrieve the value of the last element in the (); Retrieve the value of the first element in the (); Remove elements from the list based on a given e_if(predicate); Remove consecutive duplicate elements from the e(); Sort the elements in the list in ascending (); Merge the elements from another list into this list while maintaining the sorted (otherList);Question 37 Complete Mark 2.00 out of 2.00 Consider the incomplete code below and indicate which of the code (i) will: Modify the targetList using the splice function to move elements from the sourceList as indicated in the comment. #include <iostream> #include <list> int main() { std::list<int> sourceList = { 8,9,4 }; std::list<int> targetList = { 5,1,3 }; //result targetList should be {5,1,3,8,9,4} //--(i)-- std::cout << "Modified targetList n"; for (int item : targetList) { std::cout << item << " "; } } Select one: tLe(targetL(), sourceList); tLe(targetL(), sourceList); tLe(targetL(), sourceList, sourceL()); tLe(targetL(), sourceList, sourceL());Question 38 Complete Mark 5.00 out of 5.00 Study the code below and then link the explanation with the code: #include <iostream> #include <vector> int main() { // Create a vector of integers std::vector<int> numbers = {10, 20, 30, 40, 50}; // Declare an iterator for the vector std::vector<int>::iterator it; // Iterate through the vector using the iterator std::cout << "Elements in the vector: "; for (it = (); it != (); ++it) { std::cout << *it << " "; // Dereference the iterator to access the element } std::cout << std::endl; // Modify elements in the vector using the iterator for (it = (); it != (); ++it) { *it += 5; // Add 5 to each element } // Print the modified vector std::cout << "Modified elements in the vector: "; for (it = (); it != (); ++it) { std::cout << *it << " "; } std::cout << std::endl; return 0; } A vector numbers is initialized with the elements {10, 20, 30, 40, 50}. Declares an iterator it for the vector. This iterator will be used to traverse the vector. The loop uses the iterator to traverse the vector from the first element (()) to the end The second loop modifies each element in the vector by adding 5 to it using the iterator. Creating a std::vector std::vector::iterator it; for (it = (); it != (); ++it) *it += 5;Question 39 Complete Mark 2.00 out of 2.00 Consider a scenario where you have a std::vector<int> named numbers containing the values [10, 20, 30, 40, 50]. You need to use an iterator to find and print the sum of all the elements in the vector. Which code snippet accomplishes this task correctly ? Select one: a. vector<int> numbers = { 10,20,30,40,50 }; int sum = 0; for (int num : numbers) { sum += num; } b. vector<int> numbers = { 10,20,30,40,50 }; int sum = 0; for (auto it = (); it != (); ++it) { sum += *it; } c. vector<int> numbers = { 10,20,30,40,50 }; int sum = 0; for (unsigned int i = 0; i < (); ++i) { sum += numbers[i]; } d. vector<int> numbers = { 10,20,30,40,50 }; int sum = 0; for (auto& num : numbers) { sum += num; }Question 40 Complete Mark 4.00 out of 4.00 Match the definition with the algorithm function Searches for the first occurrence of a value in a ::find Searches for the first element in a range that satisfies a given ::find_if Searches for the first element in a range that does not satisfy a given ::find_if_not Compares two sequences and returns true if they are ::equal Finds the first position where two sequences ::mismatch Searches for a subsequence within a sequencestd::search Searches for the first occurrence of two consecutive elements that are equal (or satisfy a given predicate). std::adjacent_findQuestion 41 Complete Mark 2.00 out of 2.00 Consider the code below and indicate what the output will be: #include <iostream> #include <vector> #include <algorithm> bool testVal(int num) { return num % 3 == 0; } void removeNumbers(std::vector<int>& numbers) { ( std::remove_if((), (), testVal), () ); } int main() { std::vector<int> numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; removeNumbers(numbers); // Display the result vector std::cout << "Numbers after removing values: "; for (int num : numbers) { std::cout << num << " "; } std::cout << std::endl; return 0; } Select one: a.Numbers after removing values: 1 2 4 5 7 8 10 b.Numbers after removing values: 1 2 3 5 6 7 9 10 c.Numbers after removing values: 1 3 5 7 9 d.Numbers after removing values: 1 2 3 4 5 6 8 9 10

Show more Read less











Whoops! We can’t load your doc right now. Try again or contact support.

Document information

Uploaded on
September 30, 2025
Number of pages
71
Written in
2025/2026
Type
Other
Person
Unknown

Content preview

COS2611-25-Y  Assessment 4


QUIZ




Started on Monday, 29 September 2025, 7:53 PM
State Finished
Completed on Monday, 29 September 2025, 9:33 PM
Time taken 1 hour 40 mins
Marks 97.70/106.00
Grade 92.17 out of 100.00


Information




This assessment covers the complete curriculum and outcomes and is also a good preparation for the written part of the
exam. [Remember - the final exam will consist of two sections: the written part (quiz) and a Final Practical Project].

Work through Class 4 and Chapter 21 in the prescribed textbook, before you attempt the assessment.

You have two (2) attempts to complete this assessment. The system will automatically upload your attempts when the due
date and time is reached. The due date is Wednesday, 1 October 2025, 11:00 PM. Note that this is the last day that UNISA
will accept assessments - thus no extension can be given.

Remember, the due date is the last date for submission and not the day on which you should start with the assessment.




Question 1

Complete

Mark 3.00 out of 3.00




Match each description with its correct data type.


Represents a single character, usually 1 byte char

Single-precision floating-point number float

Double-precision floating-point number double

Boolean type, stores true or false bool

Typically stores integers in the range of -32,768 to 32,767 (2 bytes) short

Typically stores integers in the range of -2,147,483,648 to 2,147,483,647 int

Extended size integer, at least 8 bytes long long

,Question 2

Complete

Mark 2.00 out of 2.00




When you build a linked list in the backward manner, a new node is always inserted at the end of the linked list.


True

False




Question 3

Complete

Mark 2.00 out of 2.00




When you build a linked list in the backward manner, a new node is always inserted at the end of the linked list.


True

False




Question 4

Complete

Mark 2.70 out of 3.00




Match each of the characteristics with the correct data structure (Queue or Stack).


Follows First In, First Out (FIFO) Queue

Uses push() to add an element Stack

Access the element at the front using front() Queue

Access the element at the top using top() Stack

Follows Last In, First Out (LIFO) Stack

Uses pop() to remove an element Both (Queue & Stack)

Elements are processed in the order they were added Queue

The last element added is processed first Stack

Allows direct access to any element by index Neither Queue nor Stack

Allows sorting of elements Neither Queue nor Stack

,Question 5

Complete

Mark 2.00 out of 2.00




Study the incomplete code. The outcome of this code should be:

10

20

30



Complete the code by dragging the relevant code segments into the empty spaces in the code.



Code:

std::queue<int> numberQueue;

// Adding elements to the queue

numberQueue.push(10);

numberQueue.push(20);

numberQueue.push(30);



// Remove and display elements until the queue is empty

while ( !numberQueue.empty() )

{

std::cout << numberQueue.front() << std::endl;

numberQueue.pop(); // Remove the element after displaying it

}

numberQueue.pop()


numberQueue.start();




not(numberQueue.empty())

, Question 6

Complete

Mark 2.00 out of 2.00




Study the following incomplete code.

int val1 = 10;
int val2 = 20;
int val3 = 30;
std::stack<int> numberStack;
numberStack.push(val1);
numberStack.push(val2);
numberStack.push(val3);


...(i)...

Which of the following code (i) will correctly display the elements of numberStack in descending order?




a. while (!numberStack.empty())
{
std::cout << numberStack.top() << std::endl;
numberStack.pop();
}

b.
while (!numberStack.empty())
{
std::cout << numberStack.top().desc() << std::endl;
}



c. while (!numberStack.empty())
{
numberStack.pop();
std::cout << numberStack.top() << std::endl;
}

d. while (!numberStack.empty())
{
std::cout << numberStack.pop() << std::endl;
numberStack.top();
}

e.
while (!numberStack.empty())
{
numberStack.top();
std::cout << numberStack.pop() << std::endl;
}



f. None of the options provided is correct.

Get to know the seller

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
CrystalIndigo University of South Africa (Unisa)
View profile
Follow You need to be logged in order to follow users or courses
Sold
486
Member since
5 year
Number of followers
226
Documents
73
Last sold
3 months ago
CrystalIndigo Solutions

providing all solutions to all computer science modules

4,1

51 reviews

5
27
4
13
3
6
2
1
1
4

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can immediately select a different document that better matches what you need.

Pay how you prefer, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card or EFT and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions