copy array in C (recursion)

2010-04-24 7:48 am
我有條問題唔係好識點做??
例如個array係 int arrayA={1,2,4,5},
我想copy去另一個array(例如arrayB),用recursion
我可以點做??
個function係有三個parameter
int copyarry(int A, int B, int len)
PS. copy次序要係順序, 我試過係copy左去B, 但係係倒序左
thx!!
更新1:

My question translated to English I have a question about using recursion copy the array to another array For instance the arrayA(5) and arrayB(5) The content of ArrayA is arrayA={1,2,4,5} I want to copy the content of arrayA to arrayB

更新2:

But I only can do the result of in reverse order For instance, arrayB={5,4,3,2,1} The function named copyarray (int A, int B, int len), int A, int b and int len are the paremeters for this function plz help!!

回答 (3)

2010-05-03 4:22 pm
✔ 最佳答案
#include
void copy_array( int a[], int b[], int len )
{
if ( len > 0 ) {
b[len-1] = a[len-1];
copy_array( a, b, --len );
}
}
main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
int b[10];
int i;
copy_array( a, b, 10 );
for ( i=0; i < 10; i++ ) {
printf("%d ", a[i]);
}
printf("\\n");
}

2010-05-03 08:22:19 補充:
Please see the comment section for the program.
2010-05-03 10:53 am
Reply to bean_bottom_2

Your answer provided can solve my problem
Please post you answer to answer page to gain 20 points
I will choose your answer as the best answer!!
2010-04-26 2:53 am
I can not read HongKongese Chinese.
Can you state the detail in English?

It seems that it is easy unless I miss the detail of your spec.

void copyArray(int *A, int *B, int len)
{ if (len)
{ *B = *A;
copyArray(++A, ++B, --len)
}
}

Something like this.
I do NOT check it because the detail is unknow


收錄日期: 2021-05-02 10:11:49
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20100423000051KK01781

檢視 Wayback Machine 備份