Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,339 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,111 people online right now. Registration is fast and FREE... Join Now!




Trying to convert pseudocode to C++

 
Reply to this topicStart new topic

Trying to convert pseudocode to C++

Miller98
post 15 Mar, 2008 - 08:04 AM
Post #1


New D.I.C Head

*
Joined: 8 Mar, 2008
Posts: 20

CODE

#include "stdafx.h"

#include <iostream>

using namespace std;

int main()
{

    int x,y,temp, remainder;

    // read in the two integers

    cout << endl;
    cout << "Enter the first number (positive integer) : ";
    cin >> x;
    cout << "Enter the second number (positive integer) : ";
    cin >> y;

    //echo inputs

    cout << "Input numbers are: " << x << " , " << y << endl;

    if(x<y)
       { // exchange values of x and y

         [color=#FF0000]Write three assignment statements in C++ that are
         equivalent to the three statements given in the
         pseudocode.[/color]
       }

    /* At this point we will always have x >= y */


    [color=#FF0000]Initialize remainder.[/color]

    [color=#FF0000]while (   )[/color]
        {        
            [color=#FF0000]Write the loop expression and loop body code in C++. In C++, the expression (x % y) gives the remainder
            after dividing x by y.[/color]        
         }

    // display the result
    cout << endl;
    cout << "The GCD is: " << y << endl;

    return (0); // terminate with success


I am having trouble with writing red.


This is the pseudocode which I have to go by to complete the code:

Declare x,y,temp, remainder as integer

Input x
Input y

If (x<y) then // exchange values of x and y
{
Set tmep=x
Set x=y
Set y=temp
}

End if

/* At this point we will always have x>=y*/

Set remainder=(x%y) //% is the remainder operator

While (remainder !=0)

{
Set x=y
Set y=remainder
Set remainder=(x%y)
}
End while

Output y // the final GCD will be in y
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 15 Mar, 2008 - 08:16 AM
Post #2


D.I.C Lover

Group Icon
Joined: 12 May, 2005
Posts: 1,086



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


Please don't double post. You already have a topic about this.
User is offlineProfile CardPM

Go to the top of the page

Renzokusen
post 15 Mar, 2008 - 08:55 AM
Post #3


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 32

Already replyed to your last post build apon one thread do not double post.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/22/08 03:34AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month