Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Arrays Programming Assignment 1 Template
- public class ArraysProblem2
- {
- public static void main()
- {
- //Declare an array for the 300 randomly generated numbers
- int [] array = new int [300];
- //Declare an array for the 51 numbers to count occurences of
- int [] array2 = new int [51];
- //Fill the numbers array with random numbers 0-50 inclusive
- for(int i = 0; i<array.length;i++)
- array[i] = (int) (Math.random()*51);
- for(int i = 0; i<array2.length;i++)
- array2[i] = (int) (Math.random()*51);
- //Print the numbers array 10 numbers per line separated by tabs
- for(int i = 0; i <= 29; i++)
- {
- for(int j = 0; j <= 9; j++)
- {
- System.out.print(array[i+j]+"\t");
- }
- System.out.print("\n");
- }
- System.out.println("--------------------------------------------");
- //Loop through the numbers array incrementing
- // indicis of the occurences array appropriately
- int num = 0;
- int count = 0;
- for(int i = 0;i<array.length;i++)
- {
- count = 0;
- num = array[i];
- for(int j = 0; j<array.length;j++)
- {
- if(num == array[j])
- count++;
- }
- System.out.println(num+":"+count);
- }
- //Loop through the occurences array printing the numbers
- // and their occurences
- for(int i = 0;i<array2.length;i++)
- {
- count = 0;
- num = array2[i];
- for(int j = 0; j<array2.length;j++)
- {
- if(num == array2[j])
- count++;
- }
- System.out.println(num+":"+count);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement