Write a program that declares an array numList of 100 elements of type int . Ini
ID: 3547830 • Letter: W
Question
Write a program that declares an array numList of 100 elements of type int. Initialize the array with random numbers between 1 and 500. Then use user-defined methods to do the following:
- Write a method that takes as its parameter as int array, and prints the array so that 10 elements per line are printed.
- Write a method that takes as its parameter as int array, and returns the average of the elements in the array.
- Write a method that takes as its parameter as int array, and returns the smallest element in the array.
- In main method, call these user-defined methods and display the results.
Explanation / Answer
import java.io.*;
import java.util.Random;
class random{
public static void main(String[] args) throws IOException
{
int i,arraySize=100;
int[] A = new int[arraySize];
int limit = 501; //random number generator generates upto one less
Random generator = new Random();
for (i=0;i<arraySize;i++){
A[i] = generator.nextInt(limit);
}
display(A);
average(A);
}
public static void display(int[] A){
int i=0,j=0;
for (i=0;i<10;i++){
for (j=0;j<10;j++){
System.out.print(A[j+(i*10)]+" ");
}
System.out.println();
}
}
public static void average(int[] A){
int i=0;
int avg=0;
for (i=0;i<100;i++){
avg=avg+A[i];
}
avg=avg/100;
System.out.println("Average = "+avg);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.