1. (Find the smallest element) Write a program to accept 10 numbers from the use
ID: 3529107 • Letter: 1
Question
1. (Find the smallest element) Write a program to accept 10 numbers from the user as a console application. a. These 10 numbers must be double data type, and stored in an array of double numbers. 2. Once the input is complete, write two methods to perform functions on the new array. a. 1 should go through the array and find the smallest element. i. Once array is processed fully, output a message to the user telling them what the lowest element was, as well as which number it was input as. ii. Please note: Java is a zero based array language. The number of the input would have to be one greater to be accurate. iii. Once the method is complete to find, and print, the lowest element: Then write a method to print out the array in its entirety back to the user. a. Requirements: a) Write the program described above implementing arrays. b) Create two methods (aprint and min) to perform the described functions.
Explanation / Answer
import java.util.*;
class array_oper
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double[] a = new double[10];
for(int i =0; i<10; i++)
{
System.out.println(" Enter number " + (i+1) );
a[i] = in.nextDouble();
}
min(a);
aprint(a);
}
// end of main
public static void min(double[] a)
{
double mini = a[0];
int min_index = 0;
for(int i=1; i<10; i++)
{
if(a[i] < mini)
{
mini = a[i];
min_index = i;
}
}
System.out.println(" minimum element is " + mini + " and it is found at index " + min_index);
}
public static void aprint(double[] a)
{
System.out.println(" array elements are ");
for(int i=0; i<10; i++)
{
System.out.print( a[i] + " " );
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.