Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   New Student With C++ Program (http://www.go4expert.com/forums/student-cpp-program-t7100/)

mineja101473 29Oct2007 12:30

New Student With C++ Program
 
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);
}


shabbir 29Oct2007 17:50

Re: New Student With C++ Program
 
Will you try to explain what you are trying to get yourself work out and what you are not able to ?

mineja101473 29Oct2007 20:18

Re: New Student With C++ Program
 
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

shabbir 29Oct2007 20:31

Re: New Student With C++ Program
 
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


All times are GMT +5.5. The time now is 02:48.