Lab 6c – Arrays

Create a file named Lab6c.java inside your lab6 directory.  Inside Lab6c.java write a program that satisfies the following requirements.


Inside the class (but after the main method) write a method named printArray that takes an array of integers as an argument.  The method uses a for-loop to print to the screen (on a single line with spaces between them) the elements in the array that is passed to the method.  (Hint: we did this in class on Wednesday)

Inside the main method of the class, do the following:

    • Declare an array that can hold 15 integers.
    • Print the values in the array to the screen by calling the printArray method.
    • Set all of the elements in the array to random integers between 0 and 20.
    • Print the values in the array to the screen by calling the printArray method.
    • Use a for-loop to compute the sum of the values in the array, then print to the screen “sum: ” followed by the sum.
    • Use a for-loop to determine how many elements in the array are even, then print to the screen “num even: ” followed by the number that are even.
    • Use a for-loop to compute the smallest value in the array, then print to the screen “smallest: ” followed by the smallest value.
    • Use a for-loop to compute the largest value in the array, then print to the screen “largest: ” followed by the largest value.

Test and when satisfied that your solution is correct, give the person next to you a hug, then push to GitHub.

© 2024, Eric. All rights reserved.