Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

New answers tagged

3

Since the function is hardwired to 3x3 matrices, it would make sense to pass a 3x3 matrix as an argument: int formingMagicSquare(const int s[3][3]) This way you will not worry about what to do if s_rows != 3. stdlib provides int abs(int). The line if(cost4_this_operation < cost)cost = cost4_this_operation; better be split into if(...


2

Disclaimer: I am not an expert in music theory, and english is not my first language. Therefore I may use the wrong terms in the following review. Review of your current code I/O should be separated from the computation. In your case, the findUpperStructureTriads() function should return something instead of printing it. That makes the function better ...


3

The optimal solution to this is significantly simpler than what you have. We'll get to that in a second. First, let's examine your code: Cop should not begin with a capital letter. Variables should be snake_case. Furthermore, cop is a bad name. At the least it should be copy (don't shorten names, it only makes things less clear--unless the shortening is a ...


2

As often, the recursive algorithm is readable and beautiful, but not very efficient: stack consumption can be overwhelming, memory allocations are plentiful, and you're forced to compute the whole set of permutations at once. When you mechanically translate the recursive algorithm into its iterative form, you often lose most of its beauty and readability, ...


1

Your use of the i variable isn't clear to me. The algorithm might look like this (starting with an empty permutation): Repeat 'forever' (precisely: until a break): if the permutation isn't full yet (length less than n), append zeros (or whatever the minimum allowed value is); otherwise: add the permutation to results, remove the tail of maximum values (m ...


1

I think a first problem is that m should be changed to m-1. Or maybe better you should change it so that the lines "i=0;" are changed to "i=1;" instead, and also initializing the vector with 1 instead of 0. By what I understand a permutation does not necessarily contain each number at least once. So we just need to generate all possible vectors of length \$...


3

I understand the need to reinvent the wheel but in this case you reinvented an other kind of wheel: functional-style combinatorics aren't very well suited to C++ and the high-performance / low-memory usage it is well known for. I mean, that's a bike wheel for a car. Now if you want to reinvent the C++ wheel, the best thing would be to re-implement std::...


6

A cleaner solution is to trust the standard library and try to re-use the generic components already available there. Your problem is solved by std::next_permutation, so you can proceed along the lines of: #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = { 1, 2, 3, 4, 5 }; do {...


Top 50 recent answers are included