Arrays: Intro

Screen Shot 2014-11-11 at 7.17.48 AM

Screen Shot 2014-12-07 at 12.13.44 PM

  • Typical array-processing code.
  • Programming with arrays.
    • Zero-based indexing
    • Array length
    • Memory representation
    • Bounds checking
    • Setting array values at compile time
    • Setting array values at run time
  • Shuffling and sampling
    • Exchange
    • Shuffling
    • Hardwired constants
    • Sampling without replacement

 

classwork:
Write a program, ArrayBasics_YI.java to do the following:

  1. Create an array a[] size 10. Assign a random number between 1 and 30 to each element.
    a. Display the first element of the array.
    b. Display the last element of the array assuming you do not know the size of the array.
    c. Display the messages given when you try to access an element with a negative index and with an index beyond the size of the array.
  2. Create two arrays, suit[] and face_value[].
    a. The suit[] array contains the 4 different suits as elements.
    b. The face_value array contains “2”, “3”,…,”Jack”,…,”Ace” elements.
    c. Use the random function to select five cards and display them.
  3. Create two arrays x[] and y[] with N number of elements both. Prompt the user for N. Assign random integers to each element. Then have another loop to find the product of each element wit the same index and add them all together. This operation is called the “dot product”.

Assignments:
ClosestPair_YI.java
Write a program to create an array of 100 random integers between the values of 1 and 1000 and find the two consecutive integers with the smallest difference between the two of them.

MaxInArray_YI.java
Write a program to create an array of 100 random integers between the values of 1 and 1000 and find the largest value.

AverageArray_YI.java
Write a program to create an array of 100 random integers between the values of 1 and 1000 and find the average of all the elements of the array.

PrintArray_YI.java
Write a program to create an array of 100 random integers between the values of 1 and 1000 and print them separated by 3 spaces. Note: Use printf to keep all your numbers tab properly.
A printf format reference page (cheat sheet)

Pop Quiz Array 1
Pop Quiz 1: Fix the problem