1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

New Student With C++ Program

Discussion in 'C++' started by mineja101473, Oct 29, 2007.

  1. mineja101473

    mineja101473 New Member

    Joined:
    Oct 29, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I need help with this program. I just started and I can't get it to work. Help:
    Code:
    #include <iostream> 
    
    using namespace std ;
    
    int main() 
    { 
    
        int x, y, temp, remainder ; // Compute GCM
        
        //read in the two integers
         
        cout << endl ;
        cout << "Enter the first number (integer) : " ; 
        cin >> x ;
        cout << "Enter the second number (integer) : " ; 
        cin >> y ;
    
        //echo inputs
    
        cout << "Input numbers are: " << x << " , " << y << endl;
    
        {// exchange values of x and y 
        
        if(x < y)
        {
            int temp = x;
            x = y;
            y = temp;
        }
        else if(x < 10)
        {
            x = x - 1 ;
        }
        else 
        {
            x = x + 1 ;
        }
    } 
    
        /* At this point we will always have x >= y */
    
        //Initialize remainder.
     
        while(remiander!=10)
    {
        remainder = x % y;
        x = y;
        y = remainder;
    } 
    
        // display the result
        cout << endl ;
        cout << "The GCD is: " << y << endl ;
    
        system("PAUSE");
        return (0);
    }
     
    Last edited by a moderator: Oct 29, 2007
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    Will you try to explain what you are trying to get yourself work out and what you are not able to ?
     
  3. mineja101473

    mineja101473 New Member

    Joined:
    Oct 29, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    This is basically what I have to do:

    I had to declare x, y, temp, and remainder as integers. Then I have to write a C++ if statement to determine if x < y. It is necessary for me to "swap" these values.

    This requires a “temp” variable.

    if ( x < y)
    {
    temp = x;
    x = y;
    y = temp;
    }

    This will put x into a temp storage area. It will resets x = y. Now, I have to put x in y but it is gone. However, it is stored in temp!

    To initialize the remainder, I have to initialize it to some value, usually 0 or 1 but could be something else. I have to use something else.

    remainder = (x % y);

    This will give me the remainder after division of x by y. (Note, can’t use y = 0.)

    Now, in the “while loop”:

    while (remainder != 0)

    {
    x = y
    y = remainder;
    remainder = (x % y);
    }

    In a nutshell I have to come up with a while loop and if statement with these guidelines. I've tried but I can't get it to work. Once I set everything up I have to plug in the following values:
    x y Expected answer
    1 1
    2 2
    5 5
    20 20
    2 10
    10 2
    36 48
    252 108
    1024 256
    23,205 1,638
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    So where is the problem.

    Also I would suggest you read Hints before you make a post to help you in posting your queries and code in the post
     

Share This Page