• Support PF! Buy your school textbooks, materials and every day products Here!

Java infinite loop (problem statement)

  • Comp Sci
  • Thread starter Highway
  • Start date
  • #1
349
1

Homework Statement



http://pastebin.com/7ehbwkvE [Broken]

expires in 1 month...

the problem statement is very vague... we are given this code of an infinite loop, and need to fix the problem that it poses by either using a correct while loop or a for loop.

Homework Equations





The Attempt at a Solution



so looking at this and the comments given, the loop seems to be creating a divergent sequence that will never satisfy the terms of the while loop and terminate... though, i dont know what/if that sequence is convergent at all...

i changed the while (term < 5000) and it ended fine as expected. . . i dont even know why i made this topic after all lol
 
Last edited by a moderator:

Answers and Replies

  • #2
33,270
4,975

Homework Statement



http://pastebin.com/7ehbwkvE [Broken]

expires in 1 month...

the problem statement is very vague... we are given this code of an infinite loop, and need to fix the problem that it poses by either using a correct while loop or a for loop.

Homework Equations





The Attempt at a Solution



so looking at this and the comments given, the loop seems to be creating a divergent sequence that will never satisfy the terms of the while loop and terminate... though, i dont know what/if that sequence is convergent at all...

i changed the while (term < 5000) and it ended fine as expected. . . i dont even know why i made this topic after all lol
Instead of posting your code at some remote site, why not just put it directly in your post, like this?

Also, when you post code here, put [noparse]
Code:
 and
[/noparse] tags around it.
Code:
class SumsLimit {

     public static void main (String args[])
    {
       double sum = 1.0 ;
       int term = 2 , x= 0; 

       /*               
        *       1-1/2+1/3-1/4..stop when Sum is approximately 0.6931.
        *       and as long as term is greater than 0.0001 
        *       your loop goes here  
        *       (a for, while or do while loop your choice)
        *       for (x = 0; x <= 5000; x++)
        *       while (term < 5000 )
        */

       while ((term >.0001)  )
       {
          System.out.println (  sum);
          System.out.println ( "\n" + x); 
          if (term % 2 == 0)
             sum = sum - (1/(double)term) ;
          else sum = sum + (1/(double)term);
          //end if
          term++;
          x++;
          System.out.println (  sum);
          System.out.println ( "\n" + x);
       } // end loop
                
    } // end main                           

} // end SumsLimit
On the offchance that you don't know why the code above is wrong, term is defined as an int, so why is it checking to see if term > .0001?
 
Last edited by a moderator:
  • #3
349
1
yeah, i saw that after, but still used pastebin, because of the syntax highlighting. . .

our professor doesnt think anything through, or make a clear defined problem statement (there is nothing formally written up that we have to follow while doing a program -- it's ridiculous).

most of the code he gives us to use as a basis is full of errors and other stuff like what you saw above.
 
  • #4
33,270
4,975
yeah, i saw that after, but still used pastebin, because of the syntax highlighting. . .
Which is really irrelevant. If you want help, make it as easy as possible on the people helping you, by posting the code directly right here. I would venture to say that some people would not want to go to the trouble of clicking a link to see the code.

Having the code here allows us to insert a comment at exactly the right point in your code, and not have to describe verbally where the problem is. Plus we don't have to deal with writing something in one window while looking at the code in another window.
 
  • #5
349
1
gotcha, thanks.
 

Related Threads on Java infinite loop (problem statement)

  • Last Post
Replies
3
Views
1K
  • Last Post
Replies
2
Views
1K
Replies
7
Views
3K
Replies
3
Views
2K
Replies
3
Views
526
Replies
8
Views
2K
Replies
1
Views
1K
  • Last Post
3
Replies
55
Views
5K
  • Last Post
Replies
9
Views
4K
Top