Question
A certain engineering apparatus is controlled by the input of successive numbers. If there is a run of the same number, the apparatus can optimize
A certain engineering apparatus is controlled by the input of successive numbers. If there is a run of the same number, the apparatus can optimize its performance. Write a program that reads a sequence of numbers and prints out each run of numbers in the form m(n), where m is the number which occurred n times in succession. The numbers will be in increasing order. A number which is less than the previous number will terminate the input.
Here's a sample of how the program should run (user input is shown as bold italics):
-5
-5
-5
2
-5(3)
2
2
2
10
2(4)
10
11
10(2)
7
11(1)
In the sample run, we can see that three -5's were entered, followed by a 2. When the program detected that the run of -5's was completed, it reported -5(3), which means that there was a run of three -5's. Note: the program couldn't report the first run until a number which didn't belong to the run had been entered. This is not a design flaw -- it's the way your program should work! More 2's were entered. When the program detected that the run of 2's was completed, it reported 2(4), and so on. The program stopped when the number sequence decreased -- ie. 7 is less than 11.
Some programming notes:
- Your program will be in a main method, in a class called Numbers, which will be stored in a file called Numbers.java.
- Notice that your program should accept integers only -- assume that the user will not enter anything other than an integer.
- Your solution should use a single while loop. As explained above, the program should continue to run "while the last number entered is not less than the previous number".
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started