As the title says, I'm trying to find a method to shuffle an array. C++Builder doesn't seem to have random_shuffle() and I don't know how to do it.
I can't find much about this program on google. To be more specific, I'm trying to do a memory matching game that has 16 pairs. I tried implementing something to give each card a random position (position 0, 1, 2, .. etc) but I don't know how to do it without having a value duplicated.
So the easiest method I see is creating an array a[]={0,1,2,...31} and shuffling the values somehow. Is it possible to do anything like this? If not, do I have any alternatives?
I hope my question is understandable.
The C++ Builder doesn't seem to have random_shuffle()
Yes, it does. Make sure you have #include <algorithm> in your code.
The standard algorithm to shuffle an array is the Fisher-Yates algorithm. That is easy to implement and there are a number of examples on the web. However, if you are learning then it would be a good idea to program it from scratch rather than blindly copy someone else's code.
Related
Is there any library or the code of a function in C++ that I can use for comparing numeric vectors in C++?
Just googled a little and found smth like that here I think you can use that to write your own which will work with numbers and vectors. Anyways, I dont know that topic but they say it is about strings(in other words letters). Also, I don't think for a project of this scale, speed will be an issue.
Is there any best approach or template to follow, while doing this?
I mean two things in particular, because for me it is problematic to imagine how it would go in c++:
expanding arrays on go, where they are expanded during program and i dont know whethere the final size will be e.g. 10 or 100000.
plots. I have never done any plot in c++ as I always have been doing it in matlab when necessary.
So what templates or rules should I follow, and how could I cope with those two things?
I found that eigen library would be useful for matrices (dynamically expanding also?), but as I am not sure, want to ask first to be sure of a right approach. Nothing i know about plots.
Please add some link for me to learn from, if useful.
Thanks!
expanding arrays on go, where they are expanded during program and i dont know whethere the final size will be e.g. 10 or 100000.
The solution to this is simple: look up std::vector (or std::deque) both provide this behaviour. (With "subtle" differences between a deque and a vector).
plots. I have never done any plot in c++ as I always have been doing it in matlab when necessary.
For this you'll have to search for a library that can do this, first you'll have to look into a graphical window library such as Qt. And then you'll have to look up some library that can plot data in a graph form.
Though for this matlab will probably always be the "easier/better" choice; C++ has nothing to help you with this.
Also remember: first learn the language, then learn libraries!
For plotting using QT, QWT is basically all you need as it provides all the non trivial kind of charts one may need.
Does someone know if there is any production-ready K-shortest-paths algorithm for C++?
The only available implementation (k-shortest-paths), unfortunately, leaks memory, has counter-intuitive interfaces and another "reinvented wheel" - the Graph class.
I'm looking for something better, probably, boost::graph-based.
There are two possible algorithms available - simple Yen's algorithm and optimized Yen's algorithm, both would suit me.
Thanks in advance.
There is another one, but you'll have to check if this also leaks memory.
http://sourceforge.net/projects/ksp/files/ksp/ksp-1.0/
I'm looking for a (space) efficient implementation of an LCS algorithm for use in a C++ program. Inputs are two random access sequences of integers.
I'm currently using the dynamic programming approach from the wikipedia page about LCS. However, that has O(mn) behaviour in memory and time and dies on me with out of memory errors for larger inputs.
I have read about Hirschberg's algorithm, which improves memory usage considerably, Hunt-Szymanski and Masek and Paterson. Since it isn't trivial to implement these I'd prefer to try them on my data with an existing implementation. Does anyone know of such a library? I'd imagine since text diff tools are pretty common, there ought to be some open source libraries around?
When searching for things like that, try scholar.google.com. It is much better for finding scholarly works. It turned up
http://www.biotec.icb.ufmg.br/cabi/artigos/seminarios2/subsequence_algorithm.pdf
this document, a "survey of longest common subsequences algorithms".
Not C++ but Python but I think usable.
http://wordaligned.org/articles/longest-common-subsequence
Hirschberg's Algorithm embeds a javascript implementation : almost C.
I created a program using dev-cpp and wxwidgets which solves a puzzle.
The user must fill the operations blocks and the results blocks, and the program will solve it. I'm solving it using brute force, I generate all non-repeated 9 length number combinations using a recursive algorithm. It does it pretty fast.
Up to here all is great!
But the problem is when my program operates depending the character on the blocks. Its extremely slow (it never gets the answer), because of the chars comparation against +, -, *, etc. I'm doing a CASE.
Is there some way or some programming language which allows dynamic creation of operators? So I can define the operator ROW1COL2 to be a +, and the same way to all other operations.
I leave a screenshot of the app, so its easier to understand how the puzzle works.
http://www.imageshare.web.id/images/9gg5cev8vyokp8rhlot9.png
PD: The algorithm works, I tried it with a trivial puzzle, and solved it in a second.
Not sure that this is really what you're looking for but..
Any Object Oriented language such as C++ or C# will allow you to create an "Operator" base class and then to derive from this base class a "PlusOperator" or "MinusOperator" etc'. this is the standard way to avoid such case statements.
However I am not sure this will solve your performance problem.
Using plain brute force for such a problem will result you in an exponential solution. this will seem to work fast for small input - say completing all the numbers. But if you want to complete the operations its a much larger problem with alot more possibilities.
So its likely that even without the CASE your program is not going to be able to solve it.
The right way to try to solve this kind of problems is using some advanced search methods which use some Heuristic function. See the A* (A-star) algorithm for example.
Good luck!
You can represent the numbers and operators as objects, so the parsing is done only once in the beginning of the solving.