Computing.Net > Forums > Programming > Dev-C++ error

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Dev-C++ error

Reply to Message Icon

Name: ktx22
Date: October 2, 2005 at 15:43:03 Pacific
OS: XP
CPU/Ram: 1.1ghz athon 1024mb
Comment:

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 bound

golden_search( f, atof(argv[2]), atof(argv[3]), atoi(argv[1]), 0.001 );
}



Sponsored Link
Ads by Google

Response Number 1
Name: Gargamel
Date: October 6, 2005 at 15:56:02 Pacific
Reply:

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.


0
Reply to Message Icon

Related Posts

See More


Can someone access my fil... Simple java help



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Dev-C++ error

Compiling Errors in Dev C++ www.computing.net/answers/programming/compiling-errors-in-dev-c-/9484.html

Dev C++ help www.computing.net/answers/programming/dev-c-help/12264.html

bloodshed c++ error @ compile www.computing.net/answers/programming/bloodshed-c-error-compile/1591.html