Topic 8 - Sample Questions - Arrays (10 days)

Use array declared in private area

private int[] ar = new int[20];

1. Write a function that will let you enter numbers into an array. public void enter() 2. Write a function that will fill a array of predetermined size with random numbers between 1 and 100. public void fill() 3. Write a function that will print out the elements in an array. public void print () 4. Write a function that will return the sum of the numbers in an array. public int getSum() 5. Return biggest number in an array. public int getBiggest() 6. Find average of numbers in an array. public double getAvg() 7. Print array in reverse order. public void printReverse() 8. Print every other element in the array. public void printEveryOther () 9. Return true if average of grades in array >= 90. public boolean honors () 10. Return letter grade associated with average. public String getLetter() 11. Return how many odd numbers were in the array. public int countOdd() 12. Print out elements in second half of array only. public void printHalf() 13. Return average of odd numbers in array. public double avgOfOdd() 14. Change any odd numbers to next even number. public void change() 15. Sort the array into ascending order using a selection sort public void selectionSort() 16. Switch every other element in the array. public void switcher () 17. Return the average deviation. You must call function getAvg (#6). // average deviation is the average of how far each number is away from the average public double avgDeviation() 18. Return location of first occurence of number in an array. Return -1 if number is not there. public int linear(int num) 19. Count the number of occurences of a number in a array. public static int count( int num) 20. Shift numbers in array so that first number is gone. // will have last number twice - in last 2 spots public static void shift() 21. Change array so everyone shifts down one spot and one in last spot goes into first position. public static void shift2() 22. Write a function that will return true if a number is in the array. public static boolean isThere(int num) 23. Given parallel array of names and phone numbers print out the phone number for a given name or a message that the person is not in the list (this method is not accessing array in private area) public static void printPhone(String [] na, String[] te, String name)
  1. Write a functtion that will turn the array upside down.
    7 9 3 5 12 6 -> 6 12 5 3 9 7

    public void flip()

  2. Say that there was a row of 1000 lights. Someone went down and turned off the
    multiples of 2 (but not 2) the 4th, 6th, 8th etc.
    Then another person went down and turned off the multiples of 3 (but not 3) the
    6th, 9th, 12th etc.
    Then another person went down and turned off the multiples of 4 (but not 4) the 8th, 12th etc. This continues up through 35. Write a program that will simulate this. Use an array from 1 to 1000 of int. Set the array to hold all 1's. When you turn off the light you should change that array spot to a 0.

    When you are done print out the "light bulbs" that are still on. What are these numbers?

  3. public boolean inOrder() // returns true if array is in ascending order

  4. public int fillInSequentialOrder()
    // fill the array in the private area with random numbers until you get one that is in sequential order - use function above to check // set size of array to 10 - print array and ct of how many tries it took you in main program

  5. Write a function that will fill an array with 100 non-repeating random int's between 1 and 200.
    public void fill()

  6. Write a methos sortPairs() which will sort the pairs of data in the array

    For example if the array was 3 5 9 4 12 7 2 11 14 then array would become :
    3 5 4 9 7 12 2 11 14

    Each pair in the array are in sorted order (Note 14 is left alone because it wasn't a pair)

    public void sortPairs()

  7. Write a method 
    public boolean pairsAreSorted()  
    that returns
       whether or not a list of integers is pairwise sorted (true if it is, false
       otherwise).  A list is considered pairwise sorted if each successive pair of
       numbers is in sorted (non-decreasing) order.  For example, if array ar variable
     stores the following sequence of values:
            [3, 8, 2, 5, 19, 24, -3, 0, 4, 4, 8, 205, 42]
       then the following call:
    boolean b = pairsAreSorted(ar)
       should return the value true because the successive pairs of this list are
       all sorted: (3, 8), (2, 5), (19, 24), (-3, 0), (4, 4), (8, 205).  Notice
       that the extra value 42 at the end had no effect on the result because it is
       not part of a pair.
  8. Write a method that will generate 1000 numbers between 1 and 100 and will tell you how many times 1 came out, 2 came out etc. Use an array to keep track. a[1] will keep track of 1's, a[2] will keep track of 2's etc. declare an local array (of size 12) and return it.
    public int[] returnCt()

  9. public int[] merge(int [] a, int[] b) // merge 2 sorted arrays into 1 sorted array
    /* you have to create a new local array of appropriate size to return - enter the originals arrays by hand to ensure they are sorted (just use length = 5) print out new sorted array by sending it to a print method */

  10. We know that when we roll a pair of dice, 7 will probably come out the most. What number will come out the most when we roll 4 dice? Use an array to keep track of the totals (between 4 and 24). Simulate the rolling of the dice 10000 times. Which total came out the most?

  11. public void insertionSort()

  12. public void printLastNames() // print the last names of all the people in the array

  13. public boolean BirthdaysInCommon()
    Fill an array with 10 random numbers from 1 to 365 (representing 10 peoples birthdays). What percentage of the time will there be at least one birthday in common. For example : if the computer picked 47 two times, then that would mean 2 people were born on that day.

    Run the simulation 100000 times and see what percentage of the time it worked.

  14. What number of people's birthday in the array will make the percentage approach 50%?

  15. Fill an array with 10 unique numbers between 1 and 100

  16. Have 2 parallel arrays with names and phone numbers, and find the phone number for the entered name.

  17. Get 1000 random numbers between 1 and 100 (representing student grades) and then print out how many students got a 1, how many got a 2 etc. (histogram)