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...
This is a Java program that I am having trouble making. 1- Search for the max...
This is a Java program that I am having trouble making. 1- Search for the max value in the linked list. 2- Search for the min value in the linked list. 3- Swap the node that has the min data value with the max one. (Note: Move the nodes to the new positions). 4- Add the min value and the max value and insert the new node with the calculated value before the last node. I already made a generic...
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 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 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...
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...
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...
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
The solution should be written in Java. Your POSmain program should take three file names from...
The solution should be written in Java. Your POSmain program should take three file names from command line arguments. The first file contains a list of products and their prices; the second and third files are lists of items in two shopping carts of two customers. The POSmain program should first read the price file, then read each of the cart files to load a list of items in a shopping cart and store them in a ShoppingCart objects. The...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT