Click here to Skip to main content
15,747,908 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
a0 = a; b0 = b;
x1 = 1; x2 = 0;
do
if (a > b)
{ q = a / b; a %= b; t = x2; x2 = x1 - q * x2; x1 = t;
if (a < 1) { gcd = b; x = x1; y = (b - x * a0) / b0; break; }
else
{ b -= a; t = x2; x2 = x1 - x2; x1 = t;
if (a == b) { gcd = a; x = x1; y = (b - x * a0) / b0; break; }
} }
else
{ q = b / a; b %= a; t = x1; x1 = x2 - q * x1; x2 = t;
if (b < 1) { gcd = a; x = x2; y = (a - x * a0) / b0; break; }
else
{ a -= b; t = x1; x1 = x2 - x1; x2 = t;
if (a == b) { gcd = b; x = x2; y = (a - x * a0) / b0; break; }
} }
while (true);


What I have tried:

a0 = a; b0 = b;
x1 = 1; x2 = 0;
do
if (a > b)
{ q = a / b; a %= b; t = x2; x2 = x1 - q * x2; x1 = t;
if (a < 1) { gcd = b; x = x1; y = (b - x * a0) / b0; break; }
else
{ b -= a; t = x2; x2 = x1 - x2; x1 = t;
if (a == b) { gcd = a; x = x1; y = (b - x * a0) / b0; break; }
} }
else
{ q = b / a; b %= a; t = x1; x1 = x2 - q * x1; x2 = t;
if (b < 1) { gcd = a; x = x2; y = (a - x * a0) / b0; break; }
else
{ a -= b; t = x1; x1 = x2 - x1; x2 = t;
if (a == b) { gcd = b; x = x2; y = (a - x * a0) / b0; break; }
} }
while (true);
Posted
Updated 4-Apr-21 1:03am
Comments
Richard MacCutchan 4-Apr-21 7:08am    
It does lots of things with lots of numbers.
jeron1 4-Apr-21 10:46am    
LOL That it does!
Dave Kreskowiak 4-Apr-21 13:10pm    
If you haven't figured it out yet, nobody is going to do your homework for you.

This assignment is a test of what YOU know, not what other people know.

Quote:
Describe this algorithm in C++

You code is specially crafted to be unreadable.
Saving key stokes is a bad idea, it makes only code more difficult to read and understand.
Try a Best C Formatter and Beautifier[^]

Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
a0 = a;
b0 = b;
x1 = 1;
x2 = 0;
do {
	if (a > b) {
		q = a / b;
		a %= b;
		t = x2;
		x2 = x1 - q * x2;
		x1 = t;
		if (a < 1) {
			gcd = b;
			x = x1;
			y = (b - x * a0) / b0;
			break;
		} else {
			b -= a;
			t = x2;
			x2 = x1 - x2;
			x1 = t;
			if (a == b) {
				gcd = a;
				x = x1;
				y = (b - x * a0) / b0;
				break;
			}
		}
	}
	else {
		q = b / a;
		b %= a;
		t = x1;
		x1 = x2 - q * x1;
		x2 = t;
		if (b < 1) {
			gcd = a;
			x = x2;
			y = (a - x * a0) / b0;
			break;
		} else {
			a -= b;
			t = x1;
			x1 = x2 - x1;
			x2 = t;
			if (a == b) {
				gcd = b;
				x = x2;
				y = (a - x * a0) / b0;
				break;
			}
		}
	}
}
while (true);

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
 
Share this answer
 
Quote:
Describe this algorithm in C++

Er ... no.
Why not? Because as Patrice has said, that is unreadable. Partly it's the lack of indentation he mentioned, partly it's the single character names for all variables, partly it's the complete lack of any documentation.

But mostly it's because all of those things combine to make code I don't even want to look at, much less try to understand, or work out if it does what it should, and if so what the heck that actually was.

You want people to read your code? Make it readable.
Indent it correctly.
Use sensible variable names that describe what the variable is used for.
Comment code where it's not obvious why you are doing something, so that it explains why - not what it's doing, but why it's doing it.

And that includes you: when you come back to this in a few months time you will go "WTF?" and have no idea what it does - or any interest in finding out ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900