View the step-by-step solution to:

Question

# Recursion Lab: Java application, which uses recursion to find the maximal (largest) contiguous

sum in a list of integers.

Warning: I have given you an algorithm to use below. You MUST base your code on this algorithm. There are much more elegant recursive solutions to this problem which you can easily find on the Internet (but which you may or may not actually understand). Do NOT submit that work as your own—I will recognize it and you will be referred to the university disciplinary committee for sanctions.

Input

Read from a text file (List.dat) lines of data where each line represents a list in this format: list-size numbers in the list separated by blanks

4 100 -50 5 8

For example, List.dat might contain:

7 -2-430 15 -7 -5 1000

2 -50 100

6 1000 -2000 900 2800 -2900 2801 0

4 100 -10 5 8

4 100 -50 5 8

Note: the list-size must be greater than 0. Bypass any line with 0 or a negative number as the first number, i.e., do NOT make empty list. You do NOT have error checking. You may assume that your input file is structured correctly: it contains all integers, and if the first number on a line is n, then there are n integers that follow it on that line.

Output

For each line of data read, display the largest sum of consecutive integers in the list followed by the list itself. For the example lists as above, your output would be:

Largest sum of consecutive integers 1033

100

3700

103

100

The list used

-2 -4 30 15 -7 -5 1000

-50 100

1000 -2000 900 2800 -2900 2801 100 -10 5 8

100 -50 5 8

318.795.4266 (Fax) 318.795.2419 One University Place Shreveport, LA 71115-2399

Algorithm - processing

• Use a loop to read each line of data from List.dat until end-of-file is reached
• On each pass of the loop array of list-size (1st number on the line) to hold the list of
• numbers read (from the rest of the line)
• Invoke a recursive method (or a surrogate/helper method that invokes a recursive method—
• more on this below) that returns the largest contiguous sum for this list
• Display the sum and the list (as above)
• Do not use any "global" variables in your code (your methods should use only parameters or
• local variables, no static variables that recursive methods would refer to, as they would not be reinstantiated)
• Input File
• Include a file of test data in your src folder. The contents of your file will be replaced with my test data. Recall you can access this file in your program with this code:
• Scanner fileScan = new Scanner (new File("src\List.dat"));
• You can recall in CSC 135 we read from a file that contained URLs.
• Using Recursion
• Your main method should call this helper method, which returns the maximum contiguous sum on the list aList:
• //This method returns the maximum contiguous sum public static int maxSum(int[] aList)
• but in order to use recursion we need more parameters, so the method above maxSum will simply serve as a surrogate which calls another method, the recursive method, which does all the work:
• //This method returns the maximum contiguous sum from a list stored in an //array which begins at cell "start" and ends at cell "end"
• public static int maxContigSum (int[] aList, int start, int end)
• Using the approach for developing a recursive solution:
• Base case: a list with 1 item. What will the maximum sum be?
• Assume we can determine the maximum sum for a list of contiguous items in a shorter list.
• (Looking ahead: the shorter list that we'll use in the next step, the general case, will be the list beginning at cell "start+1" and ending at cell "end (you could also do "start" till "end-1"). We'll remember that sum as it will be a candidate for the maximum sum that our method should return.
• General case: From our assumption we know what the maximum contiguous sum is for all cells excluding the first cell, so now we need to consider any sum, which contains the first cell. So now compute (use a loop, not recursion here) all possible sums from your list that include the first cell. As you compute these sums compare them to your maximum sum so far (which initially will be what was returned by your assumption above).

What to submit:

Your project MaxContSum_Last_First to Moodle before the deadline. Be sure you submit the entire project folder zipped and not just a project folder file from your project. Also be sure your input text file is in the src folder in your project.

### Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

• ### -

Study Documents

Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

Browse Documents