|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On February 04 2013 04:32 Arnstein wrote: For some reason I can't get my head around this. I visualize it like first you get xretning[0], and then you make yretning from 0 6, then you get xretning[1] and get the vertical yretning from 0-6. Is there a visualiser for C++ much like pythontutor.com?
Yes, you get yretning from 0-6 for each xretning, but you only move vertically at the cout << endl; which is executed outside the yretning loop => once for each xretning.
|
On February 04 2013 04:32 Arnstein wrote: For some reason I can't get my head around this. I visualize it like first you get xretning[0], and then you make yretning from 0 6, then you get xretning[1] and get the vertical yretning from 0-6. Is there a visualiser for C++ much like pythontutor.com?
No visualizer that I know of, but if you plug in the comparable (Python 2.7) code to pythontutor.com (which is an awesome tool, happy you're using it):
tabell = [ [1 , 2 , 4 , 5, 6], [7 , 8 , 9 , 10, 11], [12, 13, 14, 15, 16], [17, 18, 19, 20, 21], [22, 23, 24, 25, 26], [27, 28, 29, 30, 31], [32, 33, 34, 35, 36] ]
for xretning in range(0, 5): for yretning in range(0, 7): print tabell[xretning][yretning], print
You can trace it there (http://pythontutor.com/visualize.html). Highly recommend you do the tracing process on paper first to be able to concretely know where your logic differs from the actual logic.
|
On February 04 2013 04:53 Kambing wrote:Show nested quote +On February 04 2013 04:32 Arnstein wrote: For some reason I can't get my head around this. I visualize it like first you get xretning[0], and then you make yretning from 0 6, then you get xretning[1] and get the vertical yretning from 0-6. Is there a visualiser for C++ much like pythontutor.com?
No visualizer that I know of, but if you plug in the comparable (Python 2.7) code to pythontutor.com (which is an awesome tool, happy you're using it): tabell = [ [1 , 2 , 4 , 5, 6], [7 , 8 , 9 , 10, 11], [12, 13, 14, 15, 16], [17, 18, 19, 20, 21], [22, 23, 24, 25, 26], [27, 28, 29, 30, 31], [32, 33, 34, 35, 36] ]
for xretning in range(0, 5): for yretning in range(0, 7): print tabell[xretning][yretning], print
You can trace it there (http://pythontutor.com/visualize.html). Highly recommend you do the tracing process on paper first to be able to concretely know where your logic differs from the actual logic.
I see! You get an listindex out of range there, because it's the other way around! Thanks for taking your time to translate the code to Python!
|
Would some C++ guru out there like to critique my merge sort? I started taking the Algorithms class on Coursera and they introduced the merge sort in week 1. Here is my implementation, all in 1 header file:
+ Show Spoiler +#include <vector> #include <iostream> #include <algorithm> #include <iterator>
template<typename T> std::vector<T> merge(std::vector<T> v1, std::vector<T> v2) { typename std::vector<T>::const_iterator b1 = v1.begin(), e1 = v1.end(), b2 = v2.begin(), e2 = v2.end(); typename std::vector<T>::size_type length = (e1 - b1 + e2 - b2); std::vector<T> rVec; rVec.reserve(length);
while(b1 != e1 && b2 != e2) { if (*b1 < *b2) { rVec.push_back(*b1++); } else { rVec.push_back(*b2++); } }
if (b1 == e1) { std::copy(b2, e2, std::back_inserter(rVec)); } else { std::copy(b1, e1, std::back_inserter(rVec)); }
return rVec; }
template<typename T> std::vector<T> mergeSort(std::vector<T> v) { typename std::vector<T>::iterator b = v.begin(), e = v.end(); typename std::vector<T>::size_type size = v.size(); if (size == 1) { return v; } else { std::vector<T> v1(b,b + size / 2), v2(b + size / 2, e); return merge(mergeSort(v1), mergeSort(v2)); } }
Sorry if the format looks strange. I started using Emacs last week and still haven't figured out how to change the tab size from 2 to 4 spaces, though with the really long type names I kind of like the smaller indents. I am looking for any way I can improve the code both in terms of readability and performance.
|
On February 04 2013 11:43 iamke55 wrote:Would some C++ guru out there like to critique my merge sort? I started taking the Algorithms class on Coursera and they introduced the merge sort in week 1. Here is my implementation, all in 1 header file: + Show Spoiler +#include <vector> #include <iostream> #include <algorithm> #include <iterator>
template<typename T> std::vector<T> merge(std::vector<T> v1, std::vector<T> v2) { typename std::vector<T>::const_iterator b1 = v1.begin(), e1 = v1.end(), b2 = v2.begin(), e2 = v2.end(); typename std::vector<T>::size_type length = (e1 - b1 + e2 - b2); std::vector<T> rVec; rVec.reserve(length);
while(b1 != e1 && b2 != e2) { if (*b1 < *b2) { rVec.push_back(*b1++); } else { rVec.push_back(*b2++); } }
if (b1 == e1) { std::copy(b2, e2, std::back_inserter(rVec)); } else { std::copy(b1, e1, std::back_inserter(rVec)); }
return rVec; }
template<typename T> std::vector<T> mergeSort(std::vector<T> v) { typename std::vector<T>::iterator b = v.begin(), e = v.end(); typename std::vector<T>::size_type size = v.size(); if (size == 1) { return v; } else { std::vector<T> v1(b,b + size / 2), v2(b + size / 2, e); return merge(mergeSort(v1), mergeSort(v2)); } }
Sorry if the format looks strange. I started using Emacs last week and still haven't figured out how to change the tab size from 2 to 4 spaces, though with the really long type names I kind of like the smaller indents. I am looking for any way I can improve the code both in terms of readability and performance. Just looking at it, I don't think caching "v.size()" makes sense. I think std::vector already holds its size in a variable so you don't gain any perf by caching. In fact it might be slightly worse perf wise.
|
On February 04 2013 11:43 iamke55 wrote:Would some C++ guru out there like to critique my merge sort? I started taking the Algorithms class on Coursera and they introduced the merge sort in week 1. Here is my implementation, all in 1 header file: + Show Spoiler +#include <vector> #include <iostream> #include <algorithm> #include <iterator>
template<typename T> std::vector<T> merge(std::vector<T> v1, std::vector<T> v2) { typename std::vector<T>::const_iterator b1 = v1.begin(), e1 = v1.end(), b2 = v2.begin(), e2 = v2.end(); typename std::vector<T>::size_type length = (e1 - b1 + e2 - b2); std::vector<T> rVec; rVec.reserve(length);
while(b1 != e1 && b2 != e2) { if (*b1 < *b2) { rVec.push_back(*b1++); } else { rVec.push_back(*b2++); } }
if (b1 == e1) { std::copy(b2, e2, std::back_inserter(rVec)); } else { std::copy(b1, e1, std::back_inserter(rVec)); }
return rVec; }
template<typename T> std::vector<T> mergeSort(std::vector<T> v) { typename std::vector<T>::iterator b = v.begin(), e = v.end(); typename std::vector<T>::size_type size = v.size(); if (size == 1) { return v; } else { std::vector<T> v1(b,b + size / 2), v2(b + size / 2, e); return merge(mergeSort(v1), mergeSort(v2)); } }
Sorry if the format looks strange. I started using Emacs last week and still haven't figured out how to change the tab size from 2 to 4 spaces, though with the really long type names I kind of like the smaller indents. I am looking for any way I can improve the code both in terms of readability and performance. I would pass the vector to be sorted into mergeSort as a reference instead and sort the given vector instead of creating and returning a new one. Usually you don't want to keep the unsorted vector anyway, and if you do then you can always choose to make a copy.
The same applies to the call to merge, where I would make v1 and v2 const references and merge directly into v.
Personally I also would stay away from std::copy and the iterator checks in preference of indexing, but I'm not sure how much difference that makes in practice.
Fyodor is correct in that storing the result of vector::size() is not necessary. Note however that this is not true for all containers.
I just so happened to be bored earlier today and wrote merge sort for the heck of it. Here's my code: + Show Spoiler [merge sort] + template<typename T> void merge(const std::vector<T> &a, const std::vector<T> &b, std::vector<T> &v) { size_t i = 0, j = 0, k = 0; while (i < a.size() && j < b.size()) { if (a[i] < b[j]) v[k++] = a[i++]; else v[k++] = b[j++]; } while (i < a.size()) v[k++] = a[i++]; while (j < b.size()) v[k++] = b[j++]; }
template<typename T> void mergesort(std::vector<T> &v) { if (v.size() > 1) { size_t mid = v.size() / 2; std::vector<T> a(v.begin(), v.begin() + mid); std::vector<T> b(v.begin() + mid, v.end()); mergesort(a); mergesort(b); merge(a, b, v); } }
|
On February 03 2013 20:58 SevenShots wrote: I've had a full class on functional programming with focus on haskell. To be honest I still don't see what the whole buzz is about. I can't see real use to it other than playing around and building some prototypes.
Have you ever been exposed to functional programming before? I've never used a functional language on something real (except for using some of the functional bits in python), but learning one was a great exercise because it forced me to think differently about a lot of problems, and I think it enhanced my problem-solving ability quite a bit. I think everyone should be exposed to at least one functional language.
|
On February 06 2013 20:37 waxypants wrote:Show nested quote +On February 03 2013 20:58 SevenShots wrote: I've had a full class on functional programming with focus on haskell. To be honest I still don't see what the whole buzz is about. I can't see real use to it other than playing around and building some prototypes. Have you ever been exposed to functional programming before? I've never used a functional language on something real (except for using some of the functional bits in python), but learning one was a great exercise because it forced me to think differently about a lot of problems, and I think it enhanced my problem-solving ability quite a bit. I think everyone should be exposed to at least one functional language.
Indeed. Functional languages might not be THAT useful outside of university but learning them is still important because you learn a new way to think about problems that you often can use in other languages.
These days i'm programming a mixture of both object oriented and functional code, no matter the language.
A nice read about that is: http://www.joelonsoftware.com/items/2006/08/01.html
|
On February 04 2013 11:43 iamke55 wrote:Would some C++ guru out there like to critique my merge sort? I started taking the Algorithms class on Coursera and they introduced the merge sort in week 1. Here is my implementation, all in 1 header file: + Show Spoiler +#include <vector> #include <iostream> #include <algorithm> #include <iterator>
template<typename T> std::vector<T> merge(std::vector<T> v1, std::vector<T> v2) { typename std::vector<T>::const_iterator b1 = v1.begin(), e1 = v1.end(), b2 = v2.begin(), e2 = v2.end(); typename std::vector<T>::size_type length = (e1 - b1 + e2 - b2); std::vector<T> rVec; rVec.reserve(length);
while(b1 != e1 && b2 != e2) { if (*b1 < *b2) { rVec.push_back(*b1++); } else { rVec.push_back(*b2++); } }
if (b1 == e1) { std::copy(b2, e2, std::back_inserter(rVec)); } else { std::copy(b1, e1, std::back_inserter(rVec)); }
return rVec; }
template<typename T> std::vector<T> mergeSort(std::vector<T> v) { typename std::vector<T>::iterator b = v.begin(), e = v.end(); typename std::vector<T>::size_type size = v.size(); if (size == 1) { return v; } else { std::vector<T> v1(b,b + size / 2), v2(b + size / 2, e); return merge(mergeSort(v1), mergeSort(v2)); } }
Sorry if the format looks strange. I started using Emacs last week and still haven't figured out how to change the tab size from 2 to 4 spaces, though with the really long type names I kind of like the smaller indents. I am looking for any way I can improve the code both in terms of readability and performance.
There's a couple of things you could do. The biggest thing style-wise would be to sort based on a start/end iterator pair, instead of hard-coding it for vector. This also means you can split your data without allocating new vectors. For performance, don't re-allocate rVec inside merge() every time, allocate a maximum size buffer once and pass (parts of) it around as needed. Then you could look into move semantics, to eliminate some more allocations, especially if you're sorting some complex data. Look at stable_sort in <algorithm>, to see how this would all come together.
Your code is pretty readable, I think some of the typenames are superfluous and you could use auto for the iterator variable declarations, aswell as begin(v)/end(v) instead of the vector members, even if it really doesn't matter here :p
|
I want to start programming in C++, I already know Java. Now I need a (free) compiler that's good for beginners, I tried the Visual Studio one and that was just too much, I want something where I can just easily create a few classes, anything spring to mind?
|
On February 07 2013 06:20 ChrisXIV wrote: I want to start programming in C++, I already know Java. Now I need a (free) compiler that's good for beginners, I tried the Visual Studio one and that was just too much, I want something where I can just easily create a few classes, anything spring to mind?
Make sure you distinguish compiler from IDE. On windows, your only real options for compilers are Microsoft's offering (Visual C++) or gcc via cygwin. What you probably mean is IDE in which case, no, there's no decent, non-complicated learning-level C++ IDE. If VS puts you off, then I recommend simply loading up a text editor and working off the command line as all the other IDEs out there approach similiar level of complexities. Other free windows IDEs for C++ include Eclipse CDT and Qt Creator.
|
On February 06 2013 23:51 Morfildur wrote:Show nested quote +On February 06 2013 20:37 waxypants wrote:On February 03 2013 20:58 SevenShots wrote: I've had a full class on functional programming with focus on haskell. To be honest I still don't see what the whole buzz is about. I can't see real use to it other than playing around and building some prototypes. Have you ever been exposed to functional programming before? I've never used a functional language on something real (except for using some of the functional bits in python), but learning one was a great exercise because it forced me to think differently about a lot of problems, and I think it enhanced my problem-solving ability quite a bit. I think everyone should be exposed to at least one functional language. Indeed. Functional languages might not be THAT useful outside of university but learning them is still important because you learn a new way to think about problems that you often can use in other languages. These days i'm programming a mixture of both object oriented and functional code, no matter the language. A nice read about that is: http://www.joelonsoftware.com/items/2006/08/01.html the whole point about functional code is that it has less bugs. Most bugs stem from programmers not considering every single state their program can be in. Functional code solves this by simply not using state, and also has the added benefit of enabling parallel processing and super simple testing at no extra cost.
Of course though, you can't write most software without SOMEHOW storing or modeling state, and that can get really inconvenient really fast in purely functional languages. but that's fine because that should not be the goal. The goal is to write software most efficiently (and having to fix bugs counts as inefficient). So you write in functional code whatever can be comfortably written in functional code, to minimize the number of bugs involving these parts, and you use languages with easy handling of state for the parts where you need state. That's what's nice about languages that include both object oriented AND functional programming support, they allow you to do just that without any hassle of having to use multiple, different languages. The parts that need state you do in OO, and the parts that don't need state you do functional, and it all integrates seamlessly.
Learning functional programming in this sense mostly serves to teach you the difference between pure functions and functions with state and side effects. Only by knowing what you need to avoid can you keep state from affecting certain functions, and only by knowing where functional programming really starts to hurt can you make sound decisions on when to switch to using state. So hint from me, don't consider functional programming as the goal, but as a tool with benefits and drawbacks, and by learning it you learn to adjust your decision process on where what kind of code is used best.
|
On February 07 2013 06:46 Kambing wrote:Show nested quote +On February 07 2013 06:20 ChrisXIV wrote: I want to start programming in C++, I already know Java. Now I need a (free) compiler that's good for beginners, I tried the Visual Studio one and that was just too much, I want something where I can just easily create a few classes, anything spring to mind? Make sure you distinguish compiler from IDE. On windows, your only real options for compilers are Microsoft's offering (Visual C++) or gcc via cygwin. What you probably mean is IDE in which case, no, there's no decent, non-complicated learning-level C++ IDE. If VS puts you off, then I recommend simply loading up a text editor and working off the command line as all the other IDEs out there approach similiar level of complexities. Other free windows IDEs for C++ include Eclipse CDT and Qt Creator. Ah yes, IDE, don't know why I keep forgetting that word.
Hm, that sucks. It's just that clicking through all those options in VS with no idea what they do...oh well, text editor it is, thank you.
|
On February 07 2013 06:58 ChrisXIV wrote:Show nested quote +On February 07 2013 06:46 Kambing wrote:On February 07 2013 06:20 ChrisXIV wrote: I want to start programming in C++, I already know Java. Now I need a (free) compiler that's good for beginners, I tried the Visual Studio one and that was just too much, I want something where I can just easily create a few classes, anything spring to mind? Make sure you distinguish compiler from IDE. On windows, your only real options for compilers are Microsoft's offering (Visual C++) or gcc via cygwin. What you probably mean is IDE in which case, no, there's no decent, non-complicated learning-level C++ IDE. If VS puts you off, then I recommend simply loading up a text editor and working off the command line as all the other IDEs out there approach similiar level of complexities. Other free windows IDEs for C++ include Eclipse CDT and Qt Creator. Ah yes, IDE, don't know why I keep forgetting that word. Hm, that sucks. It's just that clicking through all those options in VS with no idea what they do...oh well, text editor it is, thank you.
C++, moreso than any other language, "requires" an IDE or cohesive set of command line tools to do real development in. That being said, those things tend to impede you from learning the fundamentals, so it's good that you aren't afraid of using the basic tools to start. =)
|
Hey, I'm a junior in high school looking to study computer science at college. At the moment, I'd like to specialize in web design but I feel like I need more information. I've never been a very artistic person(as in actual painting/drawing) and I'd like to know how important knowledge of art is in web design. The web design class I'm taking in school isn't really advanced so I really have learned a large amount about it yet.
Also how developed is the job market for comp sci majors? I assume it's a rather in-demand field.
|
On February 07 2013 07:58 Brawny wrote: Hey, I'm a junior in high school looking to study computer science at college. At the moment, I'd like to specialize in web design but I feel like I need more information. I've never been a very artistic person(as in actual painting/drawing) and I'd like to know how important knowledge of art is in web design. The web design class I'm taking in school isn't really advanced so I really have learned a large amount about it yet.
Also how developed is the job market for comp sci majors? I assume it's a rather in-demand field.
The job market for comp sci majors is great provided you become good at it. There are so many field to get into as well with this major. Doing your own projects is a must imo, because some schools are mainly theoretical and don't teach you actually how to use tools and build a product. It also helps you find your passion.
As for getting a job doing web "stuff", you should make the distinction between web development and web design. What you're most likely doing in school is web design (like you said), which is less about programming and comp sci stuff as it is making pretty stuff. This involves making mock-ups using photoshop, learning html/css/javascript to display them. This is also referred to as "front-end". This is the creative stuff and artsy part, but less technical and usually paid less.
Web development usually refers to the back-end. This is the server-side which involves writing programs on the server (such as Tomcat, Amazon AWS, Google App Engine, Heroku) with languages such as Java,Python, Ruby, and PHP. This is what the hard programming and comp sci stuff usually refers to. I'd recommend going through Udacity's Web Development course. It gives you a good overview of everything without overwhelming you.
Also, there are new job positions called "User experience/User Interface (UX.UI) engineers" popping up which blur the line between front-end and back-end, but in my opinion requires a lot of experience. I think you're looking more for the web development side, but it's still important to how these things work.
Finally, it's worth mentioning that the title "software engineer" can be used to describe both of these positions or someone who does both front-end and back-end.
If you have more questions feel free to pm me
|
On February 06 2013 23:51 Morfildur wrote: Indeed. Functional languages might not be THAT useful outside of university but learning them is still important because you learn a new way to think about problems that you often can use in other languages.
Beside being really important to learn, functional languages are really used, and you're exposed to devices operating with them on a daily basis. Just look around on google for erlang, the OTP, etc (i was actually really surprised when i was asked to write erlang -- actually learnt it as we were taught haskell in classes -- during an internship at one of our ISP). Funniest program i know which is written in erlang: Wings 3D, yup that's right, a 3D Polygon modeler :D As any language, it's just about using the language that fits the work you wanna do the best (aka increase productivity/stability/efficiency as much as possible). + Show Spoiler +Hell people still program in COBOL, i guess theres more with it than just "everything we have is in COBOL"
|
|
|
I'm having a bit of trouble to understand C memory management. Could you please show me an easy to understand guide? What I also need is perhaps some practical examples where it's crucial to deal with memory instead of stack.
Thanks
|
On February 07 2013 06:58 ChrisXIV wrote:Show nested quote +On February 07 2013 06:46 Kambing wrote:On February 07 2013 06:20 ChrisXIV wrote: I want to start programming in C++, I already know Java. Now I need a (free) compiler that's good for beginners, I tried the Visual Studio one and that was just too much, I want something where I can just easily create a few classes, anything spring to mind? Make sure you distinguish compiler from IDE. On windows, your only real options for compilers are Microsoft's offering (Visual C++) or gcc via cygwin. What you probably mean is IDE in which case, no, there's no decent, non-complicated learning-level C++ IDE. If VS puts you off, then I recommend simply loading up a text editor and working off the command line as all the other IDEs out there approach similiar level of complexities. Other free windows IDEs for C++ include Eclipse CDT and Qt Creator. Ah yes, IDE, don't know why I keep forgetting that word. Hm, that sucks. It's just that clicking through all those options in VS with no idea what they do...oh well, text editor it is, thank you.
I prefer CodeBlocks to Visual C++. You can also install Visual C++ Express instead of that whole Visual Studio package. Visual C++ is much more lightweight(I think, I haven't tried Studio). But check out CodeBlocks. You can also install a compiler, and write your code in Sublime Text and compile it in the command line. (Sublime Text might be able to compile, I don't know)
|
|
|
|
|
|