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

A statistician is studying the sequence of numbers obtained by repeatedly tossin

ID: 3815344 • Letter: A

Question

A statistician is studying the sequence of numbers obtained by repeatedly tossing a six-sided
number cube. On each side of the cube is a single number in the range 1 to 6 inclusive, and no
number is repeated on the cube. The statistician is particularly interested in runs of numbers. A
run occurs when two or more consecutive tosses of the cube produce the same value. for
example, in the following sequence of cube tosses, there are runs starting at position 1, 6, 12
and 14.

Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

Result 1 5 5 4 3 1 2 2 2 2 6 1 3 3 5 5 5 5
Write a program that creates an int array of 20 elements, would ask the user how many tosses
are required (1-20), fills the array with the result of tossing a number cube as many times as
requested by the user, prints the values of the tosses horizontally then displays the longest run
found in the sequence of tosses.
For the above example the output would be if the user requested 18 tosses:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 5 5 4 3 1 2 2 2 2 6 1 3 3 5 5 5 5
The longest run occurs at index 14.

Explanation / Answer

import java.util.*;
public class TossingGame {

   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       System.out.println("how many tossess you required:");
       int toses=sc.nextInt();
       int arr[]=new int[toses];
       Random r=new Random();
       System.out.print("index:");
       for(int i=0;i<toses;i++)
          
       System.out.print(i+" ");
       System.out.println();
       System.out.print("Result:");
       for(int i=0;i<toses;i++){
           int value=r.nextInt(6)+1;
           arr[i]=value;
       System.out.print(arr[i]+" ");
      
          
       }
       System.out.println();
       int j=0;
       int count=0;
       int big=0;
       int index=0;
       int datavalue=0;
       for(int i=0;i<arr.length;i++){
           int chk=arr[i];
           while(j<arr.length){
               if(chk==arr[j]){
                   count++;
                  
               }else{
                   break;
               }
               j++;
           }
           if(count>=big){
               big=count;
               index=i;
               datavalue=arr[i];
           }
           count=0;
           j=i;
       }
System.out.println("longest sequence digit is:"+datavalue);
System.out.println("longest sequence is:"+big);
System.out.println("index if longest sequence is:"+index);
   }

}

output:

how many tossess you required:
30
index:0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Result:5 1 4 3 6 6 6 5 6 1 3 1 1 2 4 6 1 3 1 6 1 4 6 4 2 3 1 6 3 5
longest sequence digit is:6
longest sequence is:3
index if longest sequence is:5

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