I wrote this code, called arrayHistogram
, that would generate a random number within a set range, and print it in an array randomIntArray
. It would then make another array arrayHist
that would count how many times each number in randomIntArray
occurred. The program works, but, I feel I have a few semantic errors in the syntax, that's not printing arrayHist
correctly. I would like to review it and see where the errors are so I can correct them. Also, If you feel like you can offer me a more efficient methods, and means for this code, feel free to do so, I'll take it into consideration.
{
public static void main(String[] args){
int[] array=randomIntArray(30);//declares the dimensions of the array "randomIntArray"
System.out.println("Scores:");
printArray(array);//calls the scoreHist method to print the "array variable
System.out.println("\n"+"count: ");
printArray(arrayHist(array));//calls score hist, to call arrayHist, to count "array"
System.out.println();
}
public static int randomInt(int low, int high){//the prng
double e;
double x=Math.random();
e=low+x*(high-low);
return (int)e;}
public static int[] randomIntArray(int n){//generates an array of random numbers based on an upper and lower bound
int[] a=new int[n];
for(int i=0;i<a.length;i++)
{a[i]=randomInt(-5,15);}//"-5&15 are the lower and upper bounds of the random number array
return a;}
public static int inRange(int[] a, int low, int high){//checks if the numbers of the random number array are in certain constraints.
int count=0;
for(int i=0;i<a.length;i++)
{if(a[i]>=low&&a[i]<=high)count++;}
return count;}
public static int[] arrayHist(int[] scores){//counts how many of each number occurs in "randomIntArray"
int[] counts=new int[11];
for(int i=0;i<10;i++)
{counts[i]=inRange(scores,i,i++);}
return counts;}
public static void printArray(int[] a){//prints any array, with only one traversal.
for(int i=0;i<a.length;i++)
{System.out.print(" "+a[i]);}
}
}
My goal is to not use any of the array methods Java provides (to challenge myself), so please don't offer them up.