This is the program and I have 5 errors that I can't seem to get rid of, it was a Fortran 95 program that I had to translate into C++. could somone please help me.

``````#include <iostream>
using namespace std;

int main()
{
//Declaration and Intialization
float XL = 0.0;
float XR = 0.0;
float XS = 0.0;
float X  = 0.0;
float Y  = 0.0;
float YL = 0.0;
float YR = 0.0;
float XB = 0.0;
float XND = 0.0;
float STEPSIZE = 0.0;
float F;

cout<<"What is the left-most interval endpoint?";
cin>>XB;

cout<<"What is the right-most interval endpoint?";
cin>>XND;

cout<<"What is the desired step-size for function evaluation?";
cin>>STEPSIZE;

XL = XB;

do

YL = F(XL);

XR = XL + STEPSIZE;

YR = F(XR);

if(YL*YR > 0)
{
if(XR >= XND) break;
{

XL = XR;

YL = YR;}
}

if (YL*YR == 0)
{
if (YL == 0.0)
{
cout<<"This is a root.";
cout<<XL;
}
if (XR >= XND) break;

XL = XR;
}
else
{
cout<<"This is a root.";
cout<<XR;

if (XR >=XND) break;

XL = XR + STEPSIZE;
}

if (YL*YR < 0)
{
XS =XR;
do
{
X=(XL + XR)/2;
Y = F(X);
if (YL*Y > 0)
{
XL=X;
YL=Y;
}

if (YL*Y == 0)
{
cout<<"This is a root.";
cout<<X;

XR = X;

YR=Y;

}

if (YL*Y < 0)
{
XR = X;

YR = Y;

}

if (XR - XL < 0.00005)
{
cout<<"This interval contains a root.";
cout<< XL,XR;

XL = XS;
}
if (XL >= XND) break;

}

while (XL<XND);

float F;

F = (X*X*X*X - 9*X*X*X - 2*X*X + 120*X - 130);

return F;

}``````

The errors are:
F:\C ++ Numerical Methods\RF_BETTER.CPP(33) : error C2064: term does not evaluate to a function
F:\C ++ Numerical Methods\RF_BETTER.CPP(35) : error C2061: syntax error : identifier 'XR'
F:\C ++ Numerical Methods\RF_BETTER.CPP(37) : error C2064: term does not evaluate to a function
F:\C ++ Numerical Methods\RF_BETTER.CPP(77) : error C2064: term does not evaluate to a function
F:\C ++ Numerical Methods\RF_BETTER.CPP(127) : fatal error C1004: unexpected end of file found
Error executing cl.exe.

RF_BETTER.OBJ - 5 error(s), 0 warning(s)

2
Contributors
5
Replies
6
Views
10 Years
Discussion Span
Last Post by mzdiva041986

Yea...post the Fortran code so that I have a point of reference for what you were trying to do.

This is the Fortran program:

program rf_better
implicit none

real::X,XL,XR,XS,Y,YL,YR
real::XB,XND,STEPSIZE

XL = 0.0
XR = 0.0
XS = 0.0

X = 0.0
Y = 0.0
YL = 0.0
YR = 0.0

XB = 0.0
XND = 0.0
STEPSIZE = 0.0

write(*,*)"What is the left-most interval endpoint?"

write(*,*)"What is the right-most interval endpoint?"

write(*,*)"What is the desired step-size for function evaluation?"

XL = XB

outer:do

YL = F(XL)

XR = XL + STEPSIZE

YR = F(XR)

if(YL*YR > 0)then

if(XR >= XND) exit outer

XL = XR

YL = YR
end if

if (YL*YR == 0) then

if (YL == 0.0)then

write(*,*)"This is a root."

write(*,*) XL

if (XR >= XND) exit outer

XL = XR

else

write(*,*)"This is a root."
write(*,*) XR

if (XR >=XND) exit outer

XL = XR + STEPSIZE
end if
end if

if (YL*YR < 0)then

XS =XR
inner:do

X=(XL + XR)/2
Y = F(X)
if (YL*Y > 0)then

XL=X
YL=Y
end if

if (YL*Y == 0)then

write(*,*)"This is a root."
write(*,*)X

XR = X

YR=Y

end if

if (YL*Y < 0) then

XR = X

YR = Y

end if

if (XR - XL < 0.00005) then

write(*,*)"This interval contains a root."
write(*,*) XL,XR

XL = XS

if (XL >= XND) exit outer

exit inner
end if
end do inner
end if
end do outer

contains

function F(X)

real::F
real,intent(in)::X

F=X**4-9*X**3 - 2*X**2 + 120*X -130

end function F
end program rf_better

I posted the Fortran code

>I posted the Fortran code
Really, I hadn't noticed. :icon_rolleyes: It's lunchtime where I am, so I was finishing my sandwich before I replied. Please don't bump your threads if you don't think your question is answered fast enough.

This compiles, but I didn't run it.

``````#include <cmath>
#include <iostream>

double F ( double X )
{
// X**4-9*X**3 - 2*X**2 + 120*X -130
return std::pow ( X, 4.0 ) - 9
* std::pow ( X, 3.0 ) - 2
* std::pow ( X, 2.0 ) + 120 * X - 130;
}

int main()
{
double X, XL, XR, XS, Y, YL, YR;
double XB, XND, STEPSIZE;

XL = 0.0;
XR = 0.0;
XS = 0.0;

X = 0.0;
Y = 0.0;
YL = 0.0;
YR = 0.0;

XB = 0.0;
XND = 0.0;
STEPSIZE = 0.0;

std::cout<<"What is the left-most interval endpoint? ";
std::cin>> XB;

std::cout<<"What is the right-most interval endpoint? ";
std::cin>> XND;

std::cout<<"What is the desired step-size for function evaluation? ";
std::cin>> STEPSIZE;

XL = XB;

while ( true ) {
YL = F ( XL );
XR = XL + STEPSIZE;
YR = F ( XR );

if ( YL * YR > 0 ) {
if ( XR >= XND ) goto end;

XL = XR;
YL = YR;
}

if ( YL * YR == 0 ) {
if ( YL == 0.0 ) {
std::cout<<"This is a root."<< XL;

if ( XR >= XND ) goto end;

XL = XR;
}
else {
std::cout<<"This is a root."<< XR;

if ( XR >= XND ) goto end;

XL = XR + STEPSIZE;
}
}

if ( YL * YR < 0 ) {
XS = XR;

while ( true ) {
X = ( XL + XR ) / 2;
Y = F ( X );

if ( YL * Y > 0 ) {
XL = X;
YL = Y;
}

if ( YL * Y == 0 ) {
std::cout<<"This is a root."<< X;

XR = X;
YR = Y;
}

if ( YL * Y < 0 ) {
XR = X;
YR = Y;
}

if ( XR - XL < 0.00005 ) {
std::cout<<"This interval contains a root."<< XL <<','<< XR;

XL = XS;

if ( XL >= XND ) goto end;

break;
}
}
}
}

end:
return 0;
}``````

thanks so much im not very good in C++ but i do try. Thanks again

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.