What are your favorite songs in the Billboard Top 100 ??
Add them to this program, and run it to let your ipod play them in ipod-order.
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; void myShuffle( int mySongOrder[] ); void playIpodOrder( const int ipodOrder[], const char * mp3[]); int main() { srand( time( 0 )); const char * mp3List[] = { "Uptown Funk!","Thinking Out Loud", "Take Me to Church","Sugar","Blank Space","FourFiveSeconds", "Lips Are Movin", "I'm Not The Only One","Love Me Like You Do","Time Of Our Lives"}; cout << "Original Song Order:" << endl; for (int i= 1 ; i <= 10 ; ++i) cout << i << ". " << mp3List[i-1] << "\n"; cout << "\n\n"<< endl; cout << "My Shuffled Order:" << endl; int mysongorder[10] = {0}; myShuffle( mysongorder ); for (int i = 1; i <= 10 ; ++i ) cout << i << "=>" << mysongorder[i-1] << " " ; cout << endl<< endl; playIpodOrder( mysongorder, mp3List ); return 0; } void myShuffle( int order[] ){ // places the numbers from 1 to 10 in random order in the array order int position; int i; for ( i= 1; i <= 10; i++ ) { do { position = rand() % 10; } while( order[position]!= 0 ); order[position] = i; } } void playIpodOrder( const int ipodOrder[], const char * mp3List[] ) { // prints out the songs in order given by ipodOrder for ( int song = 1; song <= 10; song++ ) for ( int position = 0; position < 10; position++ ) if ( ipodOrder[position] == song ) cout << song << ". " << mp3List[ position ] << "\n"; cout<< endl; }
Exercises:
Task 0: Carefully read and understand the program above; compile and run the program. Note that the song lists are ordered from 1 to 10.
Task 1: Modify the program so that you replace “mp3List” with your own longer list of strings. How you do this is up to you.
Task 2: Modify all the functions so that the size of the arrays involved are passed as value parameters.
Task 3: As written, there is a small chance that the program may run for a very long time. Modify the program so that the shuffle function is guaranteed to terminate in a fixed time bound.
The following method can be used:
First assign the values from 1 to 10 (or up to the value of size) to the elements of the order array. Then use a loop to iterate through the array considering each position exactly once. The number in that position should be swapped with the number in a random position in the array. Test your result by using the playIpodOrder function to print the
resulting shuffle to see if it works properly.
For the following tasks you will add a second playlist, call it myfriendsmp3list, or something else.
Task 4: Write a function called “printUnion” that prints a listing of the combination of all songs in the two lists (without repeats).
Task 5: Write a function called “printIntersection” that prints out a listing of all songs that appear in both of the two lists.
Extra Credit:
Task 6: Write a function “shuffleTogether” that prints out all the songs in both lists shuffled randomly together and without duplicates. Try to do this without allocating and creating a new list.
Scaffolding for Task 4:
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; void printUnion(const char * l1[], int s1, const char * l2[], int s2); int main() { srand( time( 0 )); const char * mp3List[] = { "Shut Up And Dance", "Earned It", "Uptown Funk!","Thinking Out Loud", "Take Me to Church","Sugar","Blank Space","FourFiveSeconds","Blondie-Call Me","Pink Floyd-Another Brick In The Wall"}; const char * friends80smp3List[] = {"Blondie-Call Me","Pink Floyd-Another Brick In The Wall", "Olivia Newton-John-Magic", "Michael Jackson-Rock With You", "Captain and Tennille-Do That To Me One More Time", "Queen-Crazy Little Thing Called Love", "Paul McCartney-Coming Up", "Lipps, Inc.-Funkytown", "Billy Joel-It's Still Rock And Roll To Me", "Bette Midler-The Rose"}; cout<< "Size (in bytes) of mp3list: "<< sizeof(mp3List) << endl; const int mySize = sizeof(mp3List)/ sizeof(const char *); cout << "The size of the mp3 collection is: " << mySize << endl << endl; cout<< "Size (in bytes) of friends80smp3list: "<< sizeof(friends80smp3List) << endl; const int friendSize = sizeof(friends80smp3List)/ sizeof(const char *); cout << "The size of my friends mp3 collection is: " << friendSize << endl << endl; cout << "Union of Songs:" << endl; printUnion(mp3List, mySize, friends80smp3List, friendSize); cout << "Any repeats?" << endl; cout << " does mp3List[mySIZE-1] equal? myfriends80smp3List[0] => " << (mp3List[mySize-1] == friends80smp3List[0])<< endl; cout << " does mp3List[mySIZE-1] equal? myfriends80smp3List[1] => " << (mp3List[mySize-1] == friends80smp3List[1])<< endl; return 0; } void printUnion (const char * list1[], int sizeList1, const char * list2[], int sizeList2){ // ToDo: Fix the duplicates problem for (int i= 1 ; i <= sizeList1; ++i) { cout << i << ". " << list1[i-1] << "\n";} for (int i= 1 ; i <= sizeList2; ++i) { cout << sizeList1 + i << ". " << list2[i-1] << "\n";} }
Discussion
No comments yet.