slight issue with a greatest common divisor calculator

Hello everyone. Someone gave me the following program that calculates the greatest common divisor of two positive whole numbers (posted below). I understand why the program works if I enter two numbers that aren't equal. So if I enter 10 and 15, I understand why it outputs 5. However, the program works even if I enter two numbers that are equal, and I can't figure out why. If I input 10 and 10, for example, then in the g_c_d function neither the if nor the else if block are entered. So the computer shouldn't know what value of "low" is, since low hasn't been given a value. So when I entered 10 and 10, I was expecting to get nonsense output or something. However, it correctly outputs 10. Can somebody explain why? Thanks!


#include <iostream>

/*Determines greatest common divisor*/

using namespace std;

int g_c_d(int x, int y); //function g_c_d with 2 integers

int main(){

int input1;
int input2;
cout<<"Enter a whole number"<<endl;
cin>>input1;
cout<<"Enter another whole number"<<endl;
cin>>input2;
cout<<"The greatest common divisor is"<<endl;
cout<<g_c_d(input1, input2); //outputs function determined below

return 0;
}

int g_c_d(int x, int y){

int low;
int high;
if (x>y){
low=y;
high=x;
}
else if (y>x){
low=x;
high=y;
}
while (y%low!=0 || x%low!=0){
low--;
}
return low;
}
if i enter the following code and run it i get an error of low being used without being intitialized which you should be getting and you were correct in your assumption that that would happen. as to why you are not getting the error i do not know maybe you have >= or <= sign in there instead of the > or < which would then output the correct answer
If it gives the correct answer you are lucky. Both high and low are uninitialised and may contain any random value.

If low is larger than both x and y, it will be decremented repeatedly until it is equal to x or y. That gives the answer you want.

If low is smaller than both x and y, then the wrong answer is returned.

And if low happens to be zero, you get a divide by zero error and the program will crash.
Last edited on
If it gives the correct answer you are unlucky
@ tag and Chervil: I copied an pasted this program, so it looks exactly like that in my compiler. So I don't know why it's not giving me an error. Like I say, I have input 20 and 20, and it outputs 20. I've put in 15 and 15, and it outputs 15. So I'm still not sure why it behaves properly.
what compiler are you using?
Xcode
strangelove1221 wrote:
So I don't know why it's not giving me an error.

Statistically, if the value of low is chosen at random, and both x and y are quite small, the probability that it will give the right answer but for the wrong reason is very high.

I originally said it was luck that gave you the right answer.
ne555 pointed out that it was in fact bad luck if it gives the right answer. Why? because it means the bug goes undetected, but is still there, waiting to bite you.

Some compilers will issue a warning when a variable might be used without being initialised, if so, pay attention to that warning. In any case, it's good practice to give all variables an initial value.
Last edited on
I understand now, thanks. So if I haven't assigned a value to low but I start to use it as if it has a value, then the compiler will assign it a random value?
Not exactly. The compiler allocates a particular memory location to hold the contents of the variable. If it is not initialised, whatever value is already in that memory (from some previous use) will be interpreted as its value. It's not a good idea to use this method to generate random numbers.
Topic archived. No new replies allowed.