Q:

Rotation of an array

This is a C++ code and I feel its correct, yet it doesn't work.
I will explain a bit for understanding
t - number of tries.
n - size of array.
k - number of rotation. 
Input: 1 2 3 4 5 .
for k=2, Output: 4 5 1 2 3. 
Please advise on the same.
#include<iostream>

using namespace std;

int main() {
    int t,n,k;
    cin >> t;
    int s = 0;
    int a[1000];
    int b[1000];
    for (int i = 0; i < t; i++) {
        cin >> n;  //TAKING IN THE NUMBER OF ELEMENTS.
        cin >> k;  // TAKING IN AMOUNT OF ROTATION.
        for (int j = 0; j < n; j++) {
            cin >> a[j];  //READING ARRAY.
            cout << " ";
        }
        for (int y = 0; y < n; y++) {
            b[y + k] = a[y];  // REARRANGING ARRAY.
            if (y + k >= n) {
                b[s] = a[y];
                s++;
            }
            cout << b[y] << " "; // SHOWING ARRAY.
        }
    }
    return 0;
}

A:

The Problem with your code is in line,
cout<<b[y]<<" "; // SHOWING ARRAY.

You must take it out of that for loop,since you are trying to print something that you have not given value.For example,first y=0 you set b[y+2] but you print b[y] which is not yet set.
Another problem which you will see for t>1  is initialisation of s has been done out of main i.e it is 0 for only first run.
the final code will be
#include
using namespace std;
int main()
{

int t;
cin>>t;
int n;
int k;

int a[1000];
int b[1000];
for(int i=0;i<t;i++)
{
    int s=0; //Changed
    cin>>n;  
    cin>>k;  
for(int j=0;j<n;j++)
    {
  cin>>a[j]; 
 // cout<<" ";  //Not needed
    }
for(int y=0;y<n;y++)
 {
   b[y+k]=a[y];  
   if(y+k>=n)
   {
   b[s]=a[y];
   s++;
   }
//cout<<b[y]<<" "; // changed this
 }
 for(int y=0;y<n;y++)         //added this
 cout<<b[y]<<" ";
}
return 0;
}

