2013-11-24 86 views
1

在下面的代碼,我有一個問題與行:如何讓數組使用pow()函數? - C編程

watts = pow(storage[1],2)/storage[2]; 

我希望能夠有POW()在其計算中使用數組的指針。但是,每當我編譯Pelles c中的代碼,我得到以下錯誤:

error #2140: Type error in argument 1 to 'pow'; expected 'double' but found 'double *'.

我這麼久的代碼塊道歉,但我要的StackOverflow新手,和我剛開始學習C.任何幫助非常感謝,謝謝。

float watts = 0.0; 
float volts = 0.0; 
float amps = 0.0; 
float res = 0.0; 

int form_det = 0;     //This will be the formula determiner. 
int form_mod[3];     //This will be the formula modifier. 
double storage[10][10];    //This will be the storage for the numeric values. 
int i = 0;       //Declaration and initialization for 'for' loop. 
int count = 0;      //Each time the loop is executed, this is incremented. 

printf("\nWhat specification of your system do you wish to calculate?"); 
printf("\n1.) Watts"); 
printf("\n2.) Volts"); 
printf("\n3.) Amperes"); 
printf("\n4.) Resistance\n"); 
scanf("%d",&form_det); 

if(form_det != 0); 
{ 
    for(i = 1; i <= 3; i++) 
    { 
     if(i <= 2) 
     { 
      printf("\nWhat is your "); 
      switch(i) 
      { 
       case 1: 
        printf("first"); 
        break; 
       case 2: 
        printf("second"); 
      } 
      printf(" value to calculate? "); 
      scanf("%s",storage[i][0]); 

      printf("And what type of value is this? "); 
      printf("\n1.) Watts"); 
      printf("\n2.) Volts"); 
      printf("\n3.) Amperes"); 
      printf("\n4.) Resistance\n"); 
      scanf("%d",&form_mod[i]); 
      count++; 

     } 
     else 
     {  
      if(form_det == 1) //Watts 
      { 
       if(form_mod[1] == 2 && form_mod[2] == 4) 
       { 
        watts = pow(storage[1],2)/storage[2]; // (v^2)/r 
       } 
       else if(form_mod[1] == 4 && form_mod[2] == 2) 
       { 
        // (v^2)/r 
       } 
       else if(form_mod[1] == 4 && form_mod[2] == 3) 
       { 
        // r(i^2) 
       } 
       else if(form_mod[1] == 3 && form_mod[2] == 4) 
       { 
        // r(i^2) 
       } 
       else if(form_mod[1] == 2 && form_mod[2] == 3) 
       { 
        // v*i 
       } 
       else; 
       { 
        // v*i 
       } 
      } 
      else if(form_det == 2) //Volts 
      { 
       if(form_mod[1] == 4 && form_mod[2] == 3) 
       { 
        // r*i 
       } 
       else if(form_mod[1] == 3 && form_mod[2] == 4) 
       { 
        // r*i 
       } 
       else if(form_mod[1] == 1 && form_mod[2] == 3) 
       { 
        // p/i 
       } 
       else if(form_mod[1] == 3 && form_mod[2] == 1) 
       { 
        // p/i 
       } 
       else if(form_mod[1] == 1 && form_mod[2] == 4) 
       { 
        // (p*r)^.5 
       } 
       else; 
       { 
        // (p*r)^.5 
       } 
      } 
      else if(form_det == 3) //Amperes 
      { 
       if(form_mod[1] == 1 && form_mod[2] == 4) 
       { 
        // (p/r)^.5 
       } 
       else if(form_mod[1] == 4 && form_mod[2] == 1) 
       { 
        // (p/r)^.5 
       } 
       else if(form_mod[1] == 1 && form_mod[2] == 2) 
       { 
        // p/v 
       } 
       else if(form_mod[1] == 2 && form_mod[2] == 1) 
       { 
        // p/v 
       } 
       else if(form_mod[1] == 2 && form_mod[2] == 4) 
       { 
        // v/r 
       } 
       else; 
       { 
        // v/r 
       } 
      }   
      else; //Resistance 
      { 
       if(form_mod[1] == 2 && form_mod[2] == 3) 
       { 
        // v/i 
       } 
       else if(form_mod[1] == 3 && form_mod[2] == 2) 
       { 
        // (p/r)^.5 
       } 
       else if(form_mod[1] == 1 && form_mod[2] == 2) 
       { 
        // p/v 
       } 
       else if(form_mod[1] == 2 && form_mod[2] == 1) 
       { 
        // p/v 
       } 
       else if(form_mod[1] == 2 && form_mod[2] == 4) 
       { 
        // v/r 
       } 
       else; 
       { 
        // v/r 
       } 
      }   
     } 
    } 
} 
+0

你是什麼意思「在計算中使用數組指針」? 'pow'適用於數字,而不是指針。如果你想計算多個數字的冪,你需要在一個循環中多次調用它。 –

+0

將陣列提升爲力量意味着什麼? (您還需要修復您的scanf,並且不需要發佈整個程序,大部分程序與您的問題無關) –

回答

1
storage

是2D陣列和storage[1]不是double類型。它的類型爲double *(指向數組storage的第2行的指針)。 pow函數需要類型爲double的參數。

double pow(double b, double p) 

調用未定義行爲另一個大問題是

scanf("%s",storage[i][0]); 

storagedouble小號數組的數組。使用錯誤的說明符,即%s說明符scanf中的storage將調用未定義的行爲。

+1

謝謝,行中的%s是我的問題。此外,gthacoder的回答也很有幫助。我希望我能接受你的答案。 – Green8Ball

+0

我很高興它幫助你:) – haccks

0

該錯誤表示您使用錯誤類型的第一個參數。 storage[][]是2D數組,因此要提取任何數字,您需要使用以下表示法:storage[i][j]storage[1]是雙浮點數的數組。

顯然,你應該改變這樣說:

watts = pow(storage[1][0],2)/storage[2][0]; 

這將使從用戶輸入這兩個值正確計算。

+0

謝謝你,你的解決方案幫助和haccks'。 – Green8Ball

+0

很高興能有所幫助。 – gthacoder