Consider an array a with n elements. The code snippet above is executed to calculate the value s. Which of the following options correctly describes the value of s after executing the code snippet?
4 phương ánCó giải thích
int a[MAX_SIZE];
int s = 0;
for (int i = 0; i < n; i++)
s = s + a[i] * a[i];Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "The sum of squares of all elements in the array a.". Biến s bắt đầu từ 0, vòng lặp đi qua mọi chỉ số từ 0 tới n trừ 1, và mỗi lượt cộng vào s tích của phần tử với chính nó, tức bình phương của nó. Vì không có câu điều kiện nào lọc bớt nên mọi phần tử đều góp phần, và vì bình phương không bao giờ âm nên s luôn không âm dù mảng có chứa số âm. Lựa chọn "The sum of all even elements in the array a." đòi một phép kiểm tra chia hết cho 2 trước khi cộng, điều mã không có. Lựa chọn "The sum of all odd elements in the array a." cũng đòi phép lọc theo tính chẵn lẻ tương tự. Lựa chọn "The sum of all elements in the array a." ứng với dòng cộng thẳng phần tử vào s, không có phép nhân phần tử với chính nó.
Cùng phần 7
What does the following program display?
#include <stdio.h>
int main() {
int a[5] = {1, 2, 3};
printf("%d %d %d %d %d", a[0], a[1], a[2], a[3], a[4]);
return 0;
}Có lời giảiCâu hỏiConsider the following code:
int func(int* a, int n){
int S = 0;
for(int i = 0; i < n; i++) {
S = S + (a[i] % 2)*a[i];
}
return S;
}
What does the above code snippet do?Có lời giảiCâu hỏiWhat does the expression sizeof(arr) / sizeof(arr[0]) evaluate to, where arr is an array?Có lời giảiCâu hỏiCả 21 câu của chủ đề21 câu
Xuất hiện trong 1 bộ đề
Đi tiếp từ đây