Question

2. Write a counter controlled loop to solve the following problems. Each one will involve an...

2. Write a counter controlled loop to solve the following problems. Each one will involve an array

(MinMax.java) Read in 25 ints from the keyboard, and store them in an array. Then, find the maximum and minimum values in such an array, and display them on the screen.

public class Array-Assignment {

public static void main(String [] args) {

    int [] x = new int[3];

    int [] y = {3, 5, 9, 2};

    x[2] = y[3];

    x[0]++;

    y[1] += y[2] * y[0];

    int [] z = x;

x = y;

x[1] = 4;

}

}

public class Array-Length {

public static void main(String [] args) {

    int [] x = new int[4];

    int [] y = {};

    int [] z = {0};

    System.out.println("x has " + x.length + " elements");

    System.out.println("y has " + y.length + " elements");

    System.out.println("z has " + z.length + " elements");

}

}

public class Array-With-Loop1 {

public static void main(String [] args) {

    int [] x = {-4, 9, 8, 2, -5, 7, 1};

    for(int i=1; i

      x[i] += x[i-1]; // notice: += instead of =

    }

}

}

public class Array-With-Loop2 {

public static void main(String [] args) {

    int [] x = {-4, 9, 8, 2, -5, 7, 1};

    for(int i=1; i

      x[i] = x[i-1];

    }

}

}

public class Array-With-Loop3 {

public static void main(String [] args) {

    int [] x = {-4, 9, 8, 2, -5, 7, 1};

    int val = 0;

    for(int i=1; i

      val = val + x[i];

    }

}

}

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

Code is after the output.

Please feel free to ask doubt/give feedback.

Output:

Answer:

import java.util.Scanner;

public class MinMax{

public static void main(String args[])

{

//create an array of size 25

int a[]=new int[25];

//declare min and max varibales

int min,max;

//create a scanner object to take input from the keyboard

Scanner sc=new Scanner(System.in);

System.out.println("Enter 25 integers");

//for loop, 25 times

for(int i=0;i<25;i++)

{

//scan a[i]

a[i]=sc.nextInt();

}

//initialize min and max to first item of the array

min=max=a[0];

//loop through the items of the array

for(int i=0;i<25;i++)

{

//use Math.min and Math.max to find min/max between previous min/max and a[i]

min=Math.min(min,a[i]);

max=Math.max(max,a[i]);

}

//print the result

System.out.println("Maximum number is "+max+"\nMinimum number is "+min);

}

}

Add a comment
Know the answer?
Add Answer to:
2. Write a counter controlled loop to solve the following problems. Each one will involve an...
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
  • Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array...

    Which of the following are valid array declarations? a. int[] array- new int[10]; b. double [array double[10]; c. charl charArray "Computer Science"; None of the above Analyze the following code: class Test public static void main(Stringl] args) System.out.println(xMethod(10); public static int xMethod(int n) System.out.println("int"); return n; public static long xMethod(long n) System.out.,println("long"); return n The program displays int followed by 10 The program displays long followed by 10. The program does not compile. None of the above. tions 3-4 are...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • 1. Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the...

    1. Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8. What i am given: import java.util.Scanner; public class SumOfExcess { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int NUM_VALS =...

  • 1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program!...

    1) Consider the following Java program: 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4         System.out.println("Hello, World!"); 5     } 6 } What is on line 1? a. a variable declaration b. a statement c. a method (subroutine) definition d. a comment e. a class definition 2) Which one of the following does NOT describe an array? a. It can be used in a for-each loop. b. It has a numbered sequence...

  • Java Questions When creating a for loop, which statement will correctly initialize more than one variable?...

    Java Questions When creating a for loop, which statement will correctly initialize more than one variable? a. for a=1, b=2 c. for(a=1, b=2) b. for(a=1; b=2) d. for(a = 1&& b = 2) A method employee() is returning a double value. Which of the following is the correct way of defining this method? public double employee()                                    c. public int employee() public double employee(int t)                  d. public void employee() The ____ statement is useful when you need to test a...

  • Explain in detail what the code below does: public class MyClass {       public static void...

    Explain in detail what the code below does: public class MyClass {       public static void main(String args[]) {              System.out.println(isUniqueChars("something"));       }       public static boolean isUniqueChars(String str) {             int checker = 0;                                                                                               for (int i = 0; i < str.length(); ++i) {                         int val = str.charAt(i) - 'a';                         if ((checker & (1 << val)) > 0) return false;                         checker |= (1 << val);             }             return true;...

  • Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating...

    Write a method public static ArrayList merge(ArrayList a, ArrayList b) that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elements from the longer array list. For example, if a is 1 4 9 16 and b is 9 7 4 9 11 then merge returns the array list 1 9 4 7 9 4 16 9 11...

  • Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a...

    Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print: 90, 92, 94, 95 Your code's output should end with the last element, without a subsequent comma, space, or newline. import java.util.Scanner; public class PrintWithComma {    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       final int NUM_VALS = 4;       int[] hourlyTemp = new...

  • Write a program that stores a phrase as an array of words, and then prints it...

    Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{    public static void main(String[] args) {    String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...

  • JAVA question For the following code, remember to make a table to keep track of the...

    JAVA question For the following code, remember to make a table to keep track of the various x y z values. public class Mystery6 { public static void main(String[] args) { int x = 1; int y = 2; int z = 3; z = mystery(y, x, y); // Statement 1 System.out.println(x + " " + y + " " + z); // Statement 2 x = mystery(z, y, x); // Statement 3 System.out.println(x + " " + y +...

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