上机问题~!
题干:函数fun的功能是:移动一维数组中的内容,若数组中有n个整数,要求把下标为0到p(含p,p<n-1)的数组元素平移到数组的最后。如一维数组的原始内容为:1,2,3,4,5,6,7,8,9,10,若p为2,移动后则为4,5,6,7,8,9,10,1,2,3。
我的做法是:
#include <stdio.h>
#define N 80
void fun(int *w, int p, int n)
{int i,a[N],j,k;
for(i=0;i<=p;i++)
a[i]=w[i];
a[i]='\0';
for(i=p+1,j=0;j<=n-p;i++,j++)
w[j]=w[i];
w[j]='\0';
for(i=0;i<=p;i++,j++)
w[j]=a[i];
}
main()
{
int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int i,p,n=15;
printf("The original data:\n");
for(i=0; i<n; i++)printf("%3d",a[i]);
printf("\n\nEnter p: ");scanf("%d",&p);
fun(a,p,n);
printf("\nThe data after moving:\n");
for(i=0; i<n; i++)printf("%3d",a[i]);
printf("\n\n");
}
输入2,运行后为
4,5,6,7,8,9,10,11,12,13,14,15,0,0,1
请问哪出错了~用我这方法应该怎么改?谢谢