c - 我如何为该算法编写基本案例?

标签 c arrays recursion

我在算法方面遇到了麻烦:我需要做的是通过递归将数组的元素相乘。这是我到目前为止所得到的:

 #include <stdio.h>
    void vector(int vec[],int tam) {
    // gets a simple array
        int i;
        for (i=0; i<tam; i++) {
            printf("Ingrese un numero: ");
            scanf("%d",&vec[i]);
        }
    }
    int mv(int vec[], int tam, int pos) {
     // need help with the base and general case!
        if (tam==0) {
            return 1;
        } else {
            return vec[pos]*mv(vec,tam,pos+1);
        }   
    }

    int main() {
        int vec[3];
        vector(vec,3);
        printf("%d",mv(vec,3,0));
    }

最佳答案

基本情况是if (pos == tam) return 1;

您还必须确保数组不为空

关于c - 我如何为该算法编写基本案例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12208947/

相关文章:

c++ - 如何在 OpenCV 中将矩阵转换为 InpuArrayOfArrays?

c - 这是返回二维数组的有效方法吗?

Java递归如何计算每个数字的和

java - 在 JSP 页面上显示树

c - 在 Lua 中从 C 注册函数时如何将模块嵌套到命名空间中?

c - C 中的预期表达式

关闭标准输入

pthread_mutex_lock() 能否在 pthread_cond_wait() 解除阻塞之前解除阻塞?

JavaScript 嵌套数组

R - 如何加速递归和双重求和