Last Updated: 2017-10-10 Tue 16:37

CSCI 1103 Lab05: Arrays, Review

CODE DISTRIBUTION: lab05-code.zip

  • Download the code distribution every lab
  • See further setup instructions below

CHANGELOG:

1 Rationale

Arrays are the first and most primary data structure of note in computer science. Basic mastery of fixed size blocks of memory that hold the same type of thing will allow programs to handle a much broader cadre of problems and is essential knowledge to build more complex data structures. This lab has a couple brief problems pertaining to using arrays.

This lab also prevents several review problems which are intended to help review for the upcoming exam.

Associated Reading: Eck Chapter 3

Chapter 3.8 introduces arrays and basics of processing them in loops. Chapter 7.1-2 discusses arrays in more detail. Read these sections if you are eager to get more details sooner rather than later knowing that we will cover all this material later in the course.

2 Download Lab Code and Setup

As in Lab01, download the code pack linked at the top of the page. Unzip this which will create a folder and create your files in that folder.

File State Notes
PARTNERS.txt Edit Fill in your name and the name of your partner in this text file
TextIO.java Provided Allows easy input calls like TextIO.getInt()
DotProductTests.java Testing Tests for problem 1
KTests.java Testing Utility routines for all tests
junit-1103.jar Testing For testing, don't try to edit this one
DotProduct.java Create Create this file for Problem 1

3 Problem 1: Dot Product (TESTED)

The dot product is a simple mathematical operation that combines two equal length sequences of numbers into a single number. Given two sequences (vectors) like:

a = { 1, 3, 2, 4, 7}
b = { 5, 6, 8, 0, 9}
index 0  1  2  3  4

the dot produce is computed by a multiplying each corresponding element and summing the products as in

dot( a , b) = a[0]*b[0] + a[1]*b[1] + a[2]*b[2] + a[3]*b[3] + a[4]*b[4]
            =    1*5    +    3*6    +    2*8    +    4*0    +    7*9
            = 102

The dot product only works for two equal length sequences so here are some valid and invalid examples.

x = { 2, 9, 1, 3}
y = { 4, 5, 7, 5}
index 0  1  2  3     VALID: equal length

dot( x, y) = 2*4 + 9*5 + 1*7 + 3*5
           = 75

z = { 7, 1, 4}
w = { 2, 3, 5, 6}
index 0  1  2  3     INVALID: unequal lengths

The goal of this problem is to get two equal length sequences of numbers from the user, store each in an array, the compute the dot product of the vectors. Unequal lengths will be checked to prevent invalid dot products from being computed.

3.1 Basic Approach

Create your program in DotProduct.java.

  • Prompt for and retrieve the first sequence length
  • Allocate an array of the specified size for the first sequence
  • Prompt for the number of elements specified
  • Read these elements in a loop into the elements of the array
  • Do the above steps again to get a second sequence in a second array
  • Check that the two sequences are of equal length
  • If not, print a message of the form
    INVALID: seq1 length 3, seq2 length 4
                         ^              ^
    

    where the two integers indicated are the lengths of the sequences given.

  • If the two sequences are of equal length, compute their dot product.
    • Establish a variable to hold the sum of all products which starts at 0
    • Use a loop to iterate through the arrays
    • Multiply the two element [0]'s, and add their product to the sum
    • Multiply the two element [1]'s, and add their product to the sum
    • Repeat this process to the end of the array
  • Print the resulting dot product in a message like
    Dot Product is: 75
    

3.2 Demos

> javac DotProduct.java
> java DotProduct
Enter length of sequence 1: (ex 4)
5
Enter 5 integers: (ex 19)
1 3 2 4 7
Enter length of sequence 2: (ex 4)
5
Enter 5 integers: (ex 19)
5 6 8 0 9
Dot Product is: 102

> java DotProduct
Enter length of sequence 1: (ex 4)
4
Enter 4 integers: (ex 19)
2 9 1 3
Enter length of sequence 2: (ex 4)
4
Enter 4 integers: (ex 19)
4 5 7 5
Dot Product is: 75

> java DotProduct
Enter length of sequence 1: (ex 4)
3
Enter 3 integers: (ex 19)
7 1 4
Enter length of sequence 2: (ex 4)
4
Enter 4 integers: (ex 19)
2 3 5 6
INVALID: seq1 length 3, seq2 length 4
>

4 Getting Credit for this Lab

4.1 Demonstrate your code to Lab Staff (40%)

You should feel free at any time to get help from lab staff as you get stuck on the exercises.

By the end of the lab, make sure to demonstrate your code to a staff member. This will ensure that you receive full credit on the lab. Staff might ask you to

  • Change what is printed
  • Compile and run on the command line
  • Run tests in DrJava or on the command line
  • Show how to zip the folder with your programs in it

Be ready to do any or all of these.

4.2 Zip and Submit (60%)

Ensure that file PARTNERS.txt has your name and the name of your partner in it. This fill is located with the other Java files for the lab and can be edited with DrJava.

Once your are confident your code is working, you are ready to submit. Ensure your folder has all of the required files. Create a zip archive of your lab folder and submit it to blackboard.

On Canvas:

  • Click on the Assignments section
  • Click on the appropriate link for this lab
  • Scroll down to "Attach a File"
  • Click "Browse My Computer"
  • Select you Zip file and press OK

5 Review Problems

The following problems are meant to help review material covered so far in the course. There are no tests and the "specifications" for the problems are a bit shorter but the problems cover most of what can be expected to appear on Exam 1.

5.1 Simple Input/Output

  • Prompt a user for their age in years
  • Print the approximate number of weeks and days they have been alive

Example runs

> javac Age.java

> java Age
Enter your age in years: (ex 13)
13
You've been around for 676 weeks and 4745 days

> java Age
Enter your age in years: (ex 13)
25
You've been around for 1300 weeks and 9125 days

5.2 Conditionals

  • Ask a user for their income in dollars
  • Use a conditional to determine if their income prints out a message indicating whether the user is among the
    • Top 10% of earners: $133,445 or more
    • Top 5% of earners: $214,462 or more
    • Top 1% of earners: $465,626 or more
    • Source Investopedia, 9/15/2016
  • Note that you may want to adjust the order of which category you check for first

Example runs

> javac Income.java

> java Income
Enter your yearly income: (ex: 45698.78)
30000.01
Not in the upper brackets

> java Income
Enter your yearly income: (ex: 45698.78)
80000.01
Not in the upper brackets

> java Income
Enter your yearly income: (ex: 45698.78)
180321.19
Top 10% of earners

> java Income
Enter your yearly income: (ex: 45698.78)
200213.91
Top 5% of earners

> java Income
Enter your yearly income: (ex: 45698.78)
1234567.89
Top 1% of earners

5.3 Loop Printing

  • Write a program that produces the following kind of output
    By 1s:  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 
    By 2s:  2  4  6  8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 
    By 3s:  3  6  9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 
    By 4s:  4  8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80
    
  • You can use a series of 4 individual loops for this
  • Adjust the starting conditions, termination condition, and update to each loop
  • Use a printf("%2d ",x) to get sequence numbers to line up properly

5.4 Looped Input

  • Write a program that repeatedly asks a user for numbers
  • Use a special input number like -1 that, when typed, causes the loop to end
  • After the end of the loop, print out the maximum, minimum, and count of numbers that were entered
  • Make sure NOT to include the special quit value in among the max/min/count
  • No arrays are necessary for this problem
  • Do NOT ask ahead of time how many numbers will be entered

Example runs are below.

> javac MaxMin.java

> java MaxMin
Enter a number (-1 to quit):
5
Enter a number (-1 to quit):
7
Enter a number (-1 to quit):
3
Enter a number (-1 to quit):
10
Enter a number (-1 to quit):
8
Enter a number (-1 to quit):
-1
5 numbers entered, max is 10, min is 3

> java MaxMin
Enter a number (-1 to quit):
25
Enter a number (-1 to quit):
150
Enter a number (-1 to quit):
13
Enter a number (-1 to quit):
0
Enter a number (-1 to quit):
-1
4 numbers entered, max is 150, min is 0

5.5 Table Power

  • Write a program which prints a table of bases and powers
  • Each row represents a base raised to progressively larger powers
  • The formatting for the table below is optional but good practice to get headers and columns aligned correctly
  • Note that this problem combines two concepts covered separately earlier
    • Raising integers to powers using a loop
    • Printing tables of aligned numbers

    Reviewing solutions to these problems is a good way to get a start here.

  • One can do this with either a series of 3 nested loops
    • Outer loop over rows
    • Middle loop over columns
    • Inner loop to raise a base to a power
  • Alternatively, one can get away with only 2 nested loops
    • Outer loop over rows
    • Inner loop over columns combined with raising base to a power

    This solution takes a little more cleverness.

Sample runs

> javac PowerTable.java 

> java PowerTable
Enter max base and column: (ex: 4 6)
2 8
   power     1     2     3     4     5     6     7     8
 base +=================================================
    1 :      1     1     1     1     1     1     1     1
    2 :      2     4     8    16    32    64   128   256

> java PowerTable
Enter max base and column: (ex: 4 6)
3 8
   power     1     2     3     4     5     6     7     8
 base +=================================================
    1 :      1     1     1     1     1     1     1     1
    2 :      2     4     8    16    32    64   128   256
    3 :      3     9    27    81   243   729  2187  6561

> java PowerTable
Enter max base and column: (ex: 4 6)
5 7
   power     1     2     3     4     5     6     7
 base +===========================================
    1 :      1     1     1     1     1     1     1
    2 :      2     4     8    16    32    64   128
    3 :      3     9    27    81   243   729  2187
    4 :      4    16    64   256  1024  4096 16384
    5 :      5    25   125   625  3125 15625 78125

5.6 Array Processing

  • Prompt the user for 8 input numbers
  • If the user enters the number 0 to 7 in any order, print the message All there!
  • If the user fails to enter any integer between 0 and 7, print a message indicating the missing values after collecting all inputs.
  • Missing inputs can be due to entering a number smaller than 0 or larger than 7, or by entering a number between 0 and 7 more than once thus "using up" one of the inputs
  • You will need an array of length 8 for this, likely of booleans which indicate whether the number at a given index has been entered or not
  • Make sure to do bounds checking on the array to prevent out of bounds accesses

Sample runs:

> javac AllThere.java

> java AllThere
Enter 8 integers. We'll check for alll values 0 to 7:
0 5 2 1 6 7 3 4
All there!

> java AllThere
Enter 8 integers. We'll check for alll values 0 to 7:
1 1 2 2 5 5 7 7
Missing 0
Missing 3
Missing 4
Missing 6

> java AllThere
Enter 8 integers. We'll check for alll values 0 to 7:
7 6 5 7 6 5 4 0
Missing 1
Missing 2
Missing 3

> java AllThere
Enter 8 integers. We'll check for alll values 0 to 7:
-2 -1 12 4 0 1 2 3
Missing 5
Missing 6
Missing 7

Author: Chris Kauffman (kauffman@cs.gmu.edu)
Date: 2017-10-10 Tue 16:37