Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Loops & Series Question

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Toronto, ON
    Posts
    3
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Loops & Series Question

    Assigned Question:

    The infinite series:
    " 1 + 22 * x + 32 * x2 + 42 * x 3 + ....."

    Each term in the series is made up of a coefficient multiplying a power of x. The coefficients are the squares of the successive integers starting with 1, and the powers uses successive integers, starting with 1, and the powers uses successive exponents starting at 0. The sum converges when |x| is less than 1.

    Write a program that reads x, verifies that |x| < 1 or else crashes, and then computes and outputs the sum together with the oracle's answer:
    Oracle is: (1 + x) / (1 - x)3

    To compute the sum, keep adding terms until you reach a term whose value is less than 0.0001 in absolute value.

    I wrote the program.. but something is missing because when I compiled it & ran it... my program would keep printing the correct value ( the answer of the series of the sum) an infinite amount of times.. How do I fix it?

    Here is my program:

    import java.io.PrintStream;
    import java.util.Scanner;
    import type.lib.ToolBox;
     
    public class Check07C
    {
       public static void main(String[] args)
       {
          PrintStream out = System.out;
          Scanner in = new Scanner(System.in);
     
     
          out.print("Enter the value of x ... ");
          double x = in.nextDouble();
          double series = 0;
     
          if (Math.abs(x) > 1.0)
          {
             ToolBox.crash(true, "|x| must be < 1.");
          }
          else 
          {
             double y = Math.pow(1 - x, 3);
             double oracle = (1 + x) / y;
             out.println("The Oracle's answer is: " + oracle);
     
              for (int i = 0;  ;i++)
             {
                double pow = Math.pow(i, 2);
                double pow1 = Math.pow(x, i - 1.0);
                double term = pow * pow1;
     
                if (Math.abs(term) < 0.0001)
                {
     
                   out.println("The sum of the series = " + series);
                }
                else 
                {
                   series = series + term;
                }
             }
     
          }
       }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Loops & Series Question

    Quote Originally Posted by nuttaay View Post
              for (int i = 0;  ;i++)
    When do you expect that loop to exit?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Toronto, ON
    Posts
    3
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loops & Series Question

    I want it to exit the loop when a term's absolute value is less than 0.0001. Thus I added a condition in the for loop:

     
     boolean check = true;
     
          out.print("Enter the value of x ... ");
          double x = in.nextDouble();
          double series = 0;
     
          if (Math.abs(x) > 1.0)
          {
             ToolBox.crash(true, "|x| must be < 1.");
          }
          else 
          {
             double y = Math.pow(1 - x, 3);
             double oracle = (1 + x) / y;
             out.println("The Oracle's answer is: " + oracle);
     
              for (int i = 0; check == true; i++)
             {
                double pow = Math.pow(i, 2);
                double pow1 = Math.pow(x, i - 1.0);
                double term = pow * pow1;
     
                if (Math.abs(term) < 0.0001)
                {
     
                   check = false;
                   series = series + term;
                   out.println("The sum of the series = " + series);
                }
                else 
                {
                   series = series + term;
                }
             }
     
          }
       }
    }

    but now, the answer to the sum of the series is incorrect.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Loops & Series Question

    What do you mean by incorrect? How off is it?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Location
    Toronto, ON
    Posts
    3
    My Mood
    Worried
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Loops & Series Question

    COMMAND PROMPT:

    Enter the value of x ... 0.001254
    The Oracle's answer is: "1.005030184256893"
    The sum of the series = 0.0


    From my textbook, it says the answer for the sum of the series should be : 1.005016
    Last edited by nuttaay; November 29th, 2011 at 12:32 PM.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 555 Times in 476 Posts

    Default Re: Loops & Series Question

    Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Fibonacci series
    By seanman in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 11th, 2011, 12:32 PM
  2. Sum of series program logical error
    By Neel0075 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 12th, 2011, 11:26 AM
  3. Interpolation of time series data
    By adyehh in forum Java Theory & Questions
    Replies: 0
    Last Post: January 27th, 2011, 10:21 AM
  4. problem to get Fibonacci series- please help.
    By zoala001 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 3rd, 2011, 12:10 AM
  5. [SOLVED] displaying sum of harmonic series
    By sriraj.kundan in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 15th, 2009, 11:42 PM