Thread: Redefining within a loop (?)

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Redefining within a loop (?)

    Hello everyone, I was just trying make a code in which you have to change the variable everytime there is a loop, but I can't find a way to work it out.

    Code:
    int main()
    {
    
        double rq = 1; 
        double rqn, rqnii, dif, nMaxIter;
        int n;
     
        cout<<"nMaxIter: ";
        cin>>nMaxIter;
        cout<<"Insert a number : "
        cin>>n;
    
    
    
        for (int i = 1; i <= nMaxIter; i++)
        {
            rqn = ((rq + n) / rq) /2;
            rqnii = pow(rqn,2);
            dif = n - rqnii;
    
    
            cout<<"rqn = " <<rqn <<" ";
            cout<<"rqnii = " <<rqnii <<" ";
            cout<<"dif = " <<dif <<" " <<endl;
    
            rq = rqn;
    
                    
        }
    }
    I posted above a portion of code which shows how I tried to pull it off. The loop is supposed to compute rqn, rqnii and dif ops, considering rq starts as 1. After the 1st loop, rq should assume rqn's value hence I addded:

    Code:
    rq = rqn;
    but it's not working as it's supposed to. It gives me different values each loop, but not the correct ones, which should be the following:
    (Results for n = 20 and nMaxIter = 5)

    http://i1060.photobucket.com/albums/...psacf97776.png

    My results:
    (Results for n = 20 and nMaxIter = 5)

    http://i1060.photobucket.com/albums/...psa9ff8936.png
    Last edited by Khabz; 03-05-2013 at 07:53 AM.

  2. #2
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I just made some tests, and rq actually works correctly when I assume it's equal to rqn.
    Results are different though.
    I will post an update soon, I might have misread the exercise itself.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    It was my fault, misread the exercise lol.
    Anyway, it consists on an algorithm for the square root. Here goes the code if anyone is interested:

    Code:
    #include <iostream>
    
    
     using namespace std;
    
    
    int main()
    {
        double delta, nMaxIter;
        int n;
    
    
        cout<<"delta: ";
        cin>>delta;
        cout<<"nMaxIter: ";
        cin>>nMaxIter;
        cout<<"Numero: ";
        cin>>n;
    
    
        double rqn, rqnii, dif;
        double rq = 1;
    
    
        for (int i = 1; i <= nMaxIter; i++)
        {
            rqn = ((rq + (n / rq)) /2);
            rqnii = pow(rqn,2);
            dif = n - rqnii;
            
            rq = rqn;
    
    
            if (abs(dif) <= delta || i == nMaxIter)
            {
                cout<<"rq = " <<rq <<endl;
                break;
            }
    
    
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redefining arrays
    By FlyingShoes in forum C Programming
    Replies: 1
    Last Post: 06-25-2009, 12:16 PM
  2. redefining variables without extern
    By robwhit in forum C Programming
    Replies: 16
    Last Post: 05-23-2008, 02:24 PM
  3. Redefining Structs
    By Rune Hunter in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2006, 06:55 PM
  4. Redefining an operator... is it possible?
    By Trauts in forum C++ Programming
    Replies: 3
    Last Post: 11-21-2002, 12:00 PM
  5. redefining and overriding members
    By strobe9 in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2002, 03:43 PM