Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Just had a question about this problem, need some guidance on how to go about it

ID: 3563007 • Letter: J

Question

Just had a question about this problem, need some guidance on how to go about it:

You will implement an algorithm that generates 100 permutations of the numbers from 1 to n. A permutation is a sequence of numbers in which each number appears exactly once. In your program, you will write a function randperm() which will take an array (a[n]) and the value n as parameters and reorder a in place using the algorithm described below. In the main() function, you will read n, initialize a with numbers 1..n and call randperm(a, n) 100 times. After each call to randperm(), you will print a.

Specs:

You will use the random number generator functions srandom() and random()
You will then ask the user to input the desired value for n and read the response using a fgets/sscanf combination of statements.

The program will then perform the following 100 times using a loop:


This is the algorithm if needed:

The Perfect Shuffle:
The algorithm you will use to generate permutations is the Fisher-Yates shuffle, also called the "Perfect Shuffle" algorithm. It works as follows:

Explanation / Answer

RandomNumbers.c

#include<stdio.h>

void randperm(int [],int);
void display(int [],int);
int main(){
int n,i;
printf("Enter n: ");
scanf("%d",&n);

int a[n];
for(i=0;i<n;i++){
a[i] = i;
}

for(i=0;i<100;i++){
randperm(a,n);
display(a,n);
printf(" ");
}
}

void randperm(int a[],int n){
int i,j,temp;
for(i=n-1;i>0;i--){
j = srand(time(NULL)) % i;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

void display(int a[],int n){
int i;
for(;i<n;i++){
printf("%d ",a[i]);
}
}