"Advanced" beginner is basically that you've done some work before and that you'd be fairly quick at picking new things up. The Stroustrup book makes you a better programmer, especially in C++, but it will absolutely make you have to work pretty hard for it. I will say that I definitely feel that I understood C++ on a much deeper level after I got through that book though. Ultimately it's only a first step, because there are further fundamentals of software that you simply don't get from an introductory course, but it's enough to put you on a solid foundation for moving forward. Self-study always comes with the downside that it's easy to get off course, learn something poorly, and never really be able to move on because you just get lost in the more advanced material.
C++ Factorial Function - Page 2
Blogs > ChristianS |
LegalLord
United Kingdom13775 Posts
"Advanced" beginner is basically that you've done some work before and that you'd be fairly quick at picking new things up. The Stroustrup book makes you a better programmer, especially in C++, but it will absolutely make you have to work pretty hard for it. I will say that I definitely feel that I understood C++ on a much deeper level after I got through that book though. Ultimately it's only a first step, because there are further fundamentals of software that you simply don't get from an introductory course, but it's enough to put you on a solid foundation for moving forward. Self-study always comes with the downside that it's easy to get off course, learn something poorly, and never really be able to move on because you just get lost in the more advanced material. | ||
ChristianS
United States3187 Posts
Yeah, I bet there's a lot of "self-taught programmers" out there that make CS professionals roll their eyes, and I guess I'm joining their ranks. I'm hoping to try to avoid too many bad habits by trying to be fairly careful in the early chapters before jumping to the other stuff. Pointers seem like a big one that make it very easy to get yourself into trouble – there's all kinds of mistakes I can make with them that the compiler won't tell me about, and a lot of them will still give the right output in most cases so I won't realize I screwed up. Maybe I'll keep writing blogs like this one so TL coders can keep me on track (if they're willing). | ||
LegalLord
United Kingdom13775 Posts
![]() In all seriousness: yeah, there's definitely a lot of people here that are fairly experienced in software. For me, it's a secondary, albeit critical, part of my job (I'm a rocket engineer first and foremost), but there's definitely a lot of people around here who are very skilled in software and do it as their primary job. Video games and software development tend to go hand-in-hand for reasons that are both obvious and not. As for pointers: a C coder would very much have a tendency to overuse those. I'd say the best way to get a handle on how to best use them is to take some form of algorithms course once you feel you have the basics of C++ down. Quora has a decent list of courses that are available; pick one that is C++ based (because C++ is the best language for that kind of study). | ||
beg
991 Posts
On August 14 2017 03:49 LegalLord wrote: I don't really like C++ Primer. No specific feedback, I just always felt whenever I was reading it that it didn't really go about teaching things the right way. CS books are generally all available online for free though, so Google the book and give it a read-through before buying if that's you cup of tea. "Advanced" beginner is basically that you've done some work before and that you'd be fairly quick at picking new things up. The Stroustrup book makes you a better programmer, especially in C++, but it will absolutely make you have to work pretty hard for it. I will say that I definitely feel that I understood C++ on a much deeper level after I got through that book though. Ultimately it's only a first step, because there are further fundamentals of software that you simply don't get from an introductory course, but it's enough to put you on a solid foundation for moving forward. Self-study always comes with the downside that it's easy to get off course, learn something poorly, and never really be able to move on because you just get lost in the more advanced material. EDIT: Damnit, you guys were talking about a book called "C++ Primer Plus". I noticed too late. That is a very different book from the legendary "C++ Primer". My original post: I love C++ Primer! It teaches the whole language in a no bullshit approach. Very academic and to the point. But also very thorough with all the details of C++. Definitely not an easy book. I already had experience with C when I read it. It's a challenge for a complete beginner. I totally agree with your advice to check out various books by googling. That's what I did too. My choice was C++ Primer ![]() | ||
bo1b
Australia12814 Posts
For what it's worth, using pointers quite a lot of the time is unnecessary over engineering, but if you're teaching yourself at home I'd try and put them in just about everything until you are super comfortable using them, as there are times when you will have to use them, and they will be complicated as hell otherwise. This includes const pointers as well. | ||
LegalLord
United Kingdom13775 Posts
I saw one example there that instantly bugged me, in like chapter 1 or 2. It was something like "well the proper way is return x+4; because its faster but if you aren't comfortable with that to int y = x+4; return y; but that's bad." Lots of little tidbit micro-optimizations that frankly don't add anything to the learning experience, but that kind of just overload you with minutia that is best saved for when you can actually understand the concepts behind certain coding choices - at which point it becomes less of an artificial rule and more of an automatically ingrained concept. Not that it's a bad book by any means - it's still a pretty solid resource - but I wouldn't recommend trying to learn from it, at least not without further resources. It can lead to making a simple exercise like a factorial turn into a convoluted pile o' crap. Of course, it's always nice to have multiple resources to reference because books are seldom universally perfect at doing everything. | ||
bo1b
Australia12814 Posts
| ||
LegalLord
United Kingdom13775 Posts
On August 15 2017 13:44 bo1b wrote: I don't recall any such instance, are you certain that wasn't primer plus? Nope, Primer 5th as referenced above. I will say that I do appreciate its focus on style and "doing things right" - but frankly that is best learned incrementally rather than through up-front where it's going to lead to coding paralysis and still kind of bad results. | ||
ChristianS
United States3187 Posts
On August 15 2017 13:36 LegalLord wrote: Harsh! I thought I was pretty clear that I thought the simple solution was the best, and I was just trying to think about these other ways to do it as an exercise in thinking about the consequences of using these more complicated mechanisms.I looked back over the book and basically confirmed my criticisms. Primer suffers from a bad case of being overly pedantic and missing the forest for the trees in terms of a book meant to teach the younglings how to code. A bit too basic to be a good reference, but far too convoluted to be a good starter book. I saw one example there that instantly bugged me, in like chapter 1 or 2. It was something like "well the proper way is return x+4; because its faster but if you aren't comfortable with that to int y = x+4; return y; but that's bad." Lots of little tidbit micro-optimizations that frankly don't add anything to the learning experience, but that kind of just overload you with minutia that is best saved for when you can actually understand the concepts behind certain coding choices - at which point it becomes less of an artificial rule and more of an automatically ingrained concept. Not that it's a bad book by any means - it's still a pretty solid resource - but I wouldn't recommend trying to learn from it, at least not without further resources. It can lead to making a simple exercise like a factorial turn into a convoluted pile o' crap. Of course, it's always nice to have multiple resources to reference because books are seldom universally perfect at doing everything. Speaking of needlessly overcomplicating the factorial function, what if I had the function return a union which can hold either a long long or a long double? Then if the answer fits in a long long I return that, otherwise I return the next-best long double. Too hacky? Convoluted pile of crap? | ||
LegalLord
United Kingdom13775 Posts
| ||
ChristianS
United States3187 Posts
Oh well. I promise I'll stop trying to make my factorial function more complicated and just move on to learning other stuff | ||
bo1b
Australia12814 Posts
| ||
![]()
TanGeng
Sanya12364 Posts
With factorials you're probably doing counting, Which means doing full precision integer math. We want long long for such a calculation, and with large factorials, we go Beyond long long easily. C++ Has exercises with big nums. See http://www.cplusplus.com/forum/lounge/32041/ The declaration might look like: bigNum* factorial(int); This makes your factorial code allocation of bigNum. And there is no need to have factorials go Beyond int. The values become too big. The computation is too slow. Concern for structure passing is an optimization with consideration with How your code translates into Machine code. At this Point, you also WanT to look at other optimizations. If you are going to call this function over and over again, you WanT to memoize and store results. Hash or an array for this memoization can work. Depending on how you do it, you may WanT to convert to: const bigNum* factorial(int); RIP capitalization. I pressed the TLPD button. | ||
LegalLord
United Kingdom13775 Posts
| ||
![]()
TanGeng
Sanya12364 Posts
On August 16 2017 05:15 LegalLord wrote: If you want bignums, my personal favorite (for C++) is ttmath. It's not arbitrary precision like most (you define a precision at compile-time) but it's easy to use and it's the most performant I've ever used. For a task quite similar to factorials (the Gamma function) I also had issues with overflow and ttmath took care of that problem like a charm, with 0.1% of the trouble of most of the solutions I have seen. ttmath is one of the best open source libraries you can use in C++ for bignums. For storage sizes: double itself is 10 bits of exponents and can compute 171 factorials. If you need factorials and care about runtime performance, you might just precompute at compile time and have an array. long double with GCC is 80-bit precision with 15 bits of exponents. That's a bit more than 3000 distinct answers. Getting a full precision for factorials greater than 3000 is sizeable. Something to think about. With bignums, you can really get up there with the precision and size of answers. Consider your needs before using and implementing. | ||
| ||