Sunday 30 December 2012

How the songs are played randomly in a playlist?? This may be the algorithm

#include<iostream>
#include<conio.h>
using namespace std;
class Playlist
{public:
        void random(); /*This function initialises the values from 1 to n and swaps the numbers for odd number count*/
               void swap(int &a,int &b);
};  

int main()
{Playlist dev;
dev.random();
getch();
}

void Playlist::random()
{int n,s,i,p;   //present song number p
cout<<"The number of songs in the playlist  :"; cin>>n;
int a[n],j=1;
cout<<"enter the present song number  : ";  cin>>p; s=p;//s  store 
a[0]=p;
     for(i=++p;i<=n;i++,j++)
     {
     a[j]=i;
     if(j%2!=0)
     swap(a[j-1],a[j]);
     }


   if(j!=n)
   {  for(i=1;i<p;i++,j++)
      {a[j]=i;
      if(j%2!=0)
           swap(a[j-1],a[j]);
     }
    }
 cout<<"\nNew  playlist generated \n";
 for(j=0;j<n;j++)
 cout<<a[j]<<"  ";  
}
void Playlist::swap(int &a,int &b)
{int temp=a;
a=b;
b=temp;
}

No comments:

Post a Comment