CS 180 Lab 5

Loop Patterns

1

Begin by moving into the labs directory in your /Classes/CS180 directory. Next, create a new directory named Lab5. Move into the Lab4 directory and create java program named loopPatterns.java. Use VIM to edit your program following the specifications below.

NOTE: You must finish this assignment during the allotted lab time to receive credit. You may work alone, or with others to complete the assignment. The lab assignments should not be electronically submitted. Once you are finished, show your solution to the GTA.

Problem Description:

Write a program that reads integers, finds the largest and smallest of them, and counts their occurrences. Assume that the input ends with number 0. Suppose that you entered 352555 0; the program finds that the largest is 5 and the occurrence count for 5 is 4. (Hint: Maintain four variables, max, min, minC, and maxC. max stores the current max number, and maxC stores its occurrences. Min stores the current min number, and minC stores its occurrences. Initially, assign the first number to max and min, and 1 to maxC and minC. Compare each subsequent number with max. If the number is greater than max, assign it to max and reset maxC to 1. If the number is equal to max, increment count by 1. Also compare each subsequent number with min. If the number is less than min, assign it to min and reset minC to 1. If the number is equal to min, increment minC by 1.)

Note: You should have no relational, logical, or arithmetic operators in the main method.

Here are sample runs of the program:

Sample 1:

Enter numbers: 3 5 2 5 5 5 0

The largest number is 5

The smallest number is 2
The occurrence count of the largest number is 4

The occurrence count of the smallest number is 1

Sample 2:

Enter numbers: 3 6 5 4 2 4 5 25 5 0

The largest number is 6
The smallest number is 2

The occurrence count of the largest number is 1

The occurrence count of the smallest number is 2

Analysis:

(Describe the problem including input and output in your own words.)

Create a file named loopPatterns.txt to use for this section.

Design:

(Describe the major steps for solving the problem.)

Create a file named design.txt to use for this section.

Code:

The Java code solution should be placed in loopPatterns.java.

Testing:

(Describe how you test this program)

Create a file named testing.txt to use for this section.

1