Comprehend or Die trying in C++

IMO, C++ isn't a beginner language. If you are just starting to learn programming to implement algorithms, language constructs should not be the main focus. It should be easy so you can use them readily to develop your algorithm implementation.

Then once you are ok and comfortable with algorithm implementation, you can then opt for "harder" languages like C, C++. Algorithm remain the same but programming language changes. At least if I am to teach new programmers wannabe, I would start off with an "easier" language and then gradually tuned them to "harder" languages which provide you access to low-level features.

Starting off with a "hard" language can demoralize a newbie very fast. They will be so keen to develop algorithm implementation and then before they hit that objective, they are bogged down by pointers, memory never release, leaking etc such issues. This take the real focus away from the actual algorithm implementation.


The facts now straighten out. I'm demoralized, but to prove that lazy teacher that I'm not an idiot is causing me to become stubborn. I hate to lose. I love a challenge, but confusing and hand feeding me a whole code without explanation just - aggravates me more.

I want to learn programming, I hate the damn thing with a passion, but I am determined to get it done.

I understand it, but I can't implement the codes correctly, so my question is, where or who can learn/teach (me) everything there is to know about programming. I'm willing to learn even if its kills me.
Last edited on
closed account (Dy7SLyTq)
ok..... and why isnt this in the lounge?
This would be a good place to start if you want to learn some basic programs.

http://www.cplusplus.com/doc/tutorial/
I found learncpp.com tutorials noobie-friendly in my time.
...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.)
closed account (Dy7SLyTq)
honestly lisp is really easy. just make a file with a bunch of ()'s. there you go
Honestly dude, the best programming language to learn for a beginner is python.

Here is example why:
1
2
3
4
5
6
7
8
9
10
#python code:
a = 5
b = 7
if a < b:
    print "a is less than b"
else:
    print "a is not less than b"
// equivalent c++:
#include <iostream>
using namespace std;

int a = 5;
int b = 7;
if (a < b)
    cout << "a is less than b\n";
else
    cout << "a is not less than b\n";
Last edited on
Python does a lot of bookkeeping by automatically importing standard I/O modules and, as an interpreted language, not requiring a main function. If you really want to compare them, it would be fair to compare the code:

1
2
3
4
5
6
a = 5
b = 7
if a < b:
    print "a is less than b"
else:
    print "a is not less than b"
int a = 5;
int b = 7;
if (a < b)
    cout << "a is less than b\n";
else
    cout << "a is not less than b";

Nearly identical, no?

Any python program more advanced than that will require you to become familiar with modules and the import statement, just as you have to become familiar with compiled modules and include directives in C++.

Python is sufficiently similar to C++ in syntax that I did not include it in the list already given above.

And, the OP did not ask to learn another language. He was bothered by the idea that C++ is a "harder" language -- which it is not. My example shows all languages to be just as difficult (excluding those designed to be difficult, of course).

1
2
3
4
5
6
7
8
9
10
11
Python

def LCM( a b ):
  am = a
  bn = b
  while am != bn:
    if am < bn:
      am += a
    else:
      bn += b
  return am

Just as difficult/easy to read as the others.
closed account (Dy7SLyTq)
+1 for python. and @devon: the difference is you didnt make a full c++ program. and yes it might seem easy at that low level, but try a much more difficult task. python will allow you to do it faster. the downfalls of doing it in python is that its interpreted so it will be slower than equivalent c++ code and it will be harder to find bugs. however it does a bunch of the grunt work for you (which is one of the advantages of c++: interfacing at a lower level but because of that you will have to do more work to get to that level.) try making a webcrawler in python and one in c++ and tell me which was easier
Python is easier to whip up small scripts in but is much more difficult to debug and a lot easier to shoot yourself in the foot.

Dynamic typing sounds nice and easy on paper, but in my experience it makes things more complicated... because the idea of different types is still there, it's just now the job of keeping track of types is forced onto the programmer rather than on the language.

EDIT: For certain tasks, anyway. There are some situations where dynamic typing makes things worlds easier... dealing with databases comes to mind immediately... /EDIT

In a way I can see how it'd be good for a beginner to start with, because it is very easy to do very simple things. But at the same time Python's oversimplification will probably make learning other concepts more difficult further down the road.
Last edited on
Thanks for the boost of confidence and information guys.
Topic archived. No new replies allowed.