Write a program that declares an array numList of 100 elements of type int. Init
ID: 3547816 • 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 take 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
class array{
public void print(int a[])
{
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
System.out.printf("%d ",a[i*10+j]);
}
}
}
public double average(int a[])
{
double avg;
int sum=0;
for(int i=0;i<100;i++)
{
sum+=a[i];
}
avg = sum/100;
return avg;
}
public void main(String [] args)
{
int[] a = new int[100];
for(int i=0;i<100;i++)
{
a[i] = (int)(Math.random());
}
print(a);
double avg = average(a);
System.out.println(avg);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.