从键盘输入 8个数,存入一维整形数组中,使前面各数顺序向后移动 了个位置,最后 3个数变成前面3个数,输出调整后的数组。使用函数调用实现。
时间: 2024-05-06 16:16:10 浏览: 14
以下是使用函数调用实现该功能的示例代码:
```c++
#include <iostream>
using namespace std;
void shiftArray(int arr[], int n) {
int temp = arr[n-1];
for (int i = n-1; i > 0; i--) {
arr[i] = arr[i-1];
}
arr[0] = temp;
}
int main() {
const int SIZE = 8;
int arr[SIZE];
// 输入8个数
cout << "请输入8个整数:";
for (int i = 0; i < SIZE; i++) {
cin >> arr[i];
}
// 移动数组
for (int i = 0; i < 3; i++) {
shiftArray(arr, SIZE);
}
// 输出调整后的数组
cout << "调整后的数组为:";
for (int i = 0; i < SIZE; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
```
输入的8个数将存储在名为 `arr` 的一维整型数组中。`shiftArray` 函数用于将数组向右移动一位,最后3个数变成前面3个数。在主函数中,我们使用循环调用 `shiftArray` 函数来完成数组的移动。最后,我们输出调整后的数组。