Question 4c Implement a function, generateSequence which has an integer parameter length. The function generates and...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Question 4c Implement a function, generateSequence which has an integer parameter length. The function generates and returns a string consisting of length unique integers and dashes, separated by commas (). The string starts with an integer, not necessarily 1. If length is 0 or negative, the function returns an empty string. If length is 1, the function returns a string of one integer. An example string is '2,3,-,-,7,8,9,-,11' when length is 9. Note that the string also satisfies these conditions: Integers are ordered in increasing order and neighbouring numbers are consecutive, e.g., 2 and 3 are consecutive and 7, 8 and 9 are consecutive. Integers immediately after a dash are at least one but not two more than the sum of the last integer before the dash and the number of dashes, e.g., the number after the first 2 dashes can be 6 or 7, and 7 is generated and included, instead of 6. Also, the number after the last dash can be 11 or 12, and 11 is generated and included, instead of 12. (10 marks) Question 4d Write a function, main which repeatedly prompts for the length of sequence to generate, calls the functions generate Sequence and checkSequence, and prints the output of both function calls until the user enters when prompted for the length. If the user enters negative numbers, display an error message and re-prompt the user. Sample run: to end: 10 Enter length of sequence to generate and to end: -2 Length of sequence must be a positive number Enter length of sequence to generate and 89,90,91,92,93,-,96,97,-,-: False Enter length of sequence to generate and 83, 84, 85, -,87,-,89,90,-,- : True to end: 10 to end: 9 Enter length of sequence to generate and 5,6,7,8,9,-,11,12,- : True Enter length of sequence to generate and to end: 0 (5 marks) Question 4c Implement a function, generateSequence which has an integer parameter length. The function generates and returns a string consisting of length unique integers and dashes, separated by commas (). The string starts with an integer, not necessarily 1. If length is 0 or negative, the function returns an empty string. If length is 1, the function returns a string of one integer. An example string is '2,3,-,-,7,8,9,-,11' when length is 9. Note that the string also satisfies these conditions: Integers are ordered in increasing order and neighbouring numbers are consecutive, e.g., 2 and 3 are consecutive and 7, 8 and 9 are consecutive. Integers immediately after a dash are at least one but not two more than the sum of the last integer before the dash and the number of dashes, e.g., the number after the first 2 dashes can be 6 or 7, and 7 is generated and included, instead of 6. Also, the number after the last dash can be 11 or 12, and 11 is generated and included, instead of 12. (10 marks) Question 4d Write a function, main which repeatedly prompts for the length of sequence to generate, calls the functions generate Sequence and checkSequence, and prints the output of both function calls until the user enters when prompted for the length. If the user enters negative numbers, display an error message and re-prompt the user. Sample run: to end: 10 Enter length of sequence to generate and to end: -2 Length of sequence must be a positive number Enter length of sequence to generate and 89,90,91,92,93,-,96,97,-,-: False Enter length of sequence to generate and 83, 84, 85, -,87,-,89,90,-,- : True to end: 10 to end: 9 Enter length of sequence to generate and 5,6,7,8,9,-,11,12,- : True Enter length of sequence to generate and to end: 0 (5 marks)
Expert Answer:
Related Book For
Data Structures and Algorithms in Java
ISBN: 978-1118771334
6th edition
Authors: Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser
Posted Date:
Students also viewed these programming questions
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Implement and maintain internal control procedures This task will require you to roleplay a meeting with your supervisor, Chris Kohler. You will discuss the reporting requirements and timetables...
-
During the year, Summit produces 50,000 snow shovels and sells 45,000 snow shovels. Summit Manufacturing produces snow shovels. The selling price per snow shovel is $30. There is no beginning...
-
A boat has a crew of eight: Two of those eight can row only on the stroke side, while three can row only on the bow side. In how many ways can the two sides of the boat be manned?
-
Diet Cola and Weight Gain in Humans A study found that American senior citizens who report drinking diet soda regularly experience a greater increase in weight and waist circumference than those who...
-
The percentage of individual investors portfolios committed to stock depends on the state of the economy. The following table reports the percentage of stocks in a portfolio for nine quarters: a. Use...
-
why do you think the Virginia Company continued to send colonists to Jamestown after the rise in mortality rate, the continued conflict with local tribes, and disease?
-
The process of market segmentation involves breaking down a heterogeneous market into homogeneous and identifiable segments. If this process is carried to its extreme, then one could say that: a....
-
Simpson should estimate the initial outlay and the terminal year nonoperating cash flow, respectively, to be closest to: A. 1.50 billion and 0.70 billion. B. 1.90 billion and 0.70 billion. C. 1.90...
-
The following are selected account balances for Warren Clinic as of December 31, 2015, in alphabetical order. Create Warren Clinics balance sheet. Accounts payable Accounts receivable, net Cash $...
-
Gershwin Corporation obtained a franchise from Sonic Hedgehog Inc. for a cash payment of 120,000 on April 1, 2015. The franchise grants Gershwin the right to sell certain products and services for a...
-
Alan Naiman was a frugal social worker who used duct tape to repair his shoes, looked for deals at the supermarket, and chose fast-food restaurants when it was his turn to treat friends to lunch....
-
What is the NPV (C$ millions) of the original project for Society Services without considering the expansion option? A. 6.11. B. 5.66. C. 2.33. The capital budgeting committee for Laroche Industries...
-
(a) Explain to Sonia the proper plaintiff rule and the internal management rule. Corporations Act 2001 (Cth), including the procedures she may need to follow to obtain those remedies. (b) Sonia seeks...
-
What kind of rays are X-rays?
-
If the parameter to the makePayment method of the CreditCard class (see Code Fragment 1.5) were a negative number, that would have the effect of raising the balance on the account. Revise the...
-
Write a short Java method that takes an array of int values and determines if there is a pair of distinct elements of the array whose product is even.
-
Show that if d(n) is O( f (n)), then ad(n) is O( f (n)), for any constant a > 0.
-
The pocket of hot air appears to be a pool of water because A. Light reflects at the boundary between the hot and cool air. B. Its density is close to that of water. C. Light refracts at the boundary...
-
Which of these changes would allow you to get closer to the mirage before it vanishes? A. Making the pocket of hot air nearer in temperature to the air above it B. Looking for the mirage on a windy...
-
If you could clearly see the image of an object that was reflected by a mirage, the image would appear A. Magnified. B. With up and down reversed. C. Farther away than the object. D. With right and...
Study smarter with the SolutionInn App