Hi everyone, I'm working on this code but I get an error. could someone help me how to fix it? I also put 0 after the return but I don't get any answer.
#define _USE_MATH_DEFINES
usingnamespace std;
#include <cmath>
#include <vector>
#include <stdlib.h>
#include <iostream>
#include <functional>
#include <math.h>
#include <stdio.h>
int main()
{
float mu;
int N;
double NF;
double phi;
double Force;
double a;
double b;
double c;
double tol;
double fa;
double fb;
double fc;
mu = .5;
N = 1;
phi = M_PI / 6;
Force = .448018475;
NF = .672027713;
a = 1.18;
b = 1.34;
c = (a + b) / 2;
tol = 10 ^ -4;
fa = NF * cos(a) + mu * NF * sin(a) - .5;
fb = NF * cos(b) + mu * NF * sin(b) - .5;
fc = NF * cos(c) + mu * NF * sin(c) - .5;
while (tol > abs(fc)) {
fa = NF * cos(a) + mu * NF * sin(a) - .5;
fb = NF * cos(b) + mu * NF * sin(b) - .5;
fc = NF * cos(c) + mu * NF * sin(c) - .5;
if ((fa * fc) < 0)
b = c;
else
a = c;
return c = (a + b) / 2;
cout << fc << endl;
}
return 0;
};
Third, don't mix your C and C++ header files.
#include <cmath> and #include <math.h> is entirely pointless.
Fourth, the return 0 thing has NOTHING to do with the fact that the rest of your program doesn't produce output.
> return c = (a + b) / 2;
This return statement will always prevent the cout line on the next line from being seen.
Fifth, compile with more warnings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$ g++ -Wall -Wextra foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:17:7: warning: variable ‘N’ set but not used [-Wunused-but-set-variable]
int N;
^
foo.cpp:19:10: warning: variable ‘phi’ set but not used [-Wunused-but-set-variable]
double phi;
^
foo.cpp:20:10: warning: variable ‘Force’ set but not used [-Wunused-but-set-variable]
double Force;
^
foo.cpp:26:10: warning: variable ‘fb’ set but not used [-Wunused-but-set-variable]
double fb;
^
It's for you to decide whether you actually intended to use those variables, or whether they're just useless in this program. Use them, or delete them.