Thread: New Program

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Lucid15 View Post
    Ok problem fixed with the -1 i have no idea what i was thinking, and does it really matter how many lines are within the loop? i guess hes never told us that.
    I suppose it doesn't really matter how many lines are in the loop per se; the problem is that if you look at the formula or algorithm, there are three things that happen repeatedly. If you try to do more or less things than three things repeatedly, then you're not actually doing the formula. (In case you've forgotten how to scroll back up, the three things are: subtract two from n, square the new n, and divide that by (2+old_result) putting that answer back into old_result.) You'll also need to start old_result off at n^2 before the loop starts.

  2. #17
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    arent i still accomplishing all of those steps?
    Code:
    while(n>1){
       a= pow(n,2);
       a=a+2;
       n=n-2;
       b=pow(n,2);
       c=(b/a) +2;
       n=n-2;
      }
    and i need to square the n first right? not subtract two right away

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Lucid15 View Post
    arent i still accomplishing all of those steps?
    Code:
    while(n>1){
       a= pow(n,2);
       a=a+2;
       n=n-2;
       b=pow(n,2);
       c=(b/a) +2;
       n=n-2;
      }
    and i need to square the n first right? not subtract two right away
    No, and no. You can't handle the first n inside the loop, since it doesn't get divided. Hence you must handle the first n outside the loop, then your loop must (1) subtract two from n (2) square the new n (3) divide the result from step 2 by (2+c).

    You are (1) squaring n (2) adding two to n^2 (which should never happen, since it isn't in the formula anywhere) (3) subtracting two from n (4) squaring this new n (5) dividing the result of step 4 by the result of step 2, which throws away the old answer completely instead of reusing it, which makes the whole loop pointless (6) subtracting two from n again.

  4. #19
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Sorry i couldnt make this clear enough on the computer... for the first initial n, you need to square it then add two to it lets call it (a) Then you square your new n and divide it by (a). and add 2 to THAT new number. So the numerator is always just the new n^2.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Lucid15 View Post
    Sorry i couldnt make this clear enough on the computer... for the first initial n, you need to square it then add two to it lets call it (a) Then you square your new n and divide it by (a). and add 2 to THAT new number. So the numerator is always just the new n^2.
    I firmly agree. The numerator you have right (every other time). The denominator, on the other hand.... well, look at the formula or the algorithm -- the denominator involves the answer you got last time through the loop, not just n^2.

  6. #21
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Sorry lol. your last post is hard to follow cause i modified my code a little bit.
    Code:
    #include <FPT.h>
    int main()
    {
      double n,a,b,d,c;
      n=inD();
      a=1;
      b=1;
      d=0;
      while(n>1){
        a=pow(n,2)+2;
        n=n-2;
       b=pow(n,2);
       c=b/a;
       d=d+c;
       n=n-2;
      }
      if(n==1){
        a=pow(n,2);
        d=(a/d)+1;
    
      }
    
    
    
        outD(4/d);
      
      
      }
    i SEEMED to have come closer, but it's still wrong.

  7. #22
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    ok so heres my UPDATE code...
    Code:
    #include <FPT.h>
    int main()
    {
      double n,a,b,d,c;
      n=inD();
      a=1;
      b=1;
      d=0;
      while(n>1){
        a=pow(n,2)+2;
        n=n-2;
       b=pow(n,2);
       c=(b/a)+2;
       d=d+c;
       n=n-2;
      }
      if(n==1){
        a=pow(n,2);
        d=(a/d)+1;
    
      }
    
    
    
        outD(4/d);
      
      
      }

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is there a paper shortage that I've missed? Have you even looked at your code to see what it does? Let's use n=13, since that's what I used before, and see what your loop does:

    1. Set a = 169+2 = 171
    2. Set n = 11
    3. Set b = 121
    4. Set c = 121/169 + 2 = 2.7076
    5. Set n = 9
    <loop>
    1. Set a = 81+2 = 83
    2. Set n = 7
    3. Set b = 49
    4. Set c = 49/83 + 2 = 2.5904
    5. Set d = d + c = 5.2980
    6. Set n = 5
    <and so on>
    This has no resemblance to what you're supposed to do. Read the formula again. Or read the algorithm I provided again, whichever makes you happy:
    Start with current_result = n^2.
    WHILE LOOP STARTS HERE
    Subtract two from n.
    Square it.
    Take that square and divide by (current_result+2), putting the answer back in current_result.
    WHILE LOOP STOPS HERE
    Do the 1+ and the 4/ at the end.
    The current result provides the denominator for the next result. Until and unless you get something even slightly approaching this, I'm done here.

  9. #24
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    ok i think i fixed the while loop...
    Code:
    #include <FPT.h>
    int main()
    {
      double n,a,b,d;
      n=inD();
      b=1;
      a=pow(n,2);
      while(n>1){
        n=n-2;
        b=pow(n,2);
       a=b/(a+2);
      }
      if(n==1){
        b=pow(n,2);
        a=b/(a+1);
      }
      else{
        d=4/(a+1);
    
      }
    
      outD(d);
      
      
      }
    But i believe that for my n==1 if statement that i messed up something, ill try figuring out what it is.

  10. #25
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    fixed it up a tad further...
    Code:
    #include <FPT.h>
    int main()
    {
      double n,a,b,d;
      n=inD();
      a=pow(n,2);
      while(n>1){
        n=n-2;
        b=pow(n,2);
       a=b/(a+2);
      }
      if(n==1){
        d=4/(1+a);
      }
      
    
      outD(d);
      
      
      }
    Something is still a little wrong

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    At least for 13, I got the answer I got above with this code.

  12. #27
    Registered User
    Join Date
    Sep 2008
    Posts
    101
    Oh dang, it is right lol. Cause i kept going through the forumla and nothing seemed wrong. Well i appreciate your help greatly. From now on, i will try it for a couple hours on paper then try to emulate it on the computer. Sorry im a noob

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM