Friday, October 28, 2022

Rotate (shift) data on array "a" to right on 1 position

// Add to the program on C++  your fragment of code 
// which has to rotate(shift) data on array "a"   to right on 1 position.
// For example,
// array 5, 2, 8, 4
// should transform to array   4, 5, 2, 8
// Don't change the initially given code. 
//
#include <iostream>
#include <math.h>
using namespace std;
#define N 10
int main()
{   int a[N],b;
    for (int i = 0; i < N; i++) a[i] = rand() % 10;
    for (int i=0; i < N; i++) cout << a[i]<<" ";
    cout << "\n";
    //
    // Input here your code 
    //  
    // Inputted code:
    b = a[N - 1];
    for (int i = N-1; i > 0; i--) a[i] = a[i - 1];
    a[0] = b;
    //
    for (int i=0; i < N; i++) cout << a[i] << " ";
    cout << "\n";
 }


No comments:

Post a Comment

NaCl.xyz Generator with Interactive 3D Visualization

NaCl.xyz Generator with Interactive 3D Visualization NaCl.xyz Generator with Interactive 3D Visualization ...