1. Member
Join Date
Mar 2012
Posts
6
Rep Power
0

Hi Im quite new to java and programming in general and I was hoping on getting some help with this piece of code.

public static void main(String []args){
for (double xxx=-1000; xxx<1000;xxx++){
double equation = xxx*xxx*3-8*xxx+4;
}
if (equation ==0){
System.out.println(xxx);
}
}
}

Im using Netbeans ...if it matters.

2. ## Re: Kwadratic formula

That way you'll only find one root (2.0) if you're lucky; the other root (2/3) can not be found that way. Why don't you apply the close form formula (-b+-sqrt(b*b-4*a*c))/(2*a).

kind regards,

Jos

3. Member
Join Date
Mar 2012
Posts
6
Rep Power
0

## Re: Kwadratic formula

Well I wanted to find the solution without the formula to make it as basic as possible.

4. Member
Join Date
Mar 2012
Posts
6
Rep Power
0

## Re: Kwadratic formula

Is there any way of letting the xxx increment by ..lets say 0.01 instead of 1 ?

5. ## Re: Kwadratic formula

Sure, add 0.01 to variable xxx each time through the loop:

Java Code:
```for (double xxx= -1000; x < 1000; x+= 0.01)
// ...```
... but you still won't find the other root (2/3) that way.

kind regards,

Jos

6. Member
Join Date
Mar 2012
Posts
6
Rep Power
0

## Re: Kwadratic formula

Sweet ,but i still get an error :

7. ## Re: Kwadratic formula

hi,
a variable cannot be accessed outside a block
also, every instance of the variable "equation" should be checked, but u're checking it only once after the end of the loop

regards
dhilip

8. Member
Join Date
Mar 2012
Posts
6
Rep Power
0

## Re: Kwadratic formula

Uhm , in nooblanguage please. Or most preferably in code form ???

9. ## Re: Kwadratic formula

Originally Posted by BURNY26
Uhm , in nooblanguage please. Or most preferably in code form ???
Put lines #6 and #7 before line #5.

kind regards,

Jos

10. Member
Join Date
Mar 2012
Posts
6
Rep Power
0

## Re: Kwadratic formula

cheers ,case dismissed

11. ## Re: Kwadratic formula

So you're happy with this? Your program only finds solutions to your quadratic formula if the root is -1000+i*0.01. An ordinary quadratic formula isn't too difficult to implement ... it's not like a Jenkins-Traub method that works on arbitrary polynomials ...

kind regards,

Jos

12. ## Re: Kwadratic formula

Originally Posted by JosAH
Sure, add 0.01 to variable xxx each time through the loop:

Java Code:
```for (double xxx= -1000; x < 1000; x+= 0.01)
// ...```
Hi,

adding "0.01" to "double xxx=0" gives us approximate double values, not exact increment
for example,
Java Code:
```for(int xxx=0;xxx<1;xxx+=0.01)
{
System.out.println(xxx);
}```
gives output
(first 15 output values..)
Java Code:
```0.0
0.01
0.02
0.03
0.04
0.05
0.060000000000000005 //this is non-exact
0.07
0.08
0.09
0.09999999999999999//non-exact
0.10999999999999999//non-exact
0.11999999999999998//non-exact
0.12999999999999998//non-exact
0.13999999999999999//non-exact```
this is because of rounding errors
hence even "2.0" may not be displayed
I learnt it at Java theory and practice: Where's your point?

regards
dhilip
Last edited by noobplus; 03-26-2012 at 05:13 PM.

13. ## Re: Kwadratic formula

Of course you haven't read my original reply:

Originally Posted by JosAH
That way you'll only find one root (2.0) if you're lucky; the other root (2/3) can not be found that way.
Bold faced font added for clarity.

Jos

14. ## Re: Kwadratic formula

Originally Posted by JosAH
Of course you haven't read my original reply:

Bold faced font added for clarity.

Jos
ok boss

15. ## Re: Kwadratic formula

Originally Posted by JosAH
Of course you haven't read my original reply:

Bold faced font added for clarity.

Jos
Java Code:
```/**
*@author:dhilipkumar(noobplus)
*/

public class kwadratic {
static double equation;
static double incrementedvalue;
static double repeatchecker=-1;

public static void main(String []args){

for(int x=0;x<11;x++)
{

for(int i=1;i<11;i++)
{
for(int j=0;j<10;j++)
{
if(j<i)
{
incrementedvalue=(x+(double)j/(double)i);

equation = (incrementedvalue*incrementedvalue*3)-(8*incrementedvalue)+4;

if ((equation==0) && (incrementedvalue!=repeatchecker))
{

System.out.println(incrementedvalue);
repeatchecker=incrementedvalue;
}
}
}

}

}

}
}```
output:
Java Code:
```0.6666666666666666
2.0```
we don't need to use "xxx+=0.01"

regards
dhilip
Last edited by noobplus; 03-26-2012 at 02:40 PM.

16. ## Re: Kwadratic formula

Originally Posted by noobplus
we don't need to use "xxx+=0.01"
We don't need any iterations at all.

Jos

17. ## Re: Kwadratic formula

Originally Posted by JosAH
We don't need any iterations at all.

Jos
and how?

dhilip

18. ## Re: Kwadratic formula

Originally Posted by noobplus
and how?

dhilip
So you still haven't read this thread ...

Jos

19. ## Re: Kwadratic formula

"Kwadratic" is my new favorite word.

20. ## Re: Kwadratic formula

Originally Posted by JosAH
So you still haven't read this thread ...

Jos
yep..basic quadratic formula .. yep..
Originally Posted by BURNY26
Well I wanted to find the solution without the formula to make it as basic as possible.
maybe he wanted to become a loop-master

regards
dhilip
Last edited by noobplus; 03-26-2012 at 03:26 PM.

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•