Question

In: Computer Science

this should be written in Java- Problem: Min/Max Search by Index Develop a program that, given...

this should be written in Java- Problem: Min/Max Search by Index Develop a program that, given a sequence S of integers as input, produces as output two sequences of positive integers, the first of which indicates all those positions in S at which S's minimum value occurs and the second of which indicates all those positions at which S's maximum value occurs. Positions are numbered starting at zero (0).

Facts ● Scanner has a method that returns a boolean indicating whether a next integer exists in its input stream ( hasNextInt() ) ● Scanner objects can be initialized to to scan String data as input.

Sample input

3

3 6 -1 4 6 5 3

0 0 0 0

-4 45 2 0 3 5 11 -7 854 25 3 -7 4 -3

Sample output

3 6 -1 4 6 5 3

2

1 4

0 0 0 0

0 1 2 3

0 1 2 3

-4 45 2 0 3 5 11 -7 854 25 3 -7 4 -3

7 11

8

Input The input will begin with a single line containing T , the number of test cases to follow. The remaining lines contain the T sequences, one line per sequence. Each of these lines contains the values in the sequence. Each such value is separated from the next by at least one space.

Output For each sequence given as input, there should be four lines of output. The first line echos the given sequence. The second line indicates the positions at which the minimum value occurs. The third line indicates the positions at which the maximum value occurs. The fourth line is blank.

Solutions

Expert Solution

Java Program

import java.util.Scanner; //Import until scanner package for scanner class
class MinMaxSeq //set class name
{
public static void main(String args[]) //Declared main function
{
Scanner sc =new Scanner(System.in); //create object sc of scanner class
int i,len=0;
int seq[]=new int[100]; //initialize array to store all numbers
System.out.println("\n Enter Sequence ");
  
while(sc.hasNextInt()) //to take integer input from user work until user input non-integer value
{
seq[len]=sc.nextInt();
len++;
}
  
System.out.println();
  
for(i=0;i<len;i++). // To print entered series
{
System.out.print(seq[i]+" ");
}
  
int min=seq[0],max=seq[0]; //set minimum and maximum as first number
  
for(i=1;i<len;i++) //find minimum and maximum number among sequence
{
if(min>seq[i])
min=seq[i];
else if(max<seq[i])
max=seq[i];
}
  
System.out.println();
  
for(i=0;i<len;i++) // display index number of minimum number
{
if(min==seq[i])
System.out.print(i+" ");
}
  
System.out.println();
  
for(i=0;i<len;i++) // display index number of maximum number
{
if(max==seq[i])
System.out.print(i+" ");
}
  
}
}

________________________________________________________________

OUTPUT:-


Related Solutions

This program should be written in Java language. Given the uncertainty surrounding the outbreak of the...
This program should be written in Java language. Given the uncertainty surrounding the outbreak of the Coronavirus disease (COVID-19) pandemic, our federal government has to work tirelessly to ensure the distribution of needed resources such as medical essentials, water, food supply among the states, townships, and counties in the time of crisis. You are a software engineer from the Right Resource, a company that delivers logistic solutions to local and state entities (schools, institutions, government offices, etc). You are working...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java...
PROGRAM MUST BE WRITTEN IN JAVAFX Develop a program flowchart and then write a menu-driven Java program that will solve the following problem. The program uses one and two-dimensional arrays to accomplish the tasks specified below. The menu is shown below. Please build a control panel as follows: (Note: the first letter is shown as bold for emphasis and you do not have to make them bold in your program.) Help SetParams FillArray DisplayResults Quit Upon program execution, the screen...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
c++ Write a program that print stars, Max and Min values. It should use the following...
c++ Write a program that print stars, Max and Min values. It should use the following functions: (2 pts) int getNum ( ) should ask the user for a number and return the number. This function should be called by main once for each number to be entered. Input Validation: Do not accept numbers less than -100. (2 pts) void printStars ( int n ) should print n number of stars. If n is less than 0, display "Invalid" message...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key and then takes it out of the tree. The program can be C++/Java or C-style pseudocode. Do not use function call to either Min or delete done in the class. Write this from scratch. (No need to ensure AVL properties, just show it for a generic BST) Node * extract min(Node * x) { }
Develop a Java program for this problem where the user inputs an Earth age and the...
Develop a Java program for this problem where the user inputs an Earth age and the program will then display the age on Mercury, Venus, Jupiter, and Saturn. The values for d are listed in the table. Planet d = Approximate Number of Earth Days for This Planet to Travel Around the Sun Mercury 88 Venus 225 Jupiter 4380 Saturn 10767
Write a program/code that prompts the user for a minimum min and a maximum max. Then...
Write a program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. (WRITTEN IN C) For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
Java Program Sequential Search You are given a sequence of n integers S and a sequence...
Java Program Sequential Search You are given a sequence of n integers S and a sequence of different q integers T. Write a program which outputs C, the number of integers in T which are also in the set S. Do not use any import sort packages. Input: In the first line n is given. In the second line, n integers are given. In the third line q is given. Then, in the fourth line, q integers are given. Output:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT