Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help with loops

knight2255knight2255 Member Posts: 4
I'm having a problem writing the proper loop statement for a problem that wants to find the square root of a number. I'm not sure if I should use a do/while, while, or a for loop. I need to approximate the square root of a number without using math.sqrt.

Problem says to repeatedly make guesses at the square root until I get closer and closer to the right guess. I store the last guess and the next guess. The next guess is computed as: (last guess + N / last guess). The first time through this process, we assume that the last guess is 1.0.


last guess = 1.0
next guess = (last guess + N / last guess)
loop as long as the absolute difference between the two guesses is greater than 0.005
save the next guess in last guess
next guess = (last guess + N / last guess)
end loop

thanx for any help

Comments

  • : I'm having a problem writing the proper loop statement for a problem that wants to find the square root of a number. I'm not sure if I should use a do/while, while, or a for loop. I need to approximate the square root of a number without using math.sqrt.
    :
    : Problem says to repeatedly make guesses at the square root until I get closer and closer to the right guess. I store the last guess and the next guess. The next guess is computed as: (last guess + N / last guess). The first time through this process, we assume that the last guess is 1.0.
    :
    :
    : last guess = 1.0
    : next guess = (last guess + N / last guess)
    : loop as long as the absolute difference between the two guesses is greater than 0.005
    : save the next guess in last guess
    : next guess = (last guess + N / last guess)
    : end loop
    :
    : thanx for any help
    :

    Your question is already answered within your question:

    ...
    [b]loop[/b] as long as the absolute difference between the two guesses is greater than 0.005
    ...
    [b]end loop[/b]

    [code]
    ...
    while (the absolute difference between the two guesses is greater than 0.005) {
    ...
    }
    [/code]

    tron.
  • knight2255knight2255 Member Posts: 4
    : : I'm having a problem writing the proper loop statement for a problem that wants to find the square root of a number. I'm not sure if I should use a do/while, while, or a for loop. I need to approximate the square root of a number without using math.sqrt.
    : :
    : : Problem says to repeatedly make guesses at the square root until I get closer and closer to the right guess. I store the last guess and the next guess. The next guess is computed as: (last guess + N / last guess). The first time through this process, we assume that the last guess is 1.0.
    : :
    : :
    : : last guess = 1.0
    : : next guess = (last guess + N / last guess)
    : : loop as long as the absolute difference between the two guesses is greater than 0.005
    : : save the next guess in last guess
    : : next guess = (last guess + N / last guess)
    : : end loop
    : :
    : : thanx for any help
    : :
    :
    : Your question is already answered within your question:
    :
    : ...
    : [b]loop[/b] as long as the absolute difference between the two guesses is greater than 0.005
    : ...
    : [b]end loop[/b]
    :
    : [code]
    : ...
    : while (the absolute difference between the two guesses is greater than 0.005) {
    : ...
    : }
    : [/code]
    :
    : tron.
    :
    So the program would werk if i did this

    while(nextGuess < .005) {
    nextGuess = 1/2 * (lastGuess + x/lastGuess)
    nextGuess = lastGuess
  • : : : I'm having a problem writing the proper loop statement for a problem that wants to find the square root of a number. I'm not sure if I should use a do/while, while, or a for loop. I need to approximate the square root of a number without using math.sqrt.
    : : :
    : : : Problem says to repeatedly make guesses at the square root until I get closer and closer to the right guess. I store the last guess and the next guess. The next guess is computed as: (last guess + N / last guess). The first time through this process, we assume that the last guess is 1.0.
    : : :
    : : :
    : : : last guess = 1.0
    : : : next guess = (last guess + N / last guess)
    : : : loop as long as the absolute difference between the two guesses is greater than 0.005
    : : : save the next guess in last guess
    : : : next guess = (last guess + N / last guess)
    : : : end loop
    : : :
    : : : thanx for any help
    : : :
    : :
    : : Your question is already answered within your question:
    : :
    : : ...
    : : [b]loop[/b] as long as the absolute difference between the two guesses is greater than 0.005
    : : ...
    : : [b]end loop[/b]
    : :
    : : [code]
    : : ...
    : : while (the absolute difference between the two guesses is greater than 0.005) {
    : : ...
    : : }
    : : [/code]
    : :
    : : tron.
    : :
    : So the program would werk if i did this
    :
    : while(nextGuess < .005) {
    : nextGuess = 1/2 * (lastGuess + x/lastGuess)
    : nextGuess = lastGuess
    :

    No :-(
    Take a closer look at the pseudo code you'd posted in your original question.

    Here are some hints:

    - the difference between the two guesses: (nextGuess-lastGuess)
    - the absolut value of that difference: Math.abs(...)
    - loop as long as this difference is greater than 0.005: ">" not "<"
    - "save the next guess in last guess"
    - and after! that calculate the new guess

    If " (last guess + N / last guess)" means "1/2 * (lastGuess + x/lastGuess)" then your loop should look like this:

    [code]
    while(Math.abs(nextGuess-lastGuess) > 0.005) {
    lastGuess = nextGuess;
    nextGuess = 1/2 * (lastGuess + x/lastGuess);
    }
    [/code]

    tron.
  • knight2255knight2255 Member Posts: 4
    : : : : I'm having a problem writing the proper loop statement for a problem that wants to find the square root of a number. I'm not sure if I should use a do/while, while, or a for loop. I need to approximate the square root of a number without using math.sqrt.
    : : : :
    : : : : Problem says to repeatedly make guesses at the square root until I get closer and closer to the right guess. I store the last guess and the next guess. The next guess is computed as: (last guess + N / last guess). The first time through this process, we assume that the last guess is 1.0.
    : : : :
    : : : :
    : : : : last guess = 1.0
    : : : : next guess = (last guess + N / last guess)
    : : : : loop as long as the absolute difference between the two guesses is greater than 0.005
    : : : : save the next guess in last guess
    : : : : next guess = (last guess + N / last guess)
    : : : : end loop
    : : : :
    : : : : thanx for any help
    : : : :
    : : :
    : : : Your question is already answered within your question:
    : : :
    : : : ...
    : : : [b]loop[/b] as long as the absolute difference between the two guesses is greater than 0.005
    : : : ...
    : : : [b]end loop[/b]
    : : :
    : : : [code]
    : : : ...
    : : : while (the absolute difference between the two guesses is greater than 0.005) {
    : : : ...
    : : : }
    : : : [/code]
    : : :
    : : : tron.
    : : :
    : : So the program would werk if i did this
    : :
    : : while(nextGuess < .005) {
    : : nextGuess = 1/2 * (lastGuess + x/lastGuess)
    : : nextGuess = lastGuess
    : :
    :
    : No :-(
    : Take a closer look at the pseudo code you'd posted in your original question.
    :
    : Here are some hints:
    :
    : - the difference between the two guesses: (nextGuess-lastGuess)
    : - the absolut value of that difference: Math.abs(...)
    : - loop as long as this difference is greater than 0.005: ">" not "<"
    : - "save the next guess in last guess"
    : - and after! that calculate the new guess
    :
    : If " (last guess + N / last guess)" means "1/2 * (lastGuess + x/lastGuess)" then your loop should look like this:
    :
    : [code]
    : while(Math.abs(nextGuess-lastGuess) > 0.005) {
    : lastGuess = nextGuess;
    : nextGuess = 1/2 * (lastGuess + x/lastGuess);
    : }
    : [/code]
    :
    : tron.
    :
    thanx tron....word problems always stump me in the simplest of ways....I'm sorry to ask u about another possible problem but I'm having trouble with my compiler and was wondering if u could take a quick look at the finished program and point out any glaring mistakes.thanx

    // Program2.java: Calculate square root of a number
    import javax.swing.JOptionPane;

    public class Program2 {
    public static void main(String[] args) {
    double number
    double x
    double lastGuess
    double nextGuess

    String numberstring = JOptionPane.showInputDialog(null,
    "Enter a number to compute the square root for:", "Program 2 Input",
    JOptionPane.QUESTION_MESSAGE);

    number = double.parseDouble(numberString);

    lastGuess = 1.0;
    while(Math.abs(nextGuess-lastGuess) > 0.005) {

    lastGuess = nextGuess;

    nextGuess = 1/2 * (lastGuess + x/lastGuess);

    }

    If (number <= 0) {
    JOptionPane.showMessageDialog(null, "The square root of the number: ",
    + number, "is ", + next guess, "i", JOptionPane.INFORMATION_MESSAGE);
    }
    else
    JOptionPane.showMessageDialog(null, "The square root of the number:",
    + number, "is", + next guess, JOptionPane.INFORMATION_MESSAGE);

    system.exit(0);
    }
    }


  • : thanx tron....word problems always stump me in the simplest of ways....I'm sorry to ask u about another possible problem but I'm having trouble with my compiler and was wondering if u could take a quick look at the finished program and point out any glaring mistakes.thanx
    :
    : // Program2.java: Calculate square root of a number
    : import javax.swing.JOptionPane;
    :
    : public class Program2 {
    : public static void main(String[] args) {
    : double number
    : double x
    : double lastGuess
    : double nextGuess
    :
    : String numberstring = JOptionPane.showInputDialog(null,
    : "Enter a number to compute the square root for:", "Program 2 Input",
    : JOptionPane.QUESTION_MESSAGE);
    :
    : number = double.parseDouble(numberString);
    :
    : lastGuess = 1.0;
    : while(Math.abs(nextGuess-lastGuess) > 0.005) {
    :
    : lastGuess = nextGuess;
    :
    : nextGuess = 1/2 * (lastGuess + x/lastGuess);
    :
    : }
    :
    : If (number <= 0) {
    : JOptionPane.showMessageDialog(null, "The square root of the number: ",
    : + number, "is ", + next guess, "i", JOptionPane.INFORMATION_MESSAGE);
    : }
    : else
    : JOptionPane.showMessageDialog(null, "The square root of the number:",
    : + number, "is", + next guess, JOptionPane.INFORMATION_MESSAGE);
    :
    : system.exit(0);
    : }
    : }
    :
    :

    Well, this code is really not compilable ... in this case you should try to correct the errors one by one before posting [b]such[/b] a code.

    It seams that your teacher (and the one of some of the other posters, if it's not the same) has an affection for JOptionPane dialogs, doesn't (s)he ... ;-)

    [code]
    import javax.swing.*;

    public class Program2 {

    public static void main(String[] args) {
    String numberstring = JOptionPane.showInputDialog(null, "Enter a number to compute the square root for:", "Program 2 Input", JOptionPane.QUESTION_MESSAGE);
    double number = Double.parseDouble(numberstring);

    boolean isComplex = (number < 0);
    number = Math.abs(number);

    double lastGuess = 1;
    double nextGuess = (lastGuess + number/lastGuess) / 2;
    while (Math.abs(nextGuess-lastGuess) > 0.005) {
    lastGuess = nextGuess;
    nextGuess = (lastGuess + number/lastGuess) / 2;
    }

    JOptionPane.showMessageDialog(null, "The square root of the number: " + number + " is " + nextGuess + (isComplex ? "i" : ""), "result", JOptionPane.INFORMATION_MESSAGE);

    System.exit(0);
    }

    }
    [/code]

    I strongly recommend that you not just use this code as is. You should analyze it since it has changed a lot, so you can use this information for future problems.

    tron.
  • knight2255knight2255 Member Posts: 4
    :
    : : thanx tron....word problems always stump me in the simplest of ways....I'm sorry to ask u about another possible problem but I'm having trouble with my compiler and was wondering if u could take a quick look at the finished program and point out any glaring mistakes.thanx
    : :
    : : // Program2.java: Calculate square root of a number
    : : import javax.swing.JOptionPane;
    : :
    : : public class Program2 {
    : : public static void main(String[] args) {
    : : double number
    : : double x
    : : double lastGuess
    : : double nextGuess
    : :
    : : String numberstring = JOptionPane.showInputDialog(null,
    : : "Enter a number to compute the square root for:", "Program 2 Input",
    : : JOptionPane.QUESTION_MESSAGE);
    : :
    : : number = double.parseDouble(numberString);
    : :
    : : lastGuess = 1.0;
    : : while(Math.abs(nextGuess-lastGuess) > 0.005) {
    : :
    : : lastGuess = nextGuess;
    : :
    : : nextGuess = 1/2 * (lastGuess + x/lastGuess);
    : :
    : : }
    : :
    : : If (number <= 0) {
    : : JOptionPane.showMessageDialog(null, "The square root of the number: ",
    : : + number, "is ", + next guess, "i", JOptionPane.INFORMATION_MESSAGE);
    : : }
    : : else
    : : JOptionPane.showMessageDialog(null, "The square root of the number:",
    : : + number, "is", + next guess, JOptionPane.INFORMATION_MESSAGE);
    : :
    : : system.exit(0);
    : : }
    : : }
    : :
    : :
    :
    : Well, this code is really not compilable ... in this case you should try to correct the errors one by one before posting [b]such[/b] a code.
    :
    : It seams that your teacher (and the one of some of the other posters, if it's not the same) has an affection for JOptionPane dialogs, doesn't (s)he ... ;-)
    :
    : [code]
    : import javax.swing.*;
    :
    : public class Program2 {
    :
    : public static void main(String[] args) {
    : String numberstring = JOptionPane.showInputDialog(null, "Enter a number to compute the square root for:", "Program 2 Input", JOptionPane.QUESTION_MESSAGE);
    : double number = Double.parseDouble(numberstring);
    :
    : boolean isComplex = (number < 0);
    : number = Math.abs(number);
    :
    : double lastGuess = 1;
    : double nextGuess = (lastGuess + number/lastGuess) / 2;
    : while (Math.abs(nextGuess-lastGuess) > 0.005) {
    : lastGuess = nextGuess;
    : nextGuess = (lastGuess + number/lastGuess) / 2;
    : }
    :
    : JOptionPane.showMessageDialog(null, "The square root of the number: " + number + " is " + nextGuess + (isComplex ? "i" : ""), "result", JOptionPane.INFORMATION_MESSAGE);
    :
    : System.exit(0);
    : }
    :
    : }
    : [/code]
    :
    : I strongly recommend that you not just use this code as is. You should analyze it since it has changed a lot, so you can use this information for future problems.
    :
    : tron.
    :
    thanx again...i'm lookin over at what u did and it most of it makes sense....i was just wondering tho...can i keep the last bit

    If (number <= 0) {
    : JOptionPane.showMessageDialog(null, "The square root of the number: ",
    : + number, "is ", + next guess, "i", JOptionPane.INFORMATION_MESSAGE);
    : }
    : else
    : JOptionPane.showMessageDialog(null, "The square root of the number:",
    : + number, "is", + next guess, JOptionPane.INFORMATION_MESSAGE);
    :
    : system.exit(0);
    : }
    : }

    we haven't really gotten into the boolean statements? will this still compile or is it still too sloppy? thanx....btw, do u recomment a good beginner book with alot of real world problems in it?
Sign In or Register to comment.