診ていただきありがとうございます。
#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
#include <stdio.h> #include <math.h> float f(float x){ f(x) = x - cos(x); return f(x) ; } float df(float x){ df(x) = 1.0 + sin(x); return df(x); } int main(){ float f(x); float df(x); float x; int i; 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; }}
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){
のようなエラーが出てしまいました
まだ回答がついていません
会員登録して回答してみよう