Lab 4: Loops and arrays
The goal of this lab is to practice writing programs with
loops, switch statements, and arrays.
Make a new directory, as usual.
Then make a file from scratch containing an interactive program
that operates on arrays.
Specifically, which
does the following.
- Asks the user for the size of an array.
- Repeatedly presents the user with the following menu:
- Set a value in the array
- Find the average of the elements in the array
- Find the minimum and maximum values in the array
- Display the contents of the array
- Quit
- The user's menu selection is interpreted using a switch statement.
- If the user selects (1), the program asks for an index and a value to put in
that position.
- Operations (2)-(4) are performed using a for loop.
- If the user enters a number other than (1)-(6), an appropriate message is
displayed and the menu is displayed again.
Turn-in
- Start a script. Remember this is done using the
script
command, giving the name of a script file.
- Delete any class files. This is done using the
rm
command. rm *.class
will delete all files that
end in .class
. Be careful--- don't delete your .java
files!
- Print the file
- Compile the program, as you did before.
- Run the program, as you did above.
- Exit the script.
- Print the script. I recommend using
a2ps -P sp
(name of script file).
a2ps
prints the file in "2-up" mode. Remember that
sp
is the name of the printer.
Make sure you have exited the script file before you print, or else it
will print forever!
Then turn in the hard copy to me.
Thomas VanDrunen
Last modified: Sat Sep 16 21:48:30 CDT 2006