Question

In: Computer Science

Please enter a seed: 1 Please enter the size of the array: 1 Array size must...

Please enter a seed:
1

Please enter the size of the array:
1

Array size must be greater than 1. Please reenter:
0

Array size must be greater than 1. Please reenter:
-1

Array size must be greater than 1. Please reenter:
12

Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
1

Array: 6 1 1 6 8 4 5 1 1 1 7 7

Your program: should be named MinilabReview.java and will create an array of (pseudo)random ints and present a menu to the user to choose what array manipulations to do.   Specifically, the program should:

  • Declare constants to specify the maximum integer that the array can contain (set to 8) and the integer whose occurrances will be counted (set to 3, to be used in one of the menu options).
  • Ask the user to enter a “seed” for the generation of random numbers (this is so everyone’s results will be the same, even though random).
  • Ask the user what the size of the array should be. Read in the size; it should be greater than 1. Keep making the user re-enter the value as long as it is out of bounds.
  • Create a new random number generator using the seed.
  • Create the array and fill it in with random numbers from your random number generator.   (Everyone’s random numbers therefore array elements should be in the range 0 to <predefined maximum> and everyone’s random numbers should match).
  • Show the user a menu of options (see examples that are given). Implement each option.   The output should be in the exact same format as the example. Finally, the menu should repeat until the user chooses the exit option.

Examples: Please see the MinilabReviewExample1.pdf and MinilabReviewExample2.pdf that you are given for rather long examples of running the program.   Please note:

  • If you use the same seed as in an example and use the Random number generator correctly, your results should be the same as the example.
  • Please be sure that the formatting is EXACT, including words, blank lines, spaces, and tabs.
  • Not all of the options nor all of the error checking may have been done in a given example, so you may have to add some test cases.
  • There is 1 space after each of the outputs (Array: ) or (Length: ) or (prompts)
  • There are 2 spaces between each element when the array is listed

There are tabs before and after each option number when the menu is printed.

Solutions

Expert Solution

The required Java code is

package sample;

import java.util.Random;
import java.util.Scanner;

public class MinilabReview {
    public static void  main(String args[]){
        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter a seed:");
        int seed_val = sc.nextInt();
        System.out.println("Please enter the size of the array:");
        int size;
        while(true){
            size = sc.nextInt();
            if (size>1){
                break;
            }
            System.out.println("Array size must be greater than 1. Please reenter:");
        }
        int[] arr = new int[size];
        Random random = new Random();
        random.setSeed(new Long(seed_val));
        for (int i =0;i<size;i++){
            arr[i]=random.nextInt(10);

        }
        
        while(true){
            System.out.println("\nPlease choose an option:\n" +
                    "1 Print the array\n" +
                    "2 Find the average\n" +
                    "3 Find the largest element\n" +
                    "4 Count how many times 3 occurred\n" +
                    "5 Count how many elements are less than half of the first element\n" +
                    "6 Find how many times numbers repeat consecutively\n" +
                    "7 Swap the first and last elements\n" +
                    "8 Exit"
            );
            int option = sc.nextInt();
            if(option==8){
                break;
            }

            if (option==1){
                System.out.print("Array: ");
                for(int i =0;i<size;i++){
                    System.out.print(arr[i]+" ");
                }

            }else if (option==2){
                double aver = average(arr,size);
                System.out.println("Average: "+aver);
            }else if(option==3){
                int max = largest(arr,size);
                System.out.println("Largest: "+max);
            }else if(option==4){
                int count = Occurence(arr,size);
                System.out.println("Number of times 3 occurred: "+ count);
            }else if (option==5){
                int count = lessthfir(arr,size);
                System.out.println("Number of elements that are less than half of the first element: "+ count);
            }else if(option==6){
                repeconse(arr,size);
            }else if (option==7){
                int x = arr[0];
                arr[0]=arr[size-1];
                arr[size-1]=x;
            }

        }


    }
    // Function that return average of an array.
    static double average(int a[], int n)
    {

        // Find sum of array element
        float sum = 0f;

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

        return sum/n;
    }
    static int largest(int a[],int size){
        //initializing first value as max
        int max = a[0];
        for(int i =0;i<size;i++){
            if (a[i] > max)
                max = a[i];
        }
        return max;
    }
    static  int Occurence(int a[],int size){
        int count=0;
        for (int i =0 ;i<size;i++){
            if(a[i]==3){
                count++;
            }
        }
        return count;
    }
    static int lessthfir(int a[],int size){
        int count =0;
        int val = new Integer(a[0]/2);
        for (int i =1 ;i<size;i++){
            if(a[i]<val){
                count++;
            }
        }
        return count;
    }
    static void repeconse(int a[],int size){
        int start = a[0];
        int i =1;
        int count =0;
        while (i<size){
            if(start==a[i]){
                count++;
            }else{
                if(count>0){
                    System.out.println(start+" number is repeating"+(count+1)+" time consecutively");
                }
                count=0;
                start=a[i];
            }
            i++;
        }

    }

}

The output which I got while running it.

Please enter a seed:
1
Please enter the size of the array:
9

Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
1
Array: 5 8 7 3 4 4 4 6 8
Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
2
Average: 5.44444465637207

Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
3
Largest: 8

Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
4
Number of times 3 occurred: 1

Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
4
Number of times 3 occurred: 1

Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
5
Number of elements that are less than half of the first element: 0

Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
6
4 number is repeating3 time consecutively

Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
7

Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
1
Array: 8 8 7 3 4 4 4 6 5
Please choose an option:
1 Print the array
2 Find the average
3 Find the largest element
4 Count how many times 3 occurred
5 Count how many elements are less than half of the first element
6 Find how many times numbers repeat consecutively
7 Swap the first and last elements
8 Exit
8

I hope you got the answer and understand it.

If you have any doubts you can ask in comments. I will help with it.

Thank you:):)


Related Solutions

1) Write a function searchValue that accepts an array of integers, the size of the array,...
1) Write a function searchValue that accepts an array of integers, the size of the array, and an integer. Find the last occurrence of the integer passed in as an input argument in the array. Return the index of the last occurrence of the value. If the value is not found, return a -1 2) Write the line of code to call the previous function assuming you have an array vec with length n, and are looking for the number...
//   Given an array of size 9, with the values of 1-9, determine if the array...
//   Given an array of size 9, with the values of 1-9, determine if the array //   is valid or not. //   Display a message stating the row is VALId, or state its INVALID and what //   was the index number that determined the data invalid. // //   Use this java code as a start of your code. //   Test the array data by changing the values. //============================================================================= import java.util.*;    public class Soduko_ValidateRow    { public static void main(String...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
Write a Python function that accepts three arguments: an array, the size of the array, and...
Write a Python function that accepts three arguments: an array, the size of the array, and a number n. Assume that array contains integers. The function should display all integers in the array that are greater than the number n. Test your function.
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
1. With respect to resizable arrays, establish the relationship between the size of the array and...
1. With respect to resizable arrays, establish the relationship between the size of the array and the time it takes to perform random access (read a the value stored at a given position somewhere in the array). Provide empirical evidence to support your answer.
1. We have an array A of size n. There are only positive integers in this...
1. We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. Design an efficient algorithm to find the maximum difference between any two...
Inc++ seed the randomnumber generator, one time only with 2020 . Create an array...
In c++ seed the random number generator, one time only with 2020 . Create an array with 100 elements. Using a function fill the array with 100 numbers in the range of 101 - 199 that are based off the random number generator. Using a second function search the array and return the smallest value and the index of the element that contains the minimum value.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT