Fork me on GitHub
Math for the people, by the people.

User login

C++

Synonym: 
C with Classes
Type of Math Object: 
Definition
Major Section: 
Reference
Groups audience: 

Mathematics Subject Classification

68N15 no label found

Comments

Thanks to Michael Slone and Steve Cheng for fixing the source code listings in the C and C++ entries. Yesterday I was trying several different things to get the source code to show up and none of it was working. I've been having trouble with the Web lately, I don't know why. Funny thing is, the day before yesterday I got C and C++ programs for GCD to work after just two tries each. The Javascript program took several dozen tries to get it to work, and this after I got C and C++ to work.

Also thanks to Steve Cheng for adding the data type generalization for gcd(x, y) in the C++ example. Of course I had to try compiling it on my computer. The first try had 19 errors (things like calling "cout" not a class object) and I forgot how many warnings, but that was only because I failed to notice that the pound sign had gotten transmogrified into something else along the way. The second try had just 3 errors, so I added some semicolons and lowercased "GCD" in main(). (I read somewhere that in Standard C++ you can omit semicolons for lines right before ending braces, but I can't recall where, but it wouldn't surprise me one bit if Microsoft's compiler wasn't entirely standards-compliant). On the third try, the program compiled successfully and gave all the right results for my customary test pairs, including (42, -21).

For (-21, 42), Steve's GCD program gives -21, which I think is the right answer (I don't have any rigorous mathematical reasoning for that, nor does Mathematica back me up on this: even for (-42, -21), Mathematica gives the answer as (+)21).

> Also thanks to Steve Cheng for adding the data type
> generalization for gcd(x, y) in the C++ example. Of course I
> had to try compiling it on my computer. The first try had 19
> errors (things like calling "cout" not a class object) and I
> forgot how many warnings, but that was only because I failed
> to notice that the pound sign had gotten transmogrified into
> something else along the way. The second try had just 3
> errors, so I added some semicolons and lowercased "GCD" in
> main().

Sorry about the mistakes in the program. When editing the article, I had copied-and-pasted the code from the wrong window (which had the typos that I fixed in the actual program I compiled with gcc).

I'm also thinking of writing an example of computing the gcd of polynomials to show how polymorphism works in object-oriented languages.

By the way, the code is indented improperly; this doesn't happen if I LaTeX the article on my computer. Anyone know the solution?

// Steve

// Steve

By the way, the code is indented improperly; this doesn't happen if I LaTeX the article on my computer. Anyone know the solution?

I know of a solution for smallish examples. PrimeFan successfully put a simple example of a while loop in the font sandbox that is indented nicely enough by the use of \flushleft and \quad. But any source code longer than that (like the gcd program) using \quads seems to be doomed to rendering failure.

Subscribe to Comments for "C++"