write a program that displays a random permutation of the numbers 1 to 10. The r
ID: 674418 • Letter: W
Question
write a program that displays a random permutation of the numbers 1 to 10. The resulting permutation is stored in an array of integers. To generate a random permutation, you need to fill an array with the numbrtd 1 to 10 so that no two entries of the array have the same contents. This can be achieved by brute force, by calling Random.nxtlnt until it produces a value that is not yet in the array. Instead, you make a second array and fill it with the number 1 to 10(repository array). Then you pick one of those at random, remove it, and append it to the permutation array. Also, write a psedo-code before programming. Cannot use array list.
Explanation / Answer
import java.util.*;
class permutation
{
public static void main(String args[])
{
int a[]=new int[10];
int i;
int p[]=new int[10];
Random r = new Random();
for(i=0;i<10;i++)
{
a[i]=r.nextInt(11);
}
for(i=0;i<10;i++)
{
p[i]=(10-a[i])/fact(a[i]);
}
for(i=0;i<10;i++)
{
System.out.println(p[i]);
}
}
public static int fact(int n)
{
int i, f=1;
for(i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.