Sunday, February 27, 2011

Random order for array elements

/*
* RandomOrder.c
*
* Created on: Feb 28, 2011
* Author: Karthikeyan.D
*/
# include <stdio.h>
# include <windows.h>

int main()
{


//Variable Declaration
int szArray[20];
int i,SwapIndex,temp;

// Value Assignment
for(i=0;i<20;i++)
szArray[i]=i+1;

//Array Value Shuffling
for(i=0; i<20;i++)
{
do
{
SwapIndex = rand()+1;
}while(SwapIndex >= 20);
temp = szArray[i];
szArray[i] = szArray[SwapIndex];
szArray[SwapIndex] = temp;
}
// Dispaly part
for(i=0;i<20;i++)
printf("%d\n",szArray[i]);


return 0;
}

OUTPUT:

10
1
7
11
6
17
5
16
8
20
18
12
13
9
3
2
19
4
15
14

No comments:

Post a Comment