Fisher-Yates Shuffle?

Oct 16, 2014 at 9:35pm
Well, first off, this is homework for a c++ course. I just can't figure out how to get this fisher-yates shuffle to work without using arrays, as we haven't learned how to use those so I can't implement them.
Also, the instructor doesn't do a very good job of explaining things. He just reads off the textbook and isn't very helpful, to be honest. I've spoken to the rest of the class and we all feel this way.

The homework assignment is supposed to be the classic game of cows and bulls using the fisher-yates shuffle.

This is what I have for it:

1
2
3
4
5
6
7
8
9
10
11
12

void shuffle(vector<int> v)
{
    int n = v.size();
    for(int a=n-1; a>0; a--)
    {
        int j = randint(a, n);
        int tmp = v[a];
        v[a] = v[j];
        v[j] = tmp;
    }
}


I am a beginner, so I would appreciate any helps and tips. Thank you!
Last edited on Oct 16, 2014 at 9:36pm
Oct 16, 2014 at 9:42pm
Well, it looks like you know how to use arrays to me. does the assignment specifically forbid using them?

It would seem ludicrous to not use an array for this, as the next best option I can think of involves using pointers. And if you haven't learned arrays yet, then you certainly haven't gotten into pointers.
Last edited on Oct 16, 2014 at 9:48pm
Oct 16, 2014 at 10:56pm
No, not specifically, but we haven't gone over them in class at all so I'm assuming we aren't supposed to use them since its a beginner C++ class. Haven't learned pointers either, as you correctly assumed.
Oct 17, 2014 at 1:00am
I'd go ahead and use the vector as you've done above then, to do this without arrays or pointers would require a large nasty convoluted set of nested if/else and/or switch blocks, all of which would be confusing and error prone. Arrays make it simple, direct, and sensible. I wouldn't think the instructor would penalize you for reading ahead in the book a bit.
Oct 17, 2014 at 6:30am
void shuffle(vector<int>& v) // Note the &

The code in the OP operates on a copy of the vector that is fed to it, so the original vector is unaffected and the changed vector stops existing at the end of the function.
Oct 20, 2014 at 7:28pm
Ah, so I was just missing the ampersand?
Tried speaking to the teacher, he said it should work even though I showed him the output being EXACTLY the same beforehand.
He did say no arrays when I asked, by the way.
He gave us
 
void shuffle(vector<int> v)

without it and had us fill in the rest so I never would've thought his code was missing something.
I had a feeling that I had to add something to apply the changes, but I didn't know what exactly. Thank you, I'll try it out when I get to a computer.
Oct 21, 2014 at 3:31am
Haha that works! I swear, I spent so much time wondering why it wouldn't work, and it turns out all it was missing was the damn ampersand. Thanks a lot professor.
Thank you cire, much appreciated. You saved me countless hours of head-scratching! I'm glad I joined this forum!
He mentioned the ampersand once, but it was during a different lecture and he didn't even explain it. He just showed us code with the ampersand in a similar position and literally said don't worry about it, you don't need to know what it is yet. I'm really beginning to question the credibility of this instructor a lot more after this.
Last edited on Oct 21, 2014 at 3:33am
Oct 21, 2014 at 3:39am
Well, at least your instructor is getting you right into the STL ( via vectors ).
Topic archived. No new replies allowed.