...and to start, all that stuff is drivel. Your teacher is arrogantly asserting his (poorly formed) opinion on people who don't know better.
Programming isn't about syntax, but as a beginner, you must concern yourself with syntax at the same time you learn to program. The trick is, of course, that
the exact syntax doesn't matter.
Here are some equivalents in a variety of languages to calculate LCM:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
C++
int LCM( int a, int b )
{
int am = a;
int bn = b;
while (am != bn)
{
if (am < bn) am += a;
else an += b;
}
return am;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Object Pascal
function LCM( a, b: integer ): integer;
var am, bn: integer;
begin
am := a;
bn := b;
while am <> bn do
if am < bn
then am := am + a
else bn := bn + b;
result := am
end;
|
1 2 3 4 5 6 7 8 9 10 11 12 13
|
Tcl
proc LCM {a b} {
set am $a
set bn $b
while {$am != $bn} {
if {$am < $bn} \
then { incr am $a } \
else { incr bn $b }
}
return $am
}
}
|
1 2 3 4 5 6 7 8
|
scheme
(define (LCM a b)
(letrec ((lcm (lambda (a b am bn)
(cond ((< am bn) (lcm a b (+ am a) bn))
((> am bn) (lcm a b am (+ bn b)))
(else am)))))
(lcm a b a b)))
|
All these function identically. The
algorithm is:
while a*m is not equal to b*n:
if (a*m < b*n)
then m += 1
else n += 1
result is a*m
As you can see, the syntax varies -- sometimes little, sometimes a lot. The underlying algorithm is the same.
In order to actually
implement the algorithm, you must comply with the syntax of the language you are using. Which of the above looks like it is more or less easier than any of the others?
(Since you are studying C++, that one might be easier on your eyes, but that is only because you have been studying it.)
It is a logical fallacy that people need to learn "easier" before they learn "harder". And unfortunately, the actual fact is that if things are dumbed down ("easier") in the beginning, that it increases the amount of time it takes to learn the "harder". This is a well-known pedagogical principle.
And that stuff about pointers and the (doubly maligned) memory leaks is also a straw-man, designed to do nothing more than augment his argument with FUD.
Learning to program is learning to think about how things are done.
For example, which of the following is correct?
1 2 3 4 5
|
// A
string s;
cout << "What is your name? ";
getline( cin, s );
cout << "Hello, " << s << "!\n";
|
1 2 3 4 5
|
// B
string s;
getline( cin, s );
cout << "What is your name? ";
cout << "Hello, " << s << "!\n";
|
1 2 3 4 5
|
// C
string s;
cout << "What is your name? ";
cout << "Hello, " << s << "!\n";
getline( cin, s );
|
If you said A, then you are already a programmer, because you recognize that things must occur in a particular order.
1 ask the user a question
2 (wait for and) get an answer to the user's question
3 use that answer to say something to the user
It wouldn't make sense to do B (get answer, ask question, use answer) or C (ask question, use answer, get answer).
As a beginner, thinking your way through a problem involves breaking that problem down into pieces that you know how to do with the syntax you have learned -- the two go hand-in hand. This is hard. But it comes with time and patience.
Of course, if I have misunderstood you and you are taking a 300-level Algorithms course, then you should already have at least one programming language under your belt. None of your homework should need to employ any actually programming language, though.
Hope this helps, and gives hope. Not all instructors are created equal, and unfortunately, beginning-level instructors are a little less equal than some of the higher-level instructors.
(BTW, I do know that you can calculate LCM using GCD.)