C program to shuffle integer array & grouping its elements
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_SIZE 100
int main(void)
{
int i = 0;
int numbers[ARRAY_SIZE];
int limit;
int groupSize;
int logic,temp, mod,count;
count=1;
srand(time(NULL));
printf("\n Enter Size of Integer Array (Max 100) : ");
scanf("%d",&limit);
if(!(limit>0 && limit<=ARRAY_SIZE)){
printf("\n Array Size should be greater than 0 and less than %d", ARRAY_SIZE);
return;
}
printf("\n Enter Size of Group : ");
scanf("%d",&groupSize);
for (i = 0; i < limit; i++)
{
numbers[i] = i;
}
for(i=0;i<limit; i++){
int randomnumber = rand() % limit;
int randomnumber1 = rand() % limit;
int temp = numbers[randomnumber];
numbers[randomnumber] = numbers[randomnumber1];
numbers[randomnumber1] = temp;
}
logic = limit/groupSize;
mod = limit%groupSize;
count = 1;
int j;
printf("\n Array after shuffling & group of %d elemtns: \n", groupSize);
for ( i = 0; i < limit;){
if(mod>0)
temp=logic+1;
else
temp=logic;
printf("\n %d : ",count++);
for(j=0;j<temp && i<limit;j++,i++){
printf(" %d ", numbers[i]);
}
mod--;
}
return 0;
}
Algorigthm for the above code
Algorithm: Shuffule Array and group its element
Input: Array, NumberOfGroups
Output: List of Groups with random values
1. Input Array from User
2. Input NumberOfGroups from user
2. Shuffle Array elements
for i = 0 to i < lenghOfArray
set randno = rand() % lenghOfArray
set randno1 = rand() % lenghOfArray
set temp = Array[randno ]
set Array[randno] = Array[randno1]
set Array[randno1] = temp
End of loop
3. set MinElementInRow = lenghOfArray div NumberOfGroups
4. set ExtraStd = lenghOfArray mod NumberOfGroups
5. set tempVar to 0
5. for i=0 to i < lenghtOfArray
if ExtraStd > 0 then
tempVar = MinElementInRow + 1
else
tempVar = MinElementInRow
for j=0 to j < tempVar AND i < lenghOfArray
print Array[i]
j++, i++
ExtraStd--
End of loop
6. return 0