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

C++/java program for calculating probability: ok so the problem is,we have a fes

ID: 3667066 • Letter: C

Question

C++/java program for calculating probability:

ok so the problem is,we have a festival in which N artists are present.Each artist has his/her own masterpiece.

in this festival,the artists decide to give their masterpieces to another artist in the festival,such that they are all going to

gather up all of their masterpieces and then, at the end of the festival, each artist will pick an art piece (completely at random)

the problem that might occur is that an artist might end up with his own artpiece.

write a C++ program that calculates the probability of the event that no artpiece will fall into the hands of its creator and the event

ends successfully?

the input to the program will be the number of artists in the festival

and the output will be the probability in fraction form such that the numerator is the number of successful and correct ways of distributing the artpieces amongst the artists and the denominator stands for every possible distribution i.e. N!

example #1:

input: 3

output: 1/3

example #2:

input: 6

output: 53/144

TIP:this problem has a recursive solution

Explanation / Answer

Answer:

import java.util.*;

public class Artists {

public static void main(String[] args) {
   int count=0;
   int c;
   int value=1;
   Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the number of artists N :");
int n = keyboard.nextInt();
if ( n < 0 )
System.out.println("Number should be positive.Please Check");
else
{
for ( c = 1 ; c <= n ; c++ )
value = value*c;

//System.out.println("value of denominator is "+n+" is = "+"/"+value);
}
List<Integer> list = new ArrayList<>();
for (int i=0; i<n; ++i) {
list.add(i);
}
for (List<Integer> li : artist(list)) {
//System.out.println(li);
   count++;
  
}
   //System.out.println(count);
   double bd=(double)count/value;
  
   //System.out.println(bd);
   System.out.println(count+"/"+value);
  
}

public static <T> List<List<T>> artist(List<T> elements) {
List<List<T>> list = new ArrayList<>();
if (elements.size() == 0) {
list.add(new ArrayList<T>());
} else {
trackelements(elements, new int[elements.size()], 0, new BitSet(elements.size()), list);
}
return list;
}

private static <T> void trackelements(final List<T> elements,
final int[] moredata,
int k,
BitSet bs,
List<List<T>> result) {
if (k == elements.size()) {
List<T> range = new ArrayList<>();
for (int i : moredata) {
range.add(elements.get(i));
}
result.add(range);
} else {
for (int i=0; i<elements.size(); ++i) {
if (!bs.get(i) && i != k) {
bs.set(i);
moredata[k] = i;
trackelements(elements, moredata, k+1, bs, result);
bs.set(i, false);
}
}
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote