Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I wrote a program to do this but it isn\'t very fast. Hoping to find a better al

ID: 3550247 • Letter: I

Question

I wrote a program to do this but it isn't very fast. Hoping to find a better alternative.

This problem is similar to program 2.  However, instead of just counting the 7's and 13's, you should count the frequency of all of the numbers from 1 through 44.

After you have counted all of the number, print the number that occurs most frequently.  Display the number of times the most popular number was counted, and then list all of the numbers that were picked that often.  Note that there will always be at lest one number to be printed, but there may be more than one.  You should print all of them.

Repeat the same reporting of the least popular number.

Calculate the average number.  Note that this will be a real number, and it probably will not equal an integer.  Print the average.  The print the integer that is the Ceiling and the Floor values.  Report all of the numbers that are equal to the floor and ceiling values.  Look in the Math API for methods that you can use to calculate the floor and ceiling values.

Finally, print all of the values from 1 through 44 as shown below.   Note that the numbers run DOWN and then across in 4 columns.  The number in parenthesis indicates the number of times that value was picked.  Print a + after numbers that occur more frequently than the average, and - after the numbers that have a frequency less than or equal to the average.


Report:

Explanation / Answer


public class Frequencies {


/*

* Since you didn't mention how to get the input.

* I'm assuming that you already have an array of numbers

* that contain the integer values.

* The core logic will work just fine

* Make sure you initialize your numbers array to avoid null pointer exception

*/

public static void main(String[] args) {


int[] numbers = null;


int[] frequency = new int[43];

// since there are only unique 44 numbers

for(int j=1; j<44; j++){

int count = 0;

for(int i=0; i<numbers.length; i++){

if(numbers[i]==j){

count++;

}

}

frequency[j-1] = count;

}

int max = 0;

int posMax = 0;

for (int k = 1; k < frequency.length; k++){

if (frequency[k] > max){

max = frequency[k];

posMax = k;

}

}

int min = 0;

int posMin = 0;

for (int k = 1; k < frequency.length; k++){

if (frequency[k] < min){

max = frequency[k];

posMin = k;

}

}

int sum = 0;

for(int l=0; l<numbers.length; l++){

sum+=numbers[l];

}

double avg = sum/numbers.length;

System.out.println("Report:");

System.out.println("MOST POPULAR NUMBERS");

System.out.println("The following numbers were picked "+max+" times: "+posMax+1);

System.out.println("LEAST POPULAR NUMBERS");

System.out.println("The following numbers were picked "+min+" times: "+posMin+1);

System.out.println("AVERAGE");

System.out.println("The average was "+avg);

System.out.println("Frequencies:");

for(int i=1; i<=44; i++){

System.out.println(i+"("+frequency[i+1]+")");

}

}


}