What's new

Greatest Common Factor (GCF) Calculator

Status
Not open for further replies.

Garstyciuks

New member
x-=x would be equal to 0, but x=-x assigns the variable x to its negative value. If you don't like it that way, you may try x*=-1, which would translate to x = x * (-1). C or C++, or most of other programming languages don't solve algebraic equations. For that you have to use some higher level functional programming language. Or you should get a C++ book for beginners. In C++ you can't tell the computer what he has to do and what to solve, you tell it the steps required to achieve the results you want.

Whatever you say about a != b = c, I have never seen such in any maths and algebra textbooks. Instead I've seen a != b, b = c. And thanks for enlightening me how inequality sign looks, I really didn't know that.

Yes, I understand that, but to be honest I'm not getting the science of the coding. Why is gcd defined as its own seperate function apart from the main function?

Actually you're not getting the proper design of the program. It's much more logical to declare gcd function in global scope, not in main function scope. Because then you can use gcd function in other global functions and in other cpp files (if you have a multidocument project). It's not a very clever design to declare everything into main function scope.
 
Last edited:
OP
Iconoclast

Iconoclast

New member
x-=x would be equal to 0, but x=-x assigns the variable x to its negative value. If you don't like it that way, you may try x*=-1, which would translate to x = x * (-1). C or C++, or most of other programming languages don't solve algebraic equations. For that you have to use some higher level functional programming language. Or you should get a C++ book for beginners. In C++ you can't tell the computer what he has to do and what to solve, you tell it the steps required to achieve the results you want.

Whatever you say about a != b = c, I have never seen such in any maths and algebra textbooks. Instead I've seen a != b, b = c. And thanks for enlightening me how inequality sign looks, I really didn't know that.



Actually you're not getting the proper design of the program. It's much more logical to declare gcd function in global scope, not in main function scope. Because then you can use gcd function in other global functions and in other cpp files (if you have a multidocument project). It's not a very clever design to declare everything into main function scope.
What I had in mind is that C++ would have a 'reassign'/similarly named command, where such a term preceeds the variable to overwrite the value of. Say...reassign x -x. But just x = -x in proper algebra isn't reassigning; it's giving additional information on the value of x, further assigning and specifying a more strict range of values. But duh on that, just clarifying. Alright; I'll live with this then. I'll say if (GCF < 0) {GCF = -GCF;}. I have no complaint now.

And it's just, I'm not sure how you can define gcd as a function ("int gcd() {[...]}") by using gcd in it ("gcd(b, a % b);"). If I try to say d = gcd(a, b) in the main function of the program, why does it consider gcd an undeclared variable there but not in the gcd function? How can you define the gcd function by using gcd in it? It just doesn't make any sense to me.
 

Garstyciuks

New member
This concept is called recursion. It calls itself with different parameters, until some condition is met, and then it returns some value. In this gcd function, it's called tail-recursion. And the code I posted with a while loop is a conversion from tail-recursion function. A function with a while loop performs faster than a function with recursion, because recursion has some calling overhead. Though most compilers are capable to automatically optimse tail-recursion into a simillar while loop. If you get some compiler error, then please show us the code, so we can tell what's wrong.
 
OP
Iconoclast

Iconoclast

New member
For the tenth time, I know that you are using the method of recursion. The tail-recursion and fact that the 'while' term was what's causing this loop is what I didn't know.

More than anything, I want to know why I can't use gcd(x, y) in the main() function (since Dev-C++ recognizes gcd as an undeclared variable), but I can use it in a newly-created gcd() function, where this function defines the gcd() function as gcd(b, a % b), which is using gcd in itself.
 

Garstyciuks

New member
I think that you still don't understand the recursion . Read up a bit on it and how it is used in C/C++. Function calling itself in itself is called recursion. The definition of a function is the first line of it. (int gcd(int a, int b)) But gcd(b, a%b) is not a redefinition of that function, it's the call to itself. To see how that works, try to manually follow the execution function with some a and b values.

gcd must be defined somewhere above the main function, so it can be used in main function.

Code:
#include <stdio.h>

int gcd(int a, int b) //this function must be defined before being used (above main function)
{
    if (b == 0) return a;
    return gcd(b, a%b);
}

int main(int argc, char **argv)
{
    printf("gcd(6,9)=%i\n",gcd(6,9));
    return 0;
}

If you want to define gcd function after the main function, you must declare it by writing int gcd(int a, int b); somewhere above main function.
If you don't like printf, then change it into cout. I prefer printf.
 
Last edited:
OP
Iconoclast

Iconoclast

New member
Then yes I don't understand this recursion, which I will read up on later, but what does...'recursion' have to do with my second question?

In the gcd function you have defined above the main() function, you are basically saying...

"To define gcd as a function so that the compiler does not recognize it as an undeclared variable, this will be my definition:

When I mention gcd in the main function, if b is 0, return the value of a for the function.

If b is not 0, should gcd be mentioned, find the gcd of a and b."

You are defining gcd...using the term itself within the definition. It's like defining the word 'annoying' as 'something that is annoying'. True, but just...wrong, you know. Now stop ignoring my question..

No offense, I'm just getting a little frustrated at loops in repitition of asking the same unanswered question over and over again.
 
Last edited:

Exophase

Emulator Developer
For the tenth time, I know that you are using the method of recursion. The tail-recursion and fact that the 'while' term was what's causing this loop is what I didn't know.

More than anything, I want to know why I can't use gcd(x, y) in the main() function (since Dev-C++ recognizes gcd as an undeclared variable), but I can use it in a newly-created gcd() function, where this function defines the gcd() function as gcd(b, a % b), which is using gcd in itself.

This was already addressed earlier in the thread. Pay more attention. I'll repeat it anyway.

It must be because the location of the main function is preceeding that of the gcd function. The C compiler is not aware of the gcd function because it has not yet seen it in the code (C is a notoriously single pass language).

To fix this you must do one of the following two things:

- Define a prototype for the gcd function before it is called in main. A prototype is the same as the header of a function, but does not include the function body. Instead, place a semi-colon immediately after it.

- Define the gcd function at a higher line number than the main function.
 

Slougi

New member
Then yes I don't understand this recursion, which I will read up on later, but what does...'recursion' have to do with my second question?

In the gcd function you have defined above the main() function, you are basically saying...

"To define gcd as a function so that the compiler does not recognize it as an undeclared variable, this will be my definition:

When I mention gcd in the main function, if b is 0, return the value of a for the function.

If b is not 0, should gcd be mentioned, find the gcd of a and b."
Yes, exactly

You are defining gcd...using the term itself within the definition. It's like defining the word 'annoying' as 'something that is annoying'. True, but just...wrong, you know. Now stop ignoring my question..
It is not wrong. This is used in "real" mathematics as well. Here is how to define it in mathematical terms, given that you have already defined a function MOD(a, n) that returns the remainder of a divided by n:
gcd.png


Similarly, but with slightly different notation, Fibonacci series are defined as follows:
Code:
F(0) = 0
F(1) = 1
F(n) = F(n − 1) + F(n − 2)

No offense, I'm just getting a little frustrated at loops in repitition of asking the same unanswered question over and over again.
What question exactly is left unanswered?
 

Garstyciuks

New member
You are defining gcd...using the term itself within the definition. It's like defining the word 'annoying' as 'something that is annoying'. True, but just...wrong, you know. Now stop ignoring my question..

I already told you that the gcd(b, a%b) is a call to itself, not it's definition. This is the concept of recursion. Please read up on recursion now, because your statements are completely false. And I have already showed you a version of this function without recursion, but with a while loop. Maybe it will look a bit clearer to you.

A better word definition for recursion is in Cyberman's signature:
recursion - see recursion.
Except that recursion in computers should always have some condition for it to stop.

Your questions were answered for a lot of times, so please focus your attention while reading.
 
OP
Iconoclast

Iconoclast

New member
Okay, you know what, that's really all I was asking: gcd() will not work because it comes after the line containing "main()". gcd must be used in a prototype outside of the main function. Although, now my question derived from that is, why can't it just work within the main function? Don't answer; I'll read up, like I said I would. I just need to use my time on other things first for now. But if those tutorials on cprogramming.com don't mention the answer, I will not hesitate to restate what I fail to understand.
I already told you that the gcd(b, a%b) is a call to itself, not it's definition. This is the concept of recursion. Please read up on recursion now, because your statements are completely false. And I have already showed you a version of this function without recursion, but with a while loop. Maybe it will look a bit clearer to you.

A better word definition for recursion is in Cyberman's signature:
recursion - see recursion.
Except that recursion in computers should always have some condition for it to stop.
Which statement(s) were false? According to Slougi, I have said nothing wrong in my barely-educated 'hypothesis'.

But what I'm not understanding is, how can something such as division start a loop? Of course, again, I'm sure I'll find that out when I get back to the tutorials. I am just restating my point of confusion...yet again, so you do not again overestimate the simplicity of my questions.

Your questions were answered for a lot of times, so please focus your attention while reading.
I have spent from 5 to 60 minutes analyzing the four of you programmers' responses. Do not criticize my disabilities.
 
Last edited:

Cyberman

Moderator
Moderator
I have spent from 5 to 60 minutes analyzing the four of you programmers' responses. Do not criticize my disabilities.
Everyone has disabilities, deal with it. These people are trying to help you, and yet you are verbally sparing with them. Go and do something for a while instead of using poisonous stilted language because you don't like and or understand what you read. It takes time to understand a lot of things, don't expect to understand recursion simply by reading a few words (nothing magical will happen until you make the mental connection of what it is.) Everyone has heard things that are not readily understood; instead of arguing about it try reasoning through it. So many wouldn't say nigh unto the same thing unless there was a reason for it. I haven't seen anyone talk down to you until you had already done so in kind.

No one has mentioned disabilities. I am not interested in wither you have disabilities or not nor about your age. The attitude you present yourself with is the only thing people are not particularly happy with. So smile and try thinking about how you would like to be treated, then do that to other people, unless of course you would like to be belittled and denigrated?

As for recursion perhaps you should hit wikipedia on that, it has a fairly good (although imperfect) article on it. Recursion is a very vast subject that encompasses several mathematical sets (see fractals such as the Mandelbrot and Julia sets) and numerous disciplines. It also happens to be an extremely important concept in physics when dealing with turbulence and complex fluid dynamics simulations (aka CFD). Mandlebrot wrote a very interesting book on fractals that discussed recursion in depth as part of his observation on these mathematical sets. I recommend looking into information regarding Mandlebrot, he was a scientist who worked for IBM (computer information systems).

Garstyciuks
See I sometimes have useful things! ;)

Cyb
 

Toasty

Sony battery
Sometimes a picture is worth a thousand words:
View attachment 36564
This picture shows the flow of execution in the recursive function described earlier. There are several 'copies' of the function that get called, each with a more 'refined' set of variables. Finally, the right conditions are met and the answer is returned back up through the stack of function calls. Keep in mind, there is only one GCD function, but multiple calls to that function. The same exact function gets called each time, just with a different set of variables.
 
OP
Iconoclast

Iconoclast

New member
Thanks to Toasty, sethmcdoogle, Exophase for that one post, and of course Garstciyuks (bleh..) for all being patient. I have read up and understand now this matter.

Starting Friday, only the final exams portion of school will remain (no homework or turning it in), so my time on learning this language (C# too, maybe C...like, after I attempt to learn every other language) will be months. Again, I thank you all for your aid.
Everyone has disabilities, deal with it.
How do you intend? To deal with is to live with.

But let me tell you, at least I don't have the disability of being a trollish moderator. You obviously fail to learn: I won't listen to a damn thing you got to say to me if it's part of a post of prejudice, insult and accusation. How am I destroying everything around me? There's only one overreaction I did in this thread, and that's getting impatient with the idea that my questions were being ignored. That's my disability, and for dealing with it, what do I get? Another schizophrenic/paranoid demonstration from you.

You want to reason things with people? You need to understand that, everyone has disabilities, and tolerate them. For every bad trait in someone, there is a virtue that covers up for it. You need to take their disability into heart and gently work around it. You can get a lot of reasoning done that way. What you have posted here was not kind and persuasive: It has accused me of attacking them (which I never did) and arguing with them (which actually this time I did not do...heh) and taking your issues with me in the past and involving them here (or else you wouldn't have accused me of arguing in this thread). This is not arguing; this is defending myself from your overly-human, red-minded brain, that perceives attacking me as trying to help me.

I have suffered to every dimension of not only my learning disabilities, but my family life, and you, with unpunished arrogance, decide to go to the method of 'persuasion' that you know from the past...pisses me off, and see how much that teaches me. You came here to troll me; needless to say, you got what you wanted.

So, since this is your third post directed at me of...unusual length, I think I'll just stop compressing my posts.

These people are trying to help you, TRUE
and yet you are verbally sparing with them. FALSE
It takes time to understand a lot of things, don't expect to understand recursion simply by reading a few words TRUE
Everyone has heard things that are not readily understood; instead of arguing about it try reasoning through it. WHAT ARGUMENT?
So many wouldn't say nigh unto the same thing unless there was a reason for it. ...
Yet again, I did only one insult to them, and that was when it looked like was calling Garyadawhatshis 'annoying', although actually in truth I wasn't. Sorry about the perception that I was. For some odd reason, the word came to mind coincidentally: I was annoyed at an infinite loop of what I felt like not having my questions answered but bypassed every time, and I was annoyed at him, but I know he's got tolerance and patience, and I would not mean to call him annoying. Yeah, I'm retarded; didn't see what they were all trying to tell me sooner until I read about loops today, so what? You going to say I'm attacking them for that reason, too?

And I'll show off my excessively-exquisite spelling, grammar and vocabulary, just to see what kind of crap you conjecture about me.

No one has mentioned disabilities. I am not interested in wither you have disabilities or not nor about your age. The attitude you present yourself with is the only thing people are not particularly happy with. So smile and try thinking about how you would like to be treated, then do that to other people, unless of course you would like to be belittled and denigrated?
There you go again: you hate arrogance so much, not only does it make you act arrogant (by saying, "I don't care this," "I don't care that" about you), but you think that just because I disliked being told to focus when I already was actually meant that I was showing off the fact that I had a disability? Listen, I don't care...what you care about me, everything or nothing, but I care about you. I care if there's something about your background making you act this way, and I care if you think this is persuasion because words are automatically persuasion just because you're the one speaking them.

Garstyciuks
See I sometimes have useful things! ;)
Okay, showoff, just make sure you accuse me of being that, too, next time....

Like everyone else, you post information. They know info you don't; you know info they don't. So make yourself king already.

But you know, I'm sure you won't even look at this post for a second before you slam that Reply button (maybe even the "ban user" link) at bitch velocity (first with your hand, THEN the mouse), and your computer will go flying with it. Course, you'll still be typing, won't you? I'm just going to go ahead and assume, like you do, that you will do just that (even though I know inwardly you do have some generosity and reasoning in you I'll admit) and that your reply is going to say something that answers questions I never even asked (or even thought about), like your programming experience, and pretend to add you to my ignore list. Good day, sir.
 
Last edited:

Slougi

New member
Wow. What should have been a trivial question has devolved into a small flame war. Good grief people, grow up. Act civil.

Locked.
 
Status
Not open for further replies.

Top