1a.Write a program to input 10 integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.)
b. Repeat 1a, keeping track both the maximum element in the array and the index number for the maximum. After displaying the numbers, print these two messages
The maximum value is: _______
This is element number _______ in the list of numbers.
Have your program display the correct values in place in the underlines in the messages.
c. Repeat 1b, but have your program locate the minimum of the data entered.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// input 10 integer numbers into an array named fmax and determine the maximum
// value entered.
int[] fmax = new int[10];
for (int i = 0; i < fmax.length; i++) {
System.out.print("Enter number " + (i + 1) + ": ");
fmax[i] = in.nextInt();
}
int maximumValue = fmax[0];
int maximumValueIndex = 0;
int minimumValue = fmax[0];
int minimumValueIndex = 0;
for (int i = 0; i < fmax.length; i++) {
if (fmax[i] > maximumValue) {
maximumValue = fmax[i];
maximumValueIndex = i;
}
if (fmax[i] < minimumValue) {
minimumValue = fmax[i];
minimumValueIndex = i;
}
}
System.out.println("The maximum value is: " + maximumValue);
System.out.println("This is element number " + maximumValueIndex + " in the list of numbers.");
System.out.println("The minimum value is: " + minimumValue);
System.out.println("This is element number " + minimumValueIndex + " in the list of numbers.");
in.close();
}
}
Comments
Leave a comment