C++ Programming Help Request.*Updated*

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
I'm reading up on this C++ programming excercise, and I'm really confused. Anyways, heres the program question (w/ example) in its entirety:

==============================
The greatest common divisor (GCD) of two positive integers x and y, such that x <= y, can be found by repeating the following ...

1. Let r = y % x
2. If r == 0 then the GCD is x
3. Otherwise let y = x and x = r

Rules:

* You must write both a function prototype and definition for a function named gcd, that when given two positive integers (in any order) computes (as specified above) and returns their GCD.
* You must write a main function that:
1. Prompts the user to enter three positive Integers a,b,c
2. Uses your gcd function to compute the GCD between each pair of input values, you will need to save these as well.
3. Display a reasonable report with the input values and their GCD's.

================================

Sample run(s): AKA: Screen View. AKA Answer to the questions above, what the program will look like when it runs successfully.

grid: sample10

please enter a positive integer (a) 24
please enter a positive integer (b) 8
please enter a positive integer (c) 3

Generating Report ...

gcd(24, 8) = 8
gcd(24, 3) = 3
gcd(8, 3) = 1

grid: sample10

please enter a positive integer (a) 36
please enter a positive integer (b) 20
please enter a positive integer (c) 45

Generating Report ...

gcd(36, 20) = 4
gcd(36, 45) = 9
gcd(20, 45) = 5

grid: sample10

please enter a positive integer (a) 31
please enter a positive integer (b) 19
please enter a positive integer (c) 7

Generating Report ...

gcd(31, 19) = 1
gcd(31, 7) = 1
gcd(19, 7) = 1

=======================






Now could someone PLEASE walk me through this as if I were a bloody child. This is my attempt:

=======================
#include <iostream>
using namespace std;

bool GCD(int what)

int a;
int b;
int c;

int x;
int y;
int z;

int main ()
{
r = y % x
cout << " Please Enter A positive Integer: " << endl;
cin << a;
cout << " Please Enter A positive Integer: " << endl;
cin << b;
cout << " Please Enter A positive Integer: " << endl;
cin << c;
cout << endl;

cout << " Generating Report . . . " << endl;

cout << "gcd" << "(" << a << "," << b << ")" << "=" << b << endl;
cout << "gcd" << "(" << a << "," << c << ")" << "=" << c << endl;
cout << "gcd" << "(" << b << "," << c << ")" << "=" << c << endl;


if (GCD(what))
{
????????????????????????

bool GCD(int what)
{
if r == 0
{
return x;
}
else
{
return false;
}
}

=================
Now I admit that its far from done, but I am really confused... Please if you can help, lend me a minute. :)
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
don't know what "%" means... Modular? Whats that? "Divisible by"?

% = modular = remainder when you do old handwritten division.

for example:

10 divided by 7 is equal to 1 with a remainder of 3.

same thing as:

10 % 7 = 3
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
"Modulus Operator: %
The result of the modulus operator (%) is the remainder when the first operand is divided by the second.

Example

In the following example, the modulus operator is used to determine if the numeric value 4 evenly divides into nCenturyYear. If the remainder is zero, the nCenturyYear must be a leap year, so the LeapYearFunction() is executed.

// Example of the modulus operator
int nCentury;

if ((nCenturyYear % 4) == 0) {
LeapYearFunction() ;
} "


EXAMPLES:

5 % 0 = error! can't divide by 0
5 % 1 = 0
5 % 2 = 1
5 % 3 = 2
5 % 4 = 1
5 % 5 = 0
5 % 6 = 5
( 5 % X = 5 for all X>5 )

 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Work through some examples of % on paper with different numbers until you understand it.

Then work through the supplied algorithm on paper with different numbers until you understand how it works too.

Once you understand what you're doing the rest should be easy.
 

Schadenfroh

Elite Member
Mar 8, 2003
38,416
4
0
By the way, my old C++ professor has his supplemental notes for his lecture online in a fairly easy to understand and compact .pdf for his CSC101 class.

Here is his web page, read over chapter 2, should have everything you need to know to write your program in the acrobat file:
http://orca.st.usm.edu/~aspurgeo/
 

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
Ahhh thats what % means. Could anyone help me out with the rest of this problem. When I seem to see the problem worked out, I can understand it better.. and would like to find another program question in this book related to this one to work on next.

Whats the program asking me to do?

This is what I think it is asking:

Find GCD between x and y.
Conditions: x has to be less than or equal to y.
1. "r = y % x" -->The remainder has to equal y divided by x.
2. If the remainder is 0, then GCD is X.
3. Otherwise let y = x and x = r --> else make y=x and x=r?? whats that mean, how can I show that?

Then the samples (the screen view answers), confuse me...
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Try steps 1 - 3 with some real numbers like:
24 and 2
24 and 5
24 and 6
24 and 12

What happens?

Work through the problem, don't give up and ask for someone else's solution too soon.

A huge part of programming is learning to solve problems.
 

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
I don't even understand what they are asking in 1 - 3... it makes no sence. I don't know why I need 6 variables, why not X, Y, Z for the whole problem? I don't know any of this stuff, its hard to do a problem when you don't understand the concept behind solving it..
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Originally posted by: Coldkilla
I don't even understand what they are asking in 1 - 3... it makes no sence. I don't know why I need 6 variables, why not X, Y, Z for the whole problem? I don't know any of this stuff, its hard to do a problem when you don't understand the concept behind solving it..

Are you sure you copied that from a book? Cause the everpresent "???????????" line means there's stuff missing and I even "compiled" it in my head and I can tell you that it sure as hell won't work.
 

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
I put the ?????????? there, thats my "take" on the program. I didn't know what to do there, or at many other places.... Thats what I'm asking you guys.

edit: I edited main post to bold the parts that I guess were unclear.
 

gerwen

Senior member
Nov 24, 2006
312
0
0
Just learning C++ myself, self teaching though.

Looks like you're trying to do to much in main().

In main, you want to simply request the 3 integers, and output the results, letting your function gcd() do the work of finding the gcd. Your program should look something like this:

<code attached at bottom, read that now>

I don't want to give away too much, but your prototype is wrong. You have:
bool GCD(int what)

In this program, you want to pass 2 ints to gcd, and have it return an int (the gcd)
prototypes look like this:
returntype functioName(argument1type, argument2type, ...);

in this case, it should be:
int gcd(int , int);

which tells the compiler that you plan to pass 2 ints as arguments, and expect an int as a return.

Hope this helps, hammer away at it for a while, I did this exercise myself, and it works well, so if you're still stuck i can give you more hints in the right direction.


Currently i'm working my way through C++ for dummies (i know, i know), and it's easy to understand, but has no exercises to do. Programming is a learn-by-doing thing for me, so can anyone point me at some good exercises (or a really good book) for a complete C++ beginner who has a little bit of experience with other languages and programming concepts.

Thanks
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Originally posted by: Coldkilla
I put the ?????????? there, thats my "take" on the program. I didn't know what to do there, or at many other places.... Thats what I'm asking you guys.

edit: I edited main post to bold the parts that I guess were unclear.

Oh okay, it's my misunderstanding that you were copying an example out of a book.

Also, gerwen, you can specify the variable name in the prototype. It doesn't really matter or affect anything.

Cold, one thing you can try to do is literally translate step 1-3 into code. They're very "code-esque" right now.
 

Coldkilla

Diamond Member
Oct 7, 2004
3,944
0
71
I don't even know what the 'code' would be, I have no idea what the directions are even asking, its like speaking Japanese to someone who doesn't know anything about it... I'd gladly make it into code... but its kinda hard when I don't know what they are even asking.
 

gerwen

Senior member
Nov 24, 2006
312
0
0
The 3 steps give you a way to calculate the GCD. They work as long as x<=y

1. assign y % x to the variable r (% is a mathematical operator just like + or -, it is the remainder of y divided by x)
2. if r is equal to 0, then x is the gcd and you can have the function return x
3. if r isn't equal to 0, then make y = x and x= r, and return to 1

keep repeating these 3 steps until r does equal 0, and you have your GCD.

You will have to modify the steps when you get x>y so that it works in that case too.

This post plus my previous post has pretty much all you need to make another attempt at coding this. Post what you come up with and we'll see where we are.
 

Aikouka

Lifer
Nov 27, 2001
30,383
912
126
Originally posted by: Coldkilla
I don't even know what the 'code' would be, I have no idea what the directions are even asking, its like speaking Japanese to someone who doesn't know anything about it... I'd gladly make it into code... but its kinda hard when I don't know what they are even asking.

Look at what you've learned and try to apply an English definition of these ideas into what the problem wants. The ability to go from an "Anglicized" problem to pseudo-code (or code) is a great skill. Gerwen even made it more code-y for ya.

For example, this says "Let r = y % x".
Sometimes it helps if you say it in English, "Let r equal y modulo x" or "give r the value of y modulo x" (something to that effect).
"Let" is used a lot in mathematics to say that you're assigning a value to something or stating something. Typically you'll see this to show that you want to define something or assign a value to a variable.

Another hint is that Steps 2 and 3 go together. You can get this by the words used. "Otherwise" grammatically cannot be used without something to define the "other" case. So in other words, if 2 doesn't happen, then 3 will happen and vice-versa.