hey I know its not my personal request website but I have to hand it in tomorrow and I dont know wtf is wrong, its probably a shitty mistake any tips?
it's for a calculus II project and I dont want to repeat this course, my grade relies for 15% on this please help
integral must return a value is my error
if it helps;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
int count;
float total, inBox;
// user defined function below
float f(float x) {
return exp(cos(x));
}
//function to calculate a definite integral given bounds of integration (xmin/max) & bounds of function (ymin/ymax)
float integral(float(*f)(float), float xmin, float xmax, float ymin, float ymax) {
for (count = 0; count < 1000000; count++) {
float u1 = (float)rand() / (float)RAND_MAX;
float u2 = (float)rand() / (float)RAND_MAX;
float xcoord = ((xmax - xmin)*u1) + xmin;
float ycoord = ((ymax - ymin)*u2) + ymin;
float val = f(xcoord);
total++;
if (val > ycoord) {
inBox++;}
}
float density = inBox / total;
std::cout << (xmax - xmin)*(ymax - ymin)*density << std::endl;
}
int main() {
std::cout << "RESULT: " << std::endl;
integral(f, -2, 2, 0, 4);
}
>>61205773
1. Retard. Use tags not screen shots.
2. Retard. We're not here to do your homework for you. Fuck off, poojeet.
3. Use C++11 or kill yourself you absolute fucking imbecile.
>>61205803
a poojeet would have solved this by now mate you kw that,Im already in the shitter as of takin calculus in the summer semester
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
int count;
float total, inBox;
// user defined function below
float f(float x) {
return exp(cos(x));
}
//function to calculate a definite integral given bounds of integration (xmin/max) & bounds of function (ymin/ymax)
float integral(float(*f)(float), float xmin, float xmax, float ymin, float ymax) {
for (count = 0; count < 1000000; count++) {
float u1 = (float)rand() / (float)RAND_MAX;
float u2 = (float)rand() / (float)RAND_MAX;
float xcoord = ((xmax - xmin)*u1) + xmin;
float ycoord = ((ymax - ymin)*u2) + ymin;
float val = f(xcoord);
total++;
if (val > ycoord) {
inBox++;}
}
float density = inBox / total;
return (xmax - xmin)*(ymax - ymin)*density;
}
int main() {
std::cout << "RESULT: " << integral(f, -2, 2, 0, 4) << std::endl;
}
>>61205819
what was the mistake? could you explain?
May God bless you kind sir xD
>C style casts
kys
y
s
>>61205732
I didn't post the solution, but you're not returning a value in integral().
>>61205834
You defined that function integral should return float value, but you actually never returned any value..
>>61205732
>global variables
I'd fail you from the course just for that alone.
>passing a function pointer instead of just using a std lambda
>not using auto
>c-style casting instead of static_cast
>cout inside a math function
Don't help him. We don't want or need any more poojeets or dumb fucks shitting up the profession.
>>61205732
>too stupid to read the compiler errors