Is there something wrong with this?

Well, my classes are over and I decided to review my c++ coursework. In one of our assignments, a mastermind with an AI, he told us to get the permuteStr function to work, but it already works? Am I missing something here? Here's the code and the relevant swap function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void swap(string& s, int i, int j)
{
    char temp;
    temp = s[i];
    s[i] = s[j];
    s[j] = temp;
}

int permuteStr(string& s, const string secret, int i, int n)
{
   int j;
   if (i == n) { // base case
        Play p(s,0,0);
        add_play(plays,secret);
        return p.get_bulls();
   }
   else
   {
        for (j = i; j <= n; j++)
        {
           swap(s, i, j);
           permuteStr(s, secret, i+1, n);
           swap(s, i, j); //backtrack
        }
   }
}


Question is essentially, is there anything wrong with this, or can it be optimized somehow? I can't find any useful changes to make.
closed account (SECMoG1T)
Was it meant to use recursion? If so then you should design it to work without the loop on line 19.
Topic archived. No new replies allowed.