診ていただきありがとうございます。
#include <stdio.h> #include <math.h> int main(){ float f(); float df(); float x; int i; f(x) = x - cos(x); df(x) = 1.0 + sin(x); x = 1.0; for (i = 1; i <= 100;i++){; x = x-f(x)/df(x); if(fabs(f(x)) <= exp(-6)); printf("x=%f.\n",x); break; }}
x-cos(x)=0をニュートン法でとくプログラムを作ろうとしています。
これを実行したところ、
error: lvalue required as left operand of assignment
f(x) = x - cos(x);
error: lvalue required as left operand of assignment
df(x) = 1.0 + sin(x);
のエラーが表示されています。どのように直したらよいのかわからないです。
追記
main 外に書けばいいとの指摘をいただいたので書き直したところ
C
1#include <stdio.h> 2#include <math.h> 3 4 float f(float x){ 5 f(x) = x - cos(x); 6 return f(x) ; 7 } 8 9 float df(float x){ 10 df(x) = 1.0 + sin(x); 11 return df(x); 12 } 13 14 15int main(){ 16 float f(x); 17 float df(x); 18 float x; 19 int i; 20 21 x = 1.0; 22 for (i = 1; i <= 100;i++){; 23 x = x-f(x)/df(x); 24 if(fabs(f(x)) <= exp(-6)); 25 printf("x=%f.\n",x); 26 break; 27 }} 28 29 30
kadai11_1.c: In function ‘f’: kadai11_1.c:5:14: error: lvalue required as left operand of assignment f(x) = x - cos(x); ^ kadai11_1.c: In function ‘df’: kadai11_1.c:10:15: error: lvalue required as left operand of assignment df(x) = 1.0 + sin(x); ^ kadai11_1.c: In function ‘main’: kadai11_1.c:16:5: warning: parameter names (without types) in function declaration float f(x); ^~~~~ kadai11_1.c:16:11: error: conflicting types for ‘f’ float f(x); ^ kadai11_1.c:16:5: note: an argument type that has a default promotion can’t match an empty parameter name list declaration float f(x); ^~~~~ kadai11_1.c:4:11: note: previous definition of ‘f’ was here float f(float x){ ^ kadai11_1.c:17:5: warning: parameter names (without types) in function declaration float df(x); ^~~~~ kadai11_1.c:17:11: error: conflicting types for ‘df’ float df(x); ^~ kadai11_1.c:17:5: note: an argument type that has a default promotion can’t match an empty parameter name list declaration float df(x); ^~~~~ kadai11_1.c:9:11: note: previous definition of ‘df’ was here float df(float x){
のようなエラーが出てしまいました

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。