Question

COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing Problem Description Ali Wali o

Java code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code for the above case study is as follows:

//importing required packages
import java.util.*;
//main class
class coconut
{
        //function to construct pairs
        public static int[] pair(int[] arr)
        {
                //variables to store max,min and frequencies
                int max=arr[0], min=arr[0],freq1=0,freq2=0;
                //loop through the entire array
                for(int i=0;i<arr.length;i++)
                {
                        //array value less than the min
                        if(arr[i]<min)
                        {
                                //min set to array value
                                min=arr[i];
                                //frequency set to 1
                                freq1=1;
                        }
                        //if min value occurs more than one
                        else if(arr[i]==min)
                        {
                                //incrementing
                                freq1++;
                        }
                        //array value greater than the max
                        if(arr[i]>max)
                        {
                                //max set to array value
                                max=arr[i];
                                //frequency set to 1
                                freq2=1;
                        }
                        //if max value occurs more than one
                        else if(arr[i]==max)
                        {
                                //incrementing
                                freq2++;
                        }
                }
                //storing the results in array
                int[] res=new int[4];
                res[0]=min;
                res[1]=freq1;
                res[2]=max;
                res[3]=freq2;
                //return the array
                return(res);
        }
        //main function
        public static void main(String[] args) {
                //scanner object to take input
                Scanner in=new Scanner(System.in);
                //test cases
                int n=in.nextInt();
                //to point the index of resultant array
                int x=0;
                //allocating memory for resultant array
                int size=(n*4)+1;
                int[] res=new int[size];
                
                for(int i=0;i<n;i++)
                {
                        //size of the input array
                        int n1=in.nextInt();
                        int[] arr=new int[n1];
                        //taking input of array
                        for(int j=0;j<n1;j++)
                        {
                                int v=in.nextInt();
                                arr[j]=v;
                        }
                        //to store pair
                        int[] res1=new int[4];
                        //calling the function
                        res1=pair(arr);
                        //storing the pair value to resultant array
                        for(int k=0;k<4;k++)
                        {
                                res[x]=res1[k];
                                //index incremented
                                x++;
                        }
                }
                //printing the array
                int c=0;
                for(int i=0;i<n;i++)
                {
                        //cases
                        System.out.println("Case "+(i+1)+": ");
                        //printing values in pairs
                        for(int k=0;k<4;k++)
                        {
                                System.out.print(res[c]+" ");
                                c++;
                        }
                        System.out.println();
                }
        }
}

Output:

In the above code snippet a function named pair is used to calculated the minimum and maximum numbers occurrences of the numbers in a given array. The function takes an array and also return the result of the array. For storing the maximum and minimum values in the given array two variables named max and min is declared and for storing frequencies freq1 and freq2 is declared. The value of max and min set to the first value of the given array. A loop is executed through the entire array and in the array the value of array is checked if it is greater than the maximum value or less than the minimum and if so the value of max is set to the array value and the value of min is set to the minimum value respectively. if the min or max value occurs more than once then the frequency value of them are also incremented. After that the max, min and their frequency values are stored in an array and returned.

In the main function first of all test case value are taken by the user and stored in a variable named n. A resultant array is also declared with the size of (n*4) as per test case it will return two pairs. In the loop the inputs of array are taken and stored in an array variable defined arr. The function pair is called and the result is stored in res1 array. After that the content of res1 array are copied in the main resultant array res and for doing that a variable x is used which increments its value after storing value in the res array.

For printing the array a counter is used which will keep track of the index of the resultant array. In the test case loop one more loop is used which will execute up to 4 is basically used to separate each test case values. In the case study it is defined that the output should be printed using "case #x", but in the sample output it is ignored. You can make changes to the code as per your specifications. Refer to the screenshot of the code for better understanding.

Screenshots of the code:

Add a comment
Know the answer?
Add Answer to:
Java code COCONUT KELAPA4 4A. Input Standard input Output Standard output Topic Array & Array Processing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing!...

    Java code INNER PRODUCT 4E Input Standard input Output Standard output Topic Array & Array Processing! Problem Description The inner product (also known as the dot product or scalar product) of the elements of set A and the elements of set B of size is defined as the sum of the products of corresponding terms. For example, given set A as the array (2, 3, 4, 5, 6, 7, 8, 9; and set B as 6,5, 4, 3, 2, 7,...

  • Java code BIRTHDAY GRAPH5 4B Input Standard input Output Standard output Topic Array & Array Processing...

    Java code BIRTHDAY GRAPH5 4B Input Standard input Output Standard output Topic Array & Array Processing Birthday Graph Problem Description Everyone loves to be celebrated on their birthdays. Birthday celebration can encourage positive social E interaction among co-workers, foster friendship among classmates or even strengthen bond between E BOBO Birthday graph can be display in many forms. It can a creative drawing consists of cupcakes, balloons, UU candles with names, or it can be in the form of simple bar...

  • Java code ABOVE AVERAGE 40 Input Standard input Output Standard output Topic Array & Array Processing...

    Java code ABOVE AVERAGE 40 Input Standard input Output Standard output Topic Array & Array Processing Problem Description Understanding how to interpret test scores is a valuable skill for every teacher. That's because test score interpretation enables teachers to understand how their students' performance for each tests of the course. Average test score is one of the indicators to determine the class performance for the test. For each test, we need to calculate the average score and determine the percentage...

  • Code with Java using arrays and Scanner only ( input should end with 0 to terminate...

    Code with Java using arrays and Scanner only ( input should end with 0 to terminate the program) Everyone loves to be celebrated on their birthdays. Birthday celebration can encourage positive social interaction among co-workers, foster friendship among classmates or even strengthen bond between families. Birthday graph can be display in many forms. It can a creative drawing consists of cupcakes, balloons, candles with names, or it can be in the form of simple bar chart to indicate the birthday...

  • ENGR 200 FALL 2019 P8: TEMPERATURE ANALYSIS (input/output files, if structures, for loops, two-dimensional array, one-dimensional...

    ENGR 200 FALL 2019 P8: TEMPERATURE ANALYSIS (input/output files, if structures, for loops, two-dimensional array, one-dimensional character array) DUE: November 14, 2019, at 11:59 p.m. US Central Time POINTS: 70 INTRODUCTION: You are working on development of a new power plant for Centerville, USA. Average monthly temperature data has been collected as part of designing the new power plant, and you are required to develop a computer program that will perform averaging analysis. The input data file is called temp....

  • Please write code in java You’re given a chess board with dimension n x n. There’s...

    Please write code in java You’re given a chess board with dimension n x n. There’s a king at the bottom right square of the board marked with s. The king needs to reach the top left square marked with e. The rest of the squares are labeled either with an integer p (marking a point) or with x marking an obstacle. Note that the king can move up, left and up-left (diagonal) only. Find the maximum points the king...

  • Java 8 Braces You are designing a compiler for a C++ program and need to check...

    Java 8 Braces You are designing a compiler for a C++ program and need to check that braces in any given file are balanced Braces in a string are considered to be balanced if the following criteria are met: All braces must be closed. Braces come in pairs of the form 0.0andl1. The left brace opens the pair, and the right one closes it In any set of nested braces, the braces between any pair must be closed For example,...

  • Write a Java program, In this project, you are going to build a max-heap using array...

    Write a Java program, In this project, you are going to build a max-heap using array representation. In particular, your program should: • Implement two methods of building a max-heap. o Using sequential insertions (its time complexity: ?(?????), by successively applying the regular add method). o Using the optimal method (its time complexity: ?(?), the “smart” way we learned in class). For both methods, your implementations need to keep track of how many swaps (swapping parent and child) are required...

  • Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use...

    Must be done in Java. Provide the rest of the code with full comments and explanation and with proper indentation. Use simple methods for better understanding. Must compile. CODE PROVIDED: import java.util.ArrayList; import java.util.Scanner; public class Problem3 {    public static void main( String [] args ) {        Scanner in = new Scanner( System.in );        //PLEASE START YOUR WORK HERE        //**********************************************************                                                  //**********************************************************        // PLEASE END YOUR WORK HERE        System.out.print("END OF OUTPUT");    } } 20 points! PROBLEM 3: EXTENDED FAMILY Complete this...

  • In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list...

    In Java(using BlueJ) Purpose Purpose is to practice using file input and output, and array list of objects. Also, this lab specification tells you only what to do, you now have more responsibility to design how to do it. Problem description You are given a text file called 'Students.txt' that contains information on many students. Your program reads the file, creating many Student objects, all of which will be stored into an array list of Student objects, in the Students...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT