#include #include #include #include "randgen.h" using namespace std; struct Track { string title; // title of song/track int number; // the original track number }; void SwapTracks (Track & track1, Track & track2) { Track temp; temp = track1; // swap entries track1 = track2; track2 = temp; } void Print(const vector & tracks, int count) // precondition: there are count locations in tracks // postcondition: contents of tracks printed { int k; for (k=0; k < count; k++) { cout << tracks[k].number << "\t" << tracks[k].title << endl; } } void Shuffle(vector & tracks, int count) // precondition: count = # of entries in tracks // postcondition: entries in tracks have been randomly shuffled { RandGen gen; // for random # generator int randTrack; int k; // choose a random song from [0..count-1] for all songs for(k=0; k < count; k++) { randTrack = gen.RandInt(0, count-1); // random track SwapTracks (tracks[randTrack], tracks[k]); // swap entries } } int main() { int i; vector tracks(10); tracks[0].title = "In the Flesh?"; tracks[1].title = "Hey You"; tracks[2].title = "Another Brick In The Wall"; tracks[3].title = "The Happiest Days Of Our Lives"; tracks[4].title = "The Show Must Go On"; tracks[5].title = "Goodbye Blue Sky"; tracks[6].title = "Empty Spaces"; tracks[7].title = "Is There Anybody Out There?"; tracks[8].title = "One Of My Turns"; tracks[9].title = "Don't Leave Me Now"; for (i=0; i < 10; i++) { tracks[i].number = i + 1; } Print(tracks, 10); Shuffle(tracks, 10); cout << endl << "---- after shuffling ----" << endl << endl; Print(tracks, 10); return 0; }