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

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




Having Troubles with C++ Code

2 Pages V  1 2 >  
Reply to this topicStart new topic

Having Troubles with C++ Code

Miller98
post 15 Mar, 2008 - 07:02 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()
{

    [color=#FF0000]Declare x, y, temp, remainder as Integer.[/color]

    // 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;

    [color=#FF0000]Write a C++ if statement to determine if x < y.[/color]
       { // 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]

    while ([color=#FF0000]   [/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 a beginner and I am having difficulty making this code work. The instructions I was given are in red.
User is offlineProfile CardPM

Go to the top of the page

letthecolorsrumble
post 15 Mar, 2008 - 07:30 AM
Post #2


Student of The Sun

Group Icon
Joined: 7 Nov, 2007
Posts: 550



Thanked 1 times
My Contributions


"Declare x, y, temp, remainder as Integer."
To declare varibales as integers, all you need to do is

int x,y,temp, remainder;

That should work.

For if statements you need to put the condition-expression inside braces.

if(x<y)

If you have trouble with rest of the code, I'd suggest you it out by yourself first with the help of a book and then if you are still stuck somewhere, post your questions.
User is offlineProfile CardPM

Go to the top of the page

MorphiusFaydal
post 15 Mar, 2008 - 07:37 AM
Post #3


D.I.C Lover

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



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


http://www.cplusplus.com/doc/tutorial/
User is online!Profile CardPM

Go to the top of the page

Miller98
post 15 Mar, 2008 - 07:50 AM
Post #4


New D.I.C Head

*
Joined: 8 Mar, 2008
Posts: 20

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:17 AM
Post #5


D.I.C Lover

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



Thanked 8 times

Expert In: Hardware, Networking

My Contributions


Okay. So what're you having trouble with. We're not going to do your homework for you.
User is online!Profile CardPM

Go to the top of the page

Miller98
post 15 Mar, 2008 - 08:24 AM
Post #6


New D.I.C Head

*
Joined: 8 Mar, 2008
Posts: 20

MorphiusFaydal, if you don't have any positive feedback, please do not comment. I am learning this on my own and I have come to a fork in the rode and I am looking for assistance. Promoting websites are not welcome at all.
User is offlineProfile CardPM

Go to the top of the page

Miller98
post 15 Mar, 2008 - 08:34 AM
Post #7


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

         Set temp=x
         Set x=y
         Set y=temp

       }

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


    Initialize remainder.

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

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

    return (0); // terminate with success


Thanks letthecolorsrumble. I think I am on the right track.....I hope. Still not sure what to put in the following:

Initialize remainder.

while ( )
User is offlineProfile CardPM

Go to the top of the page

Miller98
post 15 Mar, 2008 - 08:41 AM
Post #8


New D.I.C Head

*
Joined: 8 Mar, 2008
Posts: 20

I was trying to figure this out through your website MorphiusFaydal. Is this suitable statement for C++? I am starting to think it isn't.

Set temp=x
Set x=y
Set y=temp
User is offlineProfile CardPM

Go to the top of the page

Renzokusen
post 15 Mar, 2008 - 08:47 AM
Post #9


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 32

I believe what MorphiusFaydal is saying is we cannot do your homework for you. If you are attempting to convert this program from pseudocode to c++ we can only help you if you run into a problem. I believe it's somewhere in the rules for these forums.
CODE


#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

         int temp = x;
         x=y;
         y=temp;

       }

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


    //Initialize remainder.

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

    // display the result
    cout << endl;
    cout << "The GCD is: " << y << endl;
    int t;
    cin >> t;

    return (0); // terminate with success
}


I fixed it a little so now it will compile so all you have to do is fiddle around with it. I wasn't sure why you had a while statement there.

This post has been edited by Renzokusen: 15 Mar, 2008 - 08:51 AM
User is offlineProfile CardPM

Go to the top of the page

Renzokusen
post 15 Mar, 2008 - 08:53 AM
Post #10


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 32

This file is the single most helpful programming file for noobs such as myself. It shows how each statement should be presented. A quick reference guide. My friend got it somewhere I think it was from this site, but I cannot seem to relocate it so I'll attach it.


Attached File(s)
Attached File  cpp_reference_sheet.pdf ( 909.1k ) Number of downloads: 20
User is offlineProfile CardPM

Go to the top of the page

Miller98
post 15 Mar, 2008 - 09:03 AM
Post #11


New D.I.C Head

*
Joined: 8 Mar, 2008
Posts: 20

Thanks for you help Renzokusen, but it still won't compile correctly. I have spent 2 days on this simple code and I know it is an easy fix but I can't figure it out. Anyone know of a book that can explain, how to convert pseudocode to C++?
User is offlineProfile CardPM

Go to the top of the page

Renzokusen
post 15 Mar, 2008 - 09:13 AM
Post #12


New D.I.C Head

*
Joined: 11 Mar, 2008
Posts: 32

QUOTE(Miller98 @ 15 Mar, 2008 - 10:03 AM) *

Thanks for you help Renzokusen, but it still won't compile correctly. I have spent 2 days on this simple code and I know it is an easy fix but I can't figure it out. Anyone know of a book that can explain, how to convert pseudocode to C++?



The problem I found with the program was the gcd. It will always post a crazy number.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/22/08 12:46AM

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