What will be the output of the following code? [code] #include <bits/stdc++.h> using namespace std; void func(int a[], int n, int k)

Thành Đạt

New member
What will be the output of the following code?
Code:
#include <bits/stdc++.h> 
using namespace std; 
void func(int a[], int n, int k) 
{ 
	if (k <= n) 
	{ 
		for (int i = 0; i < k/2; i++) 
		swap(a[i], a[k-i-1]); 
	} 
} 
int main() 
{ 
	int a[] = {1, 2, 3, 4, 5}; 
	int n = sizeof(a) / sizeof(int), k = 3; 
	func(a, n, k); 
	for (int i = 0; i < n; ++i) 
		cout << a[i]<<" ";
	return 0; 
}
A. 3 2 1 4 5
B. 5 4 3 2 1
C. 1 2 5 4 3
D. error
 
Answer: a
Explanation: The given code reverses only a specified segment of the input array. As the value of k is given to be 3 in the code thus only the first three elements of the array will be reversed.
 

Tìm kiếm

Back
Top