Hi jumper007,
I may have been a bit quick of the mark with some of the things in my last post. It was my bad that I didn't read & understand the problem properly, now that I have your code code makes more sense now.
However, my suggestion of doing a design might still be of some use, I have the following questions / observations:
A <vector> is really a static (ie not dynamic) array of pointers to objects. In other words it is like a C array of pointers that cannot be resized. When you resize a vector, the compiler reallocates a brand new array that is big enough. You can make the array large enough in the first place for the worst case scenario, but this is often a waste. So a vector is really only good for things that you know are not going to change at all, or rarely.
A <list> is a double linked list of pointers to objects that is much more efficient for increasing the size.
This is important because you are resizing coll1 factorial(n) times.
Now back to the size of a factorial:
factorial(20) is 2.4329E+18, so an unsigned long long will just hold this value.
This also means that your loops are going to execute 2.4329E+18 times, making it easy to get a stack overflow, or fill up the memory. It also means the vector size of 20 is now a problem - 20 was the number of cases, but you need a vector that has factorial(n) elements. I now see why you had the large value yesterday.
I find the question a bit strange that they require you to go to this limit. factorial(7) is 5040, I would have thought that would have been enough.
I guess this means that you may have to come up with a way of doing this without storing everything in a vector. Might have to google some algorithms for doing this eficiently.
The other problem is with the for loops:
1 2 3 4 5
|
for (int i = 0; i < n; i++)
{
P[i+1] = coll1[i] * coll1[i+1];
SOP2 = SOP2 + P[i+1];
}
|
the i+1 will go past the end of the vectors.
Have you tested your factorial function independently? Normally with recursion you have to have a base case that causes the recursiion to end, in this case it is n==1. You are decrementing n, so when n is 1 it returns 1, so the function always returns 1. Let me know if I have gone totally mad and what I am saying is wrong !!!
1 2 3 4 5 6 7 8 9 10 11
|
int factorial(int n)
{
if (n == 0)
return 1;
else
if(n == 1)
return 1;
else
return n*factorial(n-1);
}
|
In main, you have a do loop that tests n != 0, but I don't see where you decrement n, in fact it is re-entered by the user at the start of the do loop. May be this could be better with a for loop using a different variable.
The next_permutation function returns a bool, you should test this just in case, it will be false if the iterator returns to the beginning.
Some other improvements, would be better meaningful variable names (not single letters). I know you have used names like SOP because that was what was in the question - If it was me I would use SumOfProduct.
Comments are really helpful for others to understand your code, you could put them in before the for loops to explain what they do. Also comment the variables when you declare and initialise them, & put all these with 1 variable per line.
Hope this helps, sorry for leading you astray yesterday, I hope I have made up for it with today's comments.