问题描述
用户那里得到了我数组的大小,以及我与功能MALLOC内存分配分配和获取输入,只有递归阵列,没有循环!
The user get me the size of the array, and i am allocate with function "MALLOC" the memory allocation and get input the array with recursion only, without loops!!
我不知道如何创建一个循环使用递归功能。
I do not know how to create a "loop" with recursion function.
我希望高兴获得帮助,请。
I hope happy to get help please.
感谢。
推荐答案
由于这看起来像功课,我给一个面向功课回答 - 在伪code
Because this seems like homework, I'll give a homework-oriented answer -- in pseudo-code.
define loop(array, index)
if index == 0
return array
array[index] = get input from user
loop (array, index-1)
与循环(数组,ARRAY_SIZE)
。
我想呼应垫的情绪,这是不是一个好的使用递归。
I'd like to echo Mat's sentiment that this is not a good use of recursion.
更新
既然你想使用 scanf函数(3)
来读取输入,我会就如何使用它更强的提示。以上,我写道:
Since you'd like to use scanf(3)
to read the input, I'll give a stronger hint on how to use it. Above, I wrote:
array[index] = get input from user
您可以编写具有以下 scanf函数这一行(3)
电话:
You could write this line with the following scanf(3)
call:
scanf("%d", &array[index]);
这将十进制整数存入数组[索引]
位置 - &安培;
返回地址的数组插槽,而不是评估阵列订阅。
This will store a decimal integer into the array[index]
location -- the &
returns the address of the array slot, rather than evaluating the array subscription.
这篇关于输入scanf函数阵列递归没有回路的功能(.C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!