Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello, I get my C++ code to compile but when i try to run it the program crashes.
when i try to debug it i get this error
"access viloation(segmentation raised) in your program"any one have idea what the heck is the problem..
heres my code..
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double golden_search( double (*f)(double), double xl, double xu,
int minormax, double es )
{
double R = (sqrt(5)-1)/2;
double d = R*(xu-xl);
double x1 = xl + d;
double x2 = xu - d;
double xopt;double fxl = f(xl);
double fx1 = f(x1);
double fx2 = f(x2);
double fxu = f(xu);
printf( "Iteration xl x2 x1 xu xopt fxopt ea\n" );for ( int i=0; i <= 100; i++ ) {
double fxopt;if ( (fx1 > fx2 && !minormax) || (fx1 < fx2 && minormax) ) {
xopt = x1;
fxopt = fx1;printf( " %2d %f %f %f %f %f %f", i, xl, x2, x1, xu, xopt, fxopt );
xl = x2;
fxl = fx2;
x2 = x1;
fx2 = fx1;
x1 = xl + R*(xu-xl);
fx1 = f(x1);} else {
xopt = x2;
fxopt = fx2;printf( " %2d %f %f %f %f %f %f", i, xl, x2, x1, xu, xopt, fxopt );
xu = x1;
fxu = fx1;
x1 = x2;
fx1 = fx2;
x2 = xu - R*(xu-xl);
fx2 = f(x2);
}double ea = (1-R)*fabs((xu-xl)/xopt);
printf( " %f\n", ea*100 );
if ( ea < es ) break;
}
}double f(double x)
{
// return (6*x + 7.5*x*x + 3*x*x*x + x*x*x*x );
return (2*sin(x) - x*x/10);
// double pi = 3.1415926535;
// return ( cos(3.1848261-2.3584042*x)+sin(4.7123732+2.7123809*x) );
}main( int argc, char **argv )
{
// 3 arguments
// arg1 - if arg1 is 0, find maximum, otherwise find minimum
// arg2 - lower bound
// arg3 - upper boundgolden_search( f, atof(argv[2]), atof(argv[3]), atoi(argv[1]), 0.001 );
}

Hi.
What command line arguments are you using? I tried it few times and it ran perfectly on my computer...
Nevertheless, you might want to consider the following changes (which, in my opinion, have nothing to do with your crashes...)
1. in the first line of 'golden_search', sqrt(5) caueses ambigous function call on some compilers. You might like to change it to sqrt(5.0) which will (surprisingly) give the same result but prevent the ambigouity.
2. Though you stated golden_search returns a double, it lacks any return statement. Either put one, or change the return type to 'void'.Best regards, Itai.

![]() |
Can someone access my fil...
|
Simple java help
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |