請編寫函數(shù)fun(),該函數(shù)的功能是:移動一維數(shù)組中的內(nèi)容,若數(shù)組中有n個整數(shù),要求把下標從p~n-1(p<
請編寫函數(shù)fun(),該函數(shù)的功能是:移動一維數(shù)組中的內(nèi)容,若數(shù)組中有n個整數(shù),要求把下標從p~n-1(p<n-1)的數(shù)組元素平移到數(shù)組的前面。
例如,一維數(shù)組中的原始內(nèi)容為1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, P的值為6。移動后,一維數(shù)組的內(nèi)容應(yīng)為7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6。
[注意] 部分源程序給出如下。
請勿改動主函數(shù)main和其他函數(shù)中的任何內(nèi)容,僅在函數(shù)fun的花括號中填入所編寫的若干語句。
[試題源程序]
inciude <stdio.h>
define N 80
void fun(int *w, int p, int n)
{
}
main()
{
int a[N]=(i, 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");
}
正確答案:void fun(int *w int p int n) { int i J t; for(i=p; i=n-i; i++) { t=w[n-1]; for(j=n-2; j>=0; j--) w[j+1]=w[j]; w[0]=t; } }void fun(int *w, int p, int n) { int i, J, t; for(i=p; i=n-i; i++) { t=w[n-1]; for(j=n-2; j>=0; j--)
詞條內(nèi)容僅供參考,如果您需要解決具體問題
(尤其在法律、醫(yī)學等領(lǐng)域),建議您咨詢相關(guān)領(lǐng)域?qū)I(yè)人士。