proffreda

C++ Pointers and Indirection: shuffle.cpp

Feb 18th, 2016
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. void shuffle( int [ 10 ] );
  7. void playorder( const int [10], const char *[]);
  8.  
  9. int main()  {
  10.    int songorder[10] =  {0};
  11.    const char *mymp3list[ 10 ] =
  12.         {"Happy","Dark Horse","Talk Dirty","All Of Me","Drunk In Love",
  13.           "Pompeii","Team","Say Something","Counting Stars","Timber"};
  14.  
  15.    cout << "Original Song Order:" << endl;
  16.    for (int i= 1 ; i <= 10 ; ++i)
  17.          cout << i << ". " << mymp3list[i-1] << endl;
  18.    
  19.    cout << "Shuffled Order:" << endl;
  20.    srand( time( 0 ));
  21.    shuffle( songorder );
  22.    for (int i = 0; i < 10 ; ++i )
  23.        cout << i+1 << "->" << songorder[i] << "   " ;
  24.    cout << endl<< endl;
  25.  
  26.    playorder( songorder, mymp3list );
  27.    return 0;
  28. }
  29.  
  30. void shuffle( int order[ 10 ] ){
  31. // places the numbers from 1 to 10 in random order in the array order
  32.    int position;
  33.    int  i;
  34.    for (i= 1; i <= 10; i++ ) {
  35.       do {
  36.          position = rand() % 10;
  37.       } while( order[position]!= 0 );
  38.       order[position] = i;
  39.    }
  40. }
  41. void playorder( const int order[ 10 ], const char *songlist[] ) {
  42. // prints out the songs in order given by order
  43.    for ( int song = 1; song <= 10; song++ )
  44.       for ( int position = 0; position < 10; position++ )
  45.             if ( order[position] == song )
  46.                cout << song  << ". " << songlist[ position ] << "\n";
  47.    cout<< endl;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment