Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

I am having a hard time learning to program What to do?

  1. Sep 23, 2008 #1
    I bought a book on C++ that was recommend by a friend and a cheap book on C and I just can't get into it.

    It start off ok, but then the throw 50 lines of code at you for even a basic program and I get lost...

    I don't know what any of it means and I feel like bashing my head in.

    Any suggestions?
  2. jcsd
  3. Sep 23, 2008 #2
  4. Sep 23, 2008 #3


    User Avatar
    Science Advisor
    Homework Helper

    C++ is a very involved complex language to learn programming with.

    Either start with one specifically designed to teach beginning programming like mal4mac suggested.

    Or you can learn a general programming langauge but pick a more modern easier one, I would recommend python. Python isn't just easy to learn, it is widely used for real programming.
    There is a beginners book and examples free at http://diveintopython.org or if you might like http://openbookproject.net/thinkCSpy/index.xhtml it's also free but aimed at more advanced students.

    Rememebr there is more to programming than knowing a particular language. Programming is about describing the problem to a computer, once you know the techniques for doing this most langauges are pretty similair.
    Last edited by a moderator: Apr 23, 2017 at 3:55 PM
  5. Sep 23, 2008 #4
    I recommend C++ Primer Plus by Prata. It's an easy book to learn from, and wordy.

    The fact is many CS programs start you off with either java or c++, so you might as well learn the syntax. You can learn about variables, pointers, control structures, arrays, array lists, and recursions, starting off with C++. Plus you'll learn how the computer manages the data, a very important part of CS, or "Datatology" as some call it.

    The hardest part in the beginning CS courses is coming up with your own algoritms for projects, mapping them out, writing the pseudocode is often the most difficult part. Once that is done you should be able to convert it to the language.

    As you go along, do some exercises in the book and try and make a few programs that are a little bit harder than what you're used to. That's a good way to move forward.
  6. Sep 23, 2008 #5
    C and C++ are where you want to start if you're looking to get into serious programming. They're low level enough so that you have to deal with memory management, various data types, and other nitty things. It gives you a very good understanding of how computers work under the covers, but makes you jump through a lot of hoops to do what you might think are simple tasks.

    By contrast, things like Perl and Python are very powerful languages. You can do the same tasks with single built-in function calls, rather than writing tons of very detailed instructions. The other benefit to Perl and Python specifically is that they're not compiled. On a personal note, compiling was the thing that drove me away from C (and I guess Java too). So you write it, you run it. Skips the whole compiling phase. Plus, you can do funky things like running new code on the fly with "eval".

    For example, if you're programming with an array in C, you have to tell the compiler how big that array is going to be in advance. Oh, and you have to tell it how big each slot in the array is, as well, and what data type is going to be stored there (one and the same, usually). So if you're not sure if you're going to need 10 slots or 5000 slots, you're going to have to either declare the full max right up front, or dynamically allocate new memory on-the-fly as your array grows. In Perl? You don't need to do any of that. Perl's smart enough to handle it for you. Python, too, I assume. Technically, you don't even need to declare the variable (although you should!)-- you just start using it and it's there for you.

    The sacrifice is processing speed. If you try and do any intense data processing, like, say, ray-tracing a 3D image or extrapolating out a mathematical formula to billions of iterations, it'll be much faster in C, C++. But 9 times out of 10, you don't care about that for hobby-style programming. Having your programs be easy to build and easy to edit is far more desirable.

    Myself, I'm a Perl guy. But Perl may be losing its edge to Python, given that I see more about Python these days than Perl. Maybe because Perl 6 is such a change from Perl 5-- maybe because Perl 6 took so long to release, I dunno. Anyway, I guess I'd have to recommend Python. But Perl's still cool too :)

    Last edited: Sep 23, 2008
  7. Sep 23, 2008 #6
    davee123 makes some really good points. Obviously, it's hard to use arrays especially for basic stuff. I made a bowling score program (all it does is count the score) in C++, completely procedural, and I probably could have done it easier in Python. Plus, I had a roll count that associates a current roll with each frame so I could add in the next two rolls, and I had to use the maximum number of rolls possible for my array. In python, I probably could have made memory management a bit easier.

    With variables as well it is easier. For example, I can write x = 100 and it'll probably be an int or x = 100.12 and it will be of a floating-point type. Plus python has built in abilities to handle large numbers. So yes, it is easier.
  8. Sep 23, 2008 #7
    Oh yes-- excellent example. I forget how C/C++ handles it, but if you want to multiply 1000 * 5.25, and assign the value to an integer, it may give you the answer of 5000 rather than 5250, since the floating point "5.25" may get converted to an integer before doing the calculation. I forget all the details, but I remember having issues with things like that. Similarly, a normal "int" is what, 4 bytes? So if you multiply 1,000,000 x 6580 in C, you'll get some wacky answer, because the maximum size of a normal integer is 2^31 (that 1st bit is for the sign).

    So in C/C++, you've got to worry about all those things. In your head, doing math is easy-- a number's just a number, and that's more-or-less how Perl and Python handle things. But in C/C++, an integer is different from a short is different from a double is different than a float, etc. So you're bound to run into problems sooner or later, where how you thought about it in your mind turned out to be vastly different than how the compiler interpreted your code.

  9. Sep 23, 2008 #8
    The real reason I was looking into C an C++ is because I need something versatile and it seems like anything can be done in C++
  10. Sep 23, 2008 #9


    User Avatar
    Science Advisor
    Homework Helper

    C/C++ can also be used everywhere, from 50c washing machine controllers to supercomputers.
    But with available libraries Python, Java and C# can also do almost everything (except write an operating system!) but they are more limited in where.
    Python and Java will run on almost anything, C# is largely windows only (there is an unofficial Linux clone)
  11. Sep 24, 2008 #10


    User Avatar
    Homework Helper

    It's not so much the language, but the complexity of the first programs you try to write. Here it's the training book that makes the difference. Perhaps an online class via some junior college might be good, but that also can vary between classes.

    For some, it would help to understand how a computer works. Being able to step through the instructions one at a time while looking at the register and memory content would be useful. A typical debugger comes close to being able to do this, but a learning tool would be better.
  12. Sep 26, 2008 #11


    User Avatar

    I do agree with you. C and C++ language is a power tool. If you have ever leant about the computer structure and assemble language, you will understand that C/C++ useful.

    I suggest that you can view some video tutorials on the net, such as youtube, or MIT open cource. I'm not sure where the 50 lines code come from, but I can recall my first c code was "print("hello word");":smile:
  13. Sep 29, 2008 #12


    User Avatar
    Homework Helper

    That's some very informative advice there, davee123
  14. Sep 29, 2008 #13
    Using C++ to learn programming is a bit like learning to drive with a Formula 1 car!
  15. Sep 29, 2008 #14
  16. Sep 29, 2008 #15
    A few people have suggested starting off with a simpler language, which I agree with. I really liked learning all of my basic programming concepts in http://en.wikipedia.org/wiki/Logo_(programming_language)". Whoa, that was a long time ago.

    granpa's suggestion of javascript seems like a good idea too.
    Last edited by a moderator: Apr 23, 2017 at 4:04 PM
  17. Nov 11, 2008 #16
    Every body has given good advice, but I am surprised to see no one mentioned "BASIC" language. It can be learned in a single day. Most of the things that davee123 has written for python and perl, do apply to basic. I'd recommend QBASIC for anybody who is starting with programming.

    I'll enlist some of the features of the QBASIC here:

    You donot need to compile
    No need to define variable size, or simply you could skip defining variables
    While you are programming, code examples can quickly be consulted using build in help
    Easy serch to looup library functions
    QBASIC programs can be executed in MS-EXCEL as macros with no or little modification. (Which is very easy as you donot need to install anything)
    Good online help is available like

    Code can easily be converted to executeable file (.exe)
    Run time enviornment requieres no installation/path-setting what so ever.

    Slower in speed (Tolerable for hobbey and learning programs)
    Not objest oriented.
    Working enviornment is dos like.

    QBASIC version 4.5 is best and can be googled to download.
    Last edited by a moderator: Apr 23, 2017 at 9:54 PM
  18. Jun 27, 2011 #17
    Basic used to be the thing but the language that's now available to everyone is JavaScript. It works in any browser and every machine (even a phone) has a browser!
    For a beginners course see http://www.grelf.net/jscourse
  19. Jun 30, 2011 #18
    it always hard on the first time, start from the easy one . honestly i'm start from batch (.bat) hehe , go to python => Java => and now on C++. Actually i'm just learn python for getting the basic understanding about programming .. , once you get that basic you'll get the fun of programming language
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: I am having a hard time learning to program What to do?