can please someone solve this for me? Exercise#1: Random Numbers Write the void
ID: 3836858 • Letter: C
Question
can please someone solve this for me?
Exercise#1: Random Numbers
Write the void function generate(int N, in A[ ]) that generates and prints N numbers each between 12 and 83 inclusive.
Write the function average(int, int [ ]) that returns the average of the elements of an array.
Write the function frequency(int, int [ ], float, int &) that counts how many element in the array is above the average and how many element is below the average.
Write the function main( ) that reads N, calls the function generate( ), calls the function average( ), calls the function frequency( ), and prints the result, see sample input / output.
Sample input/output:
enter number of random number: 11
the 11 random number are: 76 82 55 53 40 69 58 55 57 30 76
the averag of the 11 random number is : 59.1818
there are 7 number below the average and 4 numbers above the average
Explanation / Answer
package randomaverage;
import java.util.Random;
import java.util.Scanner;
public class RandomAverage {
public static int[] generate(int N, int A[]){
Random rand = new Random();
for(int randCount=0; randCount < N ; randCount++){
A[randCount] = rand.nextInt((83 - 12) + 1) + 12;
}
return A;
}
public static double average(int[] randArr){
double randAverage =0;
for(int randCount=0; randCount < randArr.length ; randCount++){
randAverage = randAverage + randArr[randCount];
}
return (double)randAverage/randArr.length ;
}
public static int[] frequency(int[] randArr, double randAverage){
int[] countArr = new int[2];
int aboveAverageCount = 0;
int belowAverageCount = 0;
for(int randCount=0; randCount < randArr.length ; randCount++){
if(randArr[randCount] > randAverage){
aboveAverageCount = aboveAverageCount+1;
}
else if(randArr[randCount] < randAverage){
belowAverageCount = belowAverageCount+1;
}
}
countArr[0] = belowAverageCount;
countArr[1] = aboveAverageCount;
return countArr ;
}
public static void main(String[] args) {
Scanner inputSc=new Scanner(System.in);
System.out.println("Enter number of random number: ");
int numOfRandInt=inputSc.nextInt();
int randArrayValue[] = new int[numOfRandInt];
int randArrayValueResult[] = new int[numOfRandInt];
double averageRand = 0;
System.out.print("The " + numOfRandInt + " random numbers are : ");
randArrayValueResult = generate(numOfRandInt, randArrayValue);
for(int i=0; i < numOfRandInt ; i++){
System.out.print(randArrayValueResult[i] + " ");
}
System.out.println();
System.out.print("The average of the " + numOfRandInt + " random numbers is : " );
averageRand = average(randArrayValueResult);
System.out.println(averageRand);
System.out.print("There are " + frequency(randArrayValueResult,averageRand)[0] + " numbers below the average and " + frequency(randArrayValueResult,averageRand)[1] + " numbers above the average." );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.