reset password
Author Message
ckolodin
Posts: 6
Posted 14:04 Apr 02, 2009 |

I have been able to write a program to solve the quadratic equation for input like:

a=10 b=100 c=200 and a=1 b=2 c=5

but for input like a=1.29 b=3.5 and c=4.6 I get innaccurate solutions.

for example instead of -1.36 + 1.31i I output -2.2575000000000003 + 2.1859696132380244i to the console

here's what I'm using right now:

double discriminant = ((Math.pow(b, 2)) -(4 * a * c));

 if (discriminant < 0) {
            discriminant *= -1;
            secondSolnString = "" + " " + (-1*b / 2 * a) + " - "
                    + (Math.sqrt(discriminant) / 2 * a) + 'i';

}

 

any suggestions or hints are welcome!

mhajianpour
Posts: 20
Posted 19:59 Apr 02, 2009 |

Chris,
Remember the order of precendence when you're doing calculations in any language. Multiplication and division are both executed at the same level and are executed from left to right if not paranthesized. Check that in your code.

--
Mahan H.

ckolodin
Posts: 6
Posted 21:30 Apr 02, 2009 |
mhajianpour wrote:

Chris,
Remember the order of precendence when you're doing calculations in any language. Multiplication and division are both executed at the same level and are executed from left to right if not paranthesized. Check that in your code.

--
Mahan H.

Thanks! I made the correction and it works now. I can't believe I didn't catch that earlier.

calikidd
Posts: 41
Posted 10:35 Apr 03, 2009 |

For the second problem, I am trying to figure out after I get right most digits, and mult. them by the base... how do I get the sum.

so far, for binary I got this:

    Scanner keyboard = new Scanner(System.in);

        input = keyboard.nextInt();
       
        int power = 0;
        int base = 2;
       
        if(base == 2){
            base = (int) Math.pow(2,power++);

            for(int i = input % 2; i > 0; i++ ){
               
                input *= base;
           
                System.out.println( "The binary representation is:\t" + input);

            }
        }

 

Any suggestions...

 

 

Last edited by calikidd at 10:52 Apr 03, 2009.
ckolodin
Posts: 6
Posted 11:52 Apr 03, 2009 |

    Scanner keyboard = new Scanner(System.in);

        input = keyboard.nextInt();
       
        int power = 0;
        int base = 2;
       
        if(base == 2){
            base = (int) Math.pow(2,power++);

            for(int i = input % 2; i > 0; i++ ){
               
                input *= base;
           
                System.out.println( "The binary representation is:\t" + input);

            }
        }

 

Any suggestions...

 

You definitely want to recheck the flow of this program. Lets say a person types "11" into the console. After that the program checks that the value of base is 2 and assigns the value 1 to base(because you initialized power to 0) and then increments power(power++ is the postincrement operator). Then it assigns the value (%input) to i. If the variable i is an even number the loop never executes if the variable is odd it executes forever.


here is some pseudocode that may be of use to to:
read input from keyboard as a string (use the nextLine() method of the scanner class)
iterate through the string
at each index raise the base to the appropriate power and add that value to an integer variable
print the integer variable
hope that helps!

Last edited by ckolodin at 12:04 Apr 03, 2009.
mhajianpour
Posts: 20
Posted 17:54 Apr 03, 2009 |

Chris is right. You need to make sure the execution of your for loop is adequate for all inputs. There are cases where you current situation will not work correctly. Think about starting your loop either from 0 or the length of your input String. Either way will work fine depending on how you want to do this.

If you have your formula written on paper, applying the formula programmatically shouldn't be that difficult taking into consideration your understanding of loops.

Let me know if you need more help.

--
Mahan Hajianpour

calikidd
Posts: 41
Posted 23:16 Apr 03, 2009 |

Ya I am not sure if I am doing it right.

I can get it to reprint the input String using the for loop. But I am not sure if anything is suppose to go inside the for loop block of code. On paper it makes sense, but coding is different...

I am to suppose to parse the String into a integer right?Because thats what I did and tried putting it into a loop.

Last edited by calikidd at 23:41 Apr 03, 2009.