#include<stdio.h> int move(int m,int s[i]) { int left=x[0]; int j=0; int n=sizeof(s)/sizeof(s[0]); while(j<m) { for(i=1;i<n;i++) { s[i-1]=s[i]; s[n-1]=s[0]; } } return s[i]; } int main() { int n=0; scanf("%d",&n); int m=0; scanf("%d",&m); int arr[n]; for(int i=0;i<n;i++) { scanf("%d",&arr[i]); } int re=move(m,arr[i]); printf("%d",re); return 0; }哪里错了
时间: 2024-03-05 13:48:03 浏览: 29
代码中存在以下错误:
1. 函数`move`中的`s[i]`没有定义,需要在函数外部声明变量`i`。
2. 在`move`函数中,循环移动数组的部分有误。应该先将最后一个元素存储下来,然后将前面的元素向后移动一位,最后将存储的最后一个元素赋值给第一个位置,代码如下:
```c
int move(int m,int s[],int n)
{
int j;
while(j<m)
{
int temp = s[n-1];
for(int i=n-2;i>=0;i--)
{
s[i+1]=s[i];
}
s[0] = temp;
j++;
}
return s[0];
}
```
3. 在`main`函数中,调用`move`函数时需要传入参数`n`,表示数组`s`的长度。同时在 `printf`函数中应该输出整个数组,而不是数组中的一个元素。
修改后的代码如下:
```c
#include<stdio.h>
int move(int m,int s[],int n)
{
int j;
while(j<m)
{
int temp = s[n-1];
for(int i=n-2;i>=0;i--)
{
s[i+1]=s[i];
}
s[0] = temp;
j++;
}
return s[0];
}
int main()
{
int n=0;
scanf("%d",&n);
int m=0;
scanf("%d",&m);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int re=move(m,arr,n);
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
```
希望对您有所帮助!
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)