In section 7.3 of you text, there is a program on pages 325 and 326 called Large
ID: 3708969 • Letter: I
Question
In section 7.3 of you text, there is a program on pages 325 and 326 called LargestInArray.java. For this problem you will modify this program to find both the largest value (the code is already written) and the smallest value (this is the code you will write). Add the necessary code to show both the largest and the smallest values.
import java.uti1.Scanner; This program reads a sequence of values and prints them, marking the largest value. public class LargestInArray public static void main(String[] args) final int LENGTH = 100; double[] valuesnew double LENGTH] int currentSize0; // Read inputs System.out.println("Please enter values, Q to quit:"; Scanner in = new Scanner(System.in); while (in.hasNextDoubleO && currentSizeExplanation / Answer
LargestInArray.java
import java.util.Scanner;
public class LargestInArray {
public static void main(String[] args) {
final int LENGTH=10;//need to change 100
double values[] = new double[LENGTH];
int currentSize = 0;
System.out.println("Please enter values: Q to quit");
Scanner in = new Scanner(System.in);
while(in.hasNextDouble()&& currentSize<values.length) {
values[currentSize]=in.nextDouble();
currentSize++;
}
double largest = values[0];
for(int i=1;i<currentSize;i++) {
if(values[i] > largest) {
largest = values[i];
}
}for(int i=0;i<currentSize;i++) {
System.out.print(values[i]);
if(values[i]==largest) {
System.out.print("<== largest value ");
}
System.out.println();
}
double small = values[0];
for(int i=1;i<currentSize;i++) {
if(values[i] < small) {
small = values[i];
}
}for(int i=0;i<currentSize;i++) {
System.out.print(values[i]);
if(values[i]==small) {
System.out.print("<== smallest value ");
}
System.out.println();
}
}
}
Output:
Please enter values: Q to quit
4
5
6
3
1
2
9
8
7
10
77
4.0
5.0
6.0
3.0
1.0
2.0
9.0
8.0
7.0
10.0<== largest value
4.0
5.0
6.0
3.0
1.0<== smallest value
2.0
9.0
8.0
7.0
10.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.