can anybody help on this easy C question?

wviperw

Senior member
Aug 5, 2000
824
0
76
I am trying to make a short program to give me the answer to a 1-variable equation. This should be real easy but I can't get it to work. I don't know C too well though. Maybe my syntax is wrong. The problem I am trying it with is x^3 = x + 1. Sure I could figure it out another way by, but thats not any fun. :)

CODE:

#include <stdio.h>

int main()
{
float x;
x = x^3 - 1;

printf(&quot;answer is %f\n&quot;, x);
}

 

mosdef

Banned
May 14, 2000
2,253
0
0
I don't use C, I use C++, and I haven't worked with either for about a year, but I think your problem is that ^ isn't an operator. I think you have to use the pow or power function. Or just do x*x*x.

-mosdef
 

mosdef

Banned
May 14, 2000
2,253
0
0
Also, are you trying to find out which values satisfy that equation? I'm not sure what you're trying to accomplish but I think you have to set up a for loop that goes through an appropriate range of values. Or just factor the equation... 0=x^3-x-1.

-mosdef
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
First, the single = is the assignment operator. Second, ^ doesn't mean anything. You'd have to specify a range of values to find values that'll satsify your equation. Try this:

#include <stdio.h>

int main()
{
int i, x;

/* search between -5 and 5 inclusive */
for( i=-5; i<=5 ;i++ )
{
if (i*i*i==i)
x=i;
}

printf(&quot;answer is %f\n&quot;, x);

return 0;
}


The only problem with this program is that it'll find the largest value that satisfies the equation, which is somewhat trivial since the only solution is 1.