I’m having problems again with my Euclid’s algorithm problem. I’m running this on MS Visual Studio as a console application. When I run it, it says “process is terminated due stack overflow exception.”
I’ve recently posted another question about this algorithm, but now I’ve got this problem and it’s the first time I’ve seen it. I’m a beginner programmer.
int algoritmoeuclides(int a,int b)
{
if (a%b==0)
return b;
return algoritmoeuclides(b,a%b);
}
std::pair<int, int>division(int dividendo,int divisor)
{
int resultado=0;
int residuo=0;
residuo=dividendo%divisor;
resultado=dividendo/divisor;
return std::pair<int, int>(residuo,resultado);
}
std::pair<int, int>EuclidesExtendido(int a,int b)
{
int q,r,s,t;
if (b == 0)
return std::pair<int, int>(1, 0);
else
{
std::pair<int, int>(q, r) = division(a, b);
std::pair<int, int>(s, t) = EuclidesExtendido(b, r);
}
return std::pair<int, int>(t, s - q * t);
}
int main(array<System::String ^> ^args)
{
int a=525;
int b=231;
int s,t;
std::pair<int, int>(s, t)=EuclidesExtendido(a,b);
printf("%d",algoritmoeuclides(a,b));
printf("s=%d t=%d",s,t);
getch();
}
You’re collecting the return value of your functions incorrectly.
Despite having two values, a pair is a single object. So
Should be something like:
And then you could get
qandrindividually with:You would need to make this sort of change everywhere that you have similar errors.