Array List - LAB1
Array List - LAB1
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 1/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Câu hỏi 1
Đúng
Implement methods ensureCapacity, add, size in template class ArrayList representing the array list with type T with the
initialized frame. The description of each method is given in the code.
template <class T>
class ArrayList {
protected:
T* data; // dynamic array to store the list's items
int capacity; // size of the dynamic array
int count; // number of items stored in the array
public:
ArrayList(){capacity = 5; count = 0; data = new T[5];}
For example:
Test Result
ArrayList<int> arr; [19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
int size = 20; 20
Reset answer
1 template<class T>
2 ▼ void ArrayList<T>::ensureCapacity(int cap){
3 ▼ /*
4 if cap > capacity:
5 new_capacity = capacity * 1.5;
6 create new array with new_capacity
7 else: do nothing
8 */
9
10 ▼ if(cap > capacity){
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 2/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
11 int new_capacity = capacity * 1.5;
12 T *tmp = new T[new_capacity];
13 ▼ for(int i = 0; i < count; i++){
14 tmp[i] = data[i];
15 }
16 delete [] data;
17 data = tmp;
18 capacity = new_capacity;
19 }
20
21 }
22
23 template <class T>
24 ▼ void ArrayList<T>::add(T e) {
25 /* Insert an element into the end of the array. */
26 count++;
27 ensureCapacity(count);
28 data[count - 1] = e;
29
30 }
31
32 template<class T>
33 ▼ void ArrayList<T>::add(int index, T e) {
34 ▼ /*
35 Insert an element into the array at given index.
36 if index is invalid:
37 throw std::out_of_range("the input index is out of range!");
38 */
39 count++;
40 ensureCapacity(count);
41 ▼ for (int i = size() - 1; i > index; i--){
42 data[i] = data[i - 1];
43 }
44 data[index] = e;
45
46 }
47
48 template<class T>
49 ▼ int ArrayList<T>::size() {
50 /* Return the length (size) of the array */
51 return count;
52 }
53
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 3/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
ArrayList<int> arr; [19, 18, 17, 16, 15, 14, 13, 12, 11, [19, 18, 17, 16, 15, 14, 13, 12,
int size = 20; 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0] 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
20 0]
for(int index = 0; index < 20
size; index++){
arr.add(0, index);
}
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 4/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Câu hỏi 2
Đúng
Implement methods removeAt, removeItem, clear in template class ArrayList representing the singly linked list with type T
with the initialized frame. The description of each method is given in the code.
template <class T>
class ArrayList {
protected:
T* data; // dynamic array to store the list's items
int capacity; // size of the dynamic array
int count; // number of items stored in the array
public:
ArrayList(){capacity = 5; count = 0; data = new T[5];}
~ArrayList(){ delete[] data; }
};
For example:
Test Result
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 5/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Test Result
Reset answer
1 template<class T>
2 ▼ T ArrayList<T>::removeAt(int index){
3 ▼ /*
4 Remove element at index and return removed value
5 if index is invalid:
6 throw std::out_of_range("index is out of range");
7 */
8 ▼ if(index >= count || index < 0 || count == 0){
9 throw std::out_of_range("The index is out of range!");
10 }
11
12 count--;
13 ensureCapacity(count);
14 T removedItem = data[index];
15
16 for(int i = index; i < size(); i++)
17 ▼ {
18 data[i] = data[i + 1];
19 }
20 return removedItem;
21 }
22
23 template<class T>
24 ▼ bool ArrayList<T>::removeItem(T item){
25 /* Remove the first apperance of item in array and return true, otherwise return
26 ▼ for(int i = 0; i < count; i++){
27 ▼ if(data[i] == item){
28 ▼ for(int j = i; j < count - 1; j++){
29 data[j] = data[j + 1];
30 }
31 count--;
32 return true;
33 }
34 }
35 return false;
36 }
37
38 template<class T>
39 ▼ void ArrayList<T>::clear(){
40 ▼ /*
41 Delete array if array is not NULL
42 Create new array with: size = 0, capacity = 5
43 */
44 count = 0;
45 capacity = 5;
46 for (int i = 0; i < count; i++)
47 ▼ {
48 data[i] = 0;
49 }
50 delete[] this->data;
51 data = NULL;
52 this->data = new T[this->capacity];
53 }
54
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 6/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 7/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Câu hỏi 3
Không trả lời
Implement methods Get, set, clear, empty, indexOf, contains in template class ArrayList representing the array list with type
T with the initialized frame. The description of each method is given in the code.
template <class T>
class ArrayList {
protected:
T* data; // dynamic array to store the list's items
int capacity; // size of the dynamic array
int count; // number of items stored in the array
public:
void set(int index, T e); //set the index position in the list with the value e
int indexOf(T item); //get the first index of item in the list, else return -1
bool contains(T item); //check if the item is in the list
T removeAt(int index);
bool removeItem(T item);
};
Notice: You just have to implement the methods: set, get, clear, empty, indexOf, contains. Other methods have been
implemented already.
For example:
Test Result
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 8/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Test Result
Answer:
1
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 9/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 10/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Câu hỏi 4
Đúng
The function returns the array after applying all operation in operations.
Note:
- The iostream, and vector libraries have been included and namespace std is being used. No other libraries are allowed.
- You can write helper functions.
For example:
Test Result
Reset answer
vector<int> nums {13, 0, 6, 9, 14, 16}; [21, 8, 14, 9, 14, [21, 8, 14, 9,
vector<vector<int>> operations {{5, 5, 16}, {3, 4, 0}, {0, 2, 8}}; 32] 14, 32]
printVector(updateArrayPerRange(nums, operations));
vector<int> nums {19, 4, 3, 2, 16, 3, 17, 8, 18, 12}; [32, 28, 36, 41, [32, 28, 36,
vector<vector<int>> operations {{0, 3, 4}, {2, 5, 12}, {3, 6, 6}, 51, 61, 36, 21, 31, 41, 51, 61, 36,
{5, 8, 5}, {8, 9, 8}, {0, 5, 9}, {1, 7, 8}, {1, 1, 3}, {5, 5, 18}}; 20] 21, 31, 20]
printVector(updateArrayPerRange(nums, operations));
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 11/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Passed all tests!
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 12/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Câu hỏi 5
Đúng
The function returns if all the 1s appear consecutively in nums. If nums does not contain any elements, please return true
Note:
- The iostream and vector libraries have been included and namespace std are being used. No other libraries are allowed.
- You can write helper functions.
- Do not use global variables in your code.
For example:
Test Result
Reset answer
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 13/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 14/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Câu hỏi 6
Đúng
The prices of all cars of a car shop have been saved as an array called N. Each element of the array N is the price of each
car in shop. A person, with the amount of money k want to buy as much cars as possible.
Where nums is the array N, length is the size of this array and k is the amount of money the person has. Find the maximum
cars this person can buy with his money, and return that number.
Example:
The result is 3, he can buy the cars having index 1, 2, 3 (first index is 0).
Note: The library iostream, 'algorithm' and using namespace std have been used. You can add other functions but you are not
allowed to add other libraries.
For example:
Test Result
Reset answer
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 15/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 16/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Câu hỏi 7
Đúng
The function returns the smallest index i such that the sum of the numbers to the left of i is equal to the sum of the numbers
to the right.
If no such index exists, return -1.
Note:
- The iostream and vector libraries have been included and namespace std is being used. No other libraries are allowed.
- You can write helper functions.
For example:
Test Result
Reset answer
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 17/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 18/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
Câu hỏi 8
Đúng
The function returns the length of the longest subarray where all words share the same first letter.
Note:
- The iostream and vector libraries have been included and namespace std is being used. No other libraries are allowed.
- You can write helper functions.
For example:
Test Result
Reset answer
Đúng
Marks for this submission: 1,00/1,00.
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 19/20
9/25/24, 4:26 PM Array List: Xem lại lần làm thử | BK-LMS
https://lms.hcmut.edu.vn/mod/quiz/review.php?attempt=4101603&cmid=422111 20/20