The program in C++ to Rotate Elements of a given array of integers (length 4 ) in left direction and return the new array is given below:
#include <iostream>
using namespace std;
int main() {
int arr[4];
// Input the elements of the array
cout << "Hello Codeauri Family,Enter the elements of the array to rotate the elements of a given array of integers here:\n ";
for (int i = 0; i < 4; i++) {
cin >> arr[i];
}
// Rotate the elements to the left
int temp = arr[0];
for (int i = 0; i < 3; i++) {
arr[i] = arr[i+1];
}
arr[3] = temp;
// Output the new array
cout << "The new array is: ";
for (int i = 0; i < 4; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output:
Hello Codeauri Family,Enter the elements of the array to rotate the elements of a given array of integers here:
10
9
65
1
The new array is: 9 65 1 10
Pro-Tips💡
This program prompts the user to enter 4 integers as elements of an array.
It then uses a for loop to shift each element one position to the left, and assigns the first element to the last position.
Finally, it prints out the new array after rotation.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.