You are on page 1of 9

6/30/22, 11:52 AM LAB2 (page 3 of 3)

Trang của tôi / Khoá học / Học kỳ III năm học 2021-2022 (Semester 3 - Academic year 2021-2022)

/ Chương trình Liên kết Quốc tế (International Study Programs)

/ Khoa Khoa học và Kỹ thuật Máy tính (Faculty of Computer Science and Engineering ) / Khoa Học Máy Tính

/ Programming Fundamentals (Lab) (CO1028)_Trần Hồng Tài (QT_HK213) / Chủ đề 2 / LAB2

Thời gian còn lại 0:07:36

e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=5324422&cmid=856477&page=2 1/9
6/30/22, 11:52 AM LAB2 (page 3 of 3)

Câu hỏi 12

Chính xác

Điểm 1,00 của 1,00

Hiện thực hàm sau:


int getValueAtIndex(int *ptr, int k);
Trả về giá trị của tại vị trí được chỉ định trong mảng qua con trỏ.
Trong đó:
ptr là con trỏ tới phần tử đầu tiên trong mảng.
k là vị trí cần truy xuất phần tử (giá trị này không vượt quá độ dài của mảng).
------------------------------------------------------------
Implement the following function:
int getValueAtIndex(int *ptr, int k);
Return value at the position of the index number through pointer.
Where:
ptr is a pointer to the first element in the array.
k is the access position (this value does not exceed the length of the array).
For example:

Test Result

int arr[] = {1, 2, 3, 4, 5}; 4


int k = 3;
cout << getValueAtIndex(arr, k);

Answer: (penalty regime: 0 %)

Reset answer

1 int getValueAtIndex(int *ptr, int k)


2 ▼ {
3 ▼ for (int i=0;i<k;i++){
4 return ptr[k];
5 }
6 }

Kiểm tra

Test Expected Got

e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=5324422&cmid=856477&page=2 2/9
6/30/22, 11:52 AM LAB2 (page 3 of 3)

Test Expected Got

 int arr[] = {1, 2, 3, 4, 5}; 4 4 


int k = 3;
cout << getValueAtIndex(arr, k);

Passed all tests! 

Chính xác

Điểm cho bài nộp này: 1,00/1,00.

e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=5324422&cmid=856477&page=2 3/9
6/30/22, 11:52 AM LAB2 (page 3 of 3)

Câu hỏi 13

Chính xác

Điểm 1,00 của 1,00

Hiện thực hàm sau:


void swap(int *ptr2, int *ptr2);
Thực hiện hoán đổi giá trị tại vị trí của 2 con trỏ trỏ tới.
Trong đó:
ptr1, ptr2 lần lượt là là các con trỏ trỏ tới vị trí cần hoàn đổi.
------------------------------------------------------------
Implement the following function:
void swap(int *ptr2, int *ptr2);
Swap the value at the position that 2 pointers point to.
Where:
ptr1, ptr2 respectively are pointers to swapping positions.

For example:

Test Result

int a = 5; 6 5
int b = 6;
swap(&a, &b);
cout << a << ' ' << b;

Answer: (penalty regime: 0 %)

Reset answer

1 void swap(int *ptr1, int *ptr2)


2 ▼ {
3 int tmp=*ptr1;
4 *ptr1=*ptr2;
5 *ptr2=tmp;
6 }

Kiểm tra

Test Expected Got

e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=5324422&cmid=856477&page=2 4/9
6/30/22, 11:52 AM LAB2 (page 3 of 3)

Test Expected Got

 int a = 5; 6 5 6 5 
int b = 6;
swap(&a, &b);
cout << a << ' ' << b;

Passed all tests! 

Chính xác

Điểm cho bài nộp này: 1,00/1,00.

e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=5324422&cmid=856477&page=2 5/9
6/30/22, 11:52 AM LAB2 (page 3 of 3)

Câu hỏi 14

Chính xác

Điểm 1,00 của 1,00

Hiện thực hàm sau:


void add(int *ptr, int n, int k);
Thực hiện thêm phần tử vào cuối của mảng 1 chiều được cho bởi con trỏ.
Trong đó:
ptr là con trỏ tới phần tử đầu tiên trong mảng.
n, k lần lượt là kích thước của mảng và phần tử cần được thêm vào.
------------------------------------------------------------
Implement the following function:
void add(int *ptr, int n, int k);
Insert element to the end of the 1-dimension array given by a pointer.
Where:
ptr is a pointer to the first element in the array.
n, k respectively is the size of the array and the element that need to be added.

For example:

Test Result

int arr[100] = {1, 2, 3, 4, 5}; 7


int n = 5;
int k = 7;
add(arr, n, k);
cout << arr[n];

Answer: (penalty regime: 0 %)

Reset answer

1 void add(int *ptr, int n, int k)


2 ▼ {
3 *(ptr+n)=k;
4 }

Kiểm tra

e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=5324422&cmid=856477&page=2 6/9
6/30/22, 11:52 AM LAB2 (page 3 of 3)

Test Expected Got

 int arr[100] = {1, 2, 3, 4, 5}; 7 7 


int n = 5;
int k = 7;
add(arr, n, k);
cout << arr[n];

Passed all tests! 

Chính xác

Điểm cho bài nộp này: 1,00/1,00.

Copyright 2007-2021 Trường Đại Học Bách Khoa - ĐHQG Tp.HCM. All Rights Reserved.
Địa chỉ: Nhà A1- 268 Lý Thường Kiệt, Phường 14, Quận 10, Tp.HCM.
Email: elearning@hcmut.edu.vn
Phát triển dựa trên hệ thống Moodle

e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=5324422&cmid=856477&page=2 7/9
6/30/22, 11:52 AM LAB2 (page 3 of 3)

Câu hỏi 15

Chính xác

Điểm 1,00 của 1,00

Hiện thực hàm sau:


bool isSymmetry(int *head, int *tail);
Kiểm tra mảng 1 chiều có phải là một mảng đối xứng hay không.
Trong đó:
head, tail lần lượt là con trỏ tới phần tử đầu tiên và cuối cùng trong mảng.
------------------------------------------------------------
Implement the following function:
bool isSymmetry(int *head, int *tail);
Checks if the 1-dimensional array is a symmetric array.
Where:
head, tail respectively are pointers to the first element and last element of the array.

For example:

Test Result

int arr[] = {1, 2, 1}; 1


cout << isSymmetry(arr, arr + (sizeof(arr) / sizeof(arr[0])) - 1);

Answer: (penalty regime: 0 %)

Reset answer

1 bool isSymmetry(int *head, int *tail)


2 ▼ {
3 for (int i=0;i<=(tail-head)/2;i++)
4 if(*(head+i)!=*(tail-i)) return 0;
5 return 1;
6 }

Kiểm tra

Test Expected Got

e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=5324422&cmid=856477&page=2 8/9
6/30/22, 11:52 AM LAB2 (page 3 of 3)

Test Expected Got

 int arr[] = {1, 2, 1}; 1 1 


cout << isSymmetry(arr, arr + (sizeof(arr) / sizeof(arr[0])) - 1);

Passed all tests! 

Chính xác

Điểm cho bài nộp này: 1,00/1,00.

e-learning.hcmut.edu.vn/mod/quiz/attempt.php?attempt=5324422&cmid=856477&page=2 9/9

You might also like