Friday, February 11, 2011

My own shuffle array code

#include "iostream"
#include
#include
#include
#include
#include
using namespace std;

void shuffle(int A[],int size)
{
int tmp;
for(int i = 0; i {
srand ( time(NULL) );

int ran = rand()%(size-i);
tmp = A[size - i];
A[size - i] = A[ran];
A[ran] = tmp;
}
}

int main()
{
int A[10] = {1,2,3,4,5,6,7,8,9,10};
shuffle(A,9);

int j = 0;
while(j<9)
{
cout << A[j++] << " ";
}

getch();
return 0;
}

No comments:

Post a Comment