请编写函数fun,该函数的功能是:移动一维数组中的内容;若数组中有m个整数,要求把下标从0到t(tsm-1)的数组元素平移到数组的最后。 例如,一维数组中的原始内容为i:1,2,3,4,5,6,7,8,9,10;当t的值为3时,移动后,一维数组中的内容应为3 5,6,7,8,9,10,1,2,34 仅在函数fun的花括号中Begiin和End之间写代码,请勿改动主函数main与程序中其他的任何内容。程序运行样例:输入:3 输出:5,6,7,8,9,10,1,2,34,用c语言
时间: 2023-08-21 08:02:39 浏览: 37
以下是函数fun的代码实现:
```c
void fun(int arr[], int m, int t) {
int i, j;
int temp;
// 将下标0到t的元素移到末尾
for (i = 0; i < t; i++) {
temp = arr[i];
for (j = i; j < m - 1; j++) {
arr[j] = arr[j + 1];
}
arr[m - 1] = temp;
}
}
```
以上代码中,首先使用两个循环,将下标0到t的元素依次移到数组的末尾,其中外层循环控制循环次数,内层循环将当前元素之后的所有元素向前移动一位,最后将当前元素赋值给数组的最后一个元素即可。
在主函数中,可以先定义一个包含10个元素的数组,并初始化为1到10的整数。然后调用函数fun将数组中下标0到2的元素移到末尾,并输出移动后的数组元素,代码如下:
```c
#include <stdio.h>
void fun(int arr[], int m, int t);
int main() {
int arr[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int m = 10; // 数组元素个数
int t = 3; // 下标范围
fun(arr, m, t);
// 输出移动后的数组元素
for (int i = 0; i < m; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
运行程序,输出结果为:`4 5 6 7 8 9 10 1 2 3`。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)