What will be the output of the following code? [code] #include <iostream> using namespace std; int main() { int arr[] = {1,2,3,4,5,6};

What will be the output of the following code?
Code:
#include <iostream>
using namespace std;
int main()
{   
    int arr[] = {1,2,3,4,5,6};
    int n = sizeof(arr)/sizeof(arr[0]);
    int d=4;
    int temp[10];
    for(int i=0;i<d;i++)
    temp[i]=arr[i];
    int j=0;
    for(int i=d;i<n;i++,j++)
    arr[j]=arr[i];
    int k=0;
    for(int i=n-d;i<n;i++,k++)
    arr[i]=temp[k];
    for(int i=0;i<n;i++)
    cout<<arr[i]<<" ";
    return 0;
}
A. 5 6 1 2 3 4
B. 6 5 4 3 1 2
C. 3 4 5 6 1 2
D. error
 
Answer: a
Explanation: The given code rotates the given array by 4. It does so by using an array temp[] which stores the first d elements and then shift them to the end of the array. So the output will be 5 6 1 2 3 4.
 

Tìm kiếm

Back
Top