这个C函数用于查找数组的乘积是否正确?

时间:2014-12-06 19:01:55

标签: c

我是C的新手并且有一个关于创建一个简单函数的问题,其中我交给一个给定的数组,还有一个整数来表示该数组中的数字。我写了这段代码,但我不确定它是否正确。我试图做的是让它能够找到数组中所有数字的乘积。

#include <stdio.h>
#include <math.h>
double my_product (int n, double x[]);

int main (void)
{
    my_product(n, x);
    return 0;
}

double my_product (int n, double x[])
{
    int i;
    product=0;
    for(i=0; i<n; i++)
    {
        product=x[i]*x[i+1]
    }
    return product;
}

2 个答案:

答案 0 :(得分:1)

我会评论你的代码,指出你的错误:

double my_product (int n, double x[])
{
    int i;

    product=0;
    /* The variable "product" needs to have a type.  
       In your case, since your values have type "double",
       and a "double" return is expected, 
       of course you need to declare:

       double product;

       On the other hand, 
       it has not sense to initialize a product to 0,
       since multiplication by 0 "kills" every value,
       giving you a value 0, not matter what you do.
       So, initialize in this way:

       double product = 1.0;

    */

    for(i=0; i<n; i++)
    {
        /* A semicolon is missing in the next line: */
        product=x[i]*x[i+1]

        /* In every step of the loop,
           the variable "product" will hold the value
           given by the multiplication of two consecutive values 
           in the array.
           Thus, you lose every previous factor.
           Also, when i == n you are in trouble, 
           because x[i] == x[n] is beyond the limits of the array,
           which may cause access violation to memory.

           You need a cumulative product.
           Starting by the initialized value 1.0,
           you have to multiply the previous value of "product" 
           with the present value of the array: x[i]

           product = product * x[i];

           Thus, in the step i, it can be proved that
           the variable "product" contains the cumulative product
           of the factors x[0], x[1], ... up to x[i].

           A more compact notation in C can be provided by the *= operator:

           product *= x[i];

           */
    }
    return product;
}

在函数main()中:

int main (void) {          
    my_product(n, x);
    /* The function "my_product()" has been called with 
       undeclared parameters "n" and "x".

       First, you have to declare and define the value of "n",
       as much as the array "x", having type double:

       double x[] = {3.0, 1.41, -2.3, 9.4, };
       int n = sizeof(x)/sizeof(double);

       The first line would declare an array "x" having type "double",
       and initialized to hold 4 values, as it's seen there.
       The second line would declare an "int" variable "n" 
       holding the number of elements in the array,
       which can be computed as the size of x (measured in bytes)
       divided by the size of the type "double"
       (of course, in this example we have n == 4).

       Finally, you need to do something with the result returned by
       the function "my_product()".
       If not, the returned value will be completely lost.
       For example, to hold it in a variable, or to show it on screen.

       double ans;
       ans = my_product(n, x);
    */
    return 0; 
}

代码如下所示:

#include <stdio.h>
#include <math.h>
double my_product (int n, double x[]);

int main (void)
{
    double x[] = {3.0, 1.41, -2.3, 9.4, };
    int n = sizeof(x)/sizeof(double);
    double ans;
    ans = my_product(n, x);
    printf("Product is: %f\n", ans);
    return 0;
}

double my_product (int n, double x[])
{
    product=1.0;
    int i;
    for(int i=0; i<n; i++)
    {
        product *= x[i];
    }
    return product;
}

答案 1 :(得分:0)

在您的函数my_product中,您将覆盖循环中product的值

for(i=0; i<n; i++)
{
    product=x[i]*x[i+1]
}

假设你的数组x = {2, 3, 4},然后第一个product获得2 * 3的值,但是然后用3 * 4的值覆盖它。你可能想要的是使用一个变量来累加乘法的结果(例如total = total * number)。

另外,请注意索引。在您当前的代码i+1中,x可能会大于{{1}}中的元素数量,因此您将耗尽数组。