2014-12-06 39 views
-2

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

回答

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; 
} 
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中的元素數量,因此您用完陣列。

相關問題