gpt4 book ai didi

c - 这个求数组乘积的 C 函数正确吗?

转载 作者:行者123 更新时间:2023-11-30 20:25:54 25 4
gpt4 key购买 nike

我是 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;
}

最佳答案

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

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;
}

关于c - 这个求数组乘积的 C 函数正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27335282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com