Question
Needing help with my Java code! This is what it's supposed to be printing: 1. Implement the pseudo algorithm of the Find Maximum Subarray problem
Needing help with my Java code!
This is what it's supposed to be printing:
1. Implement the pseudo algorithm of the Find Maximum Subarray problem using a divide and conquer approach in finding the contiguous subarray whose values have the largest sum. Given the input and output requirements, your implement should follow the pseudocode provided in the textbook/lecture. You can simulate a period of 100 days with a randomly generated price ranging from $50 to $120 and calculate daily changes in prices from the generated prices. Use a table below as a model for days, prices, and changes in price.
Inputs: stock prices for 100 days, price changes for 99 days
Outputs: a sequence of 100 days prices and price changes for the days like the above table i and j indices for low and high index of the subarray with the largest sum a max profit calculated from the subarray
But my arrays seem to not be working and give a weird output..
Here is my Java code:
public class MaximumSubarrayAlgorithmA3 {
/** * @param args the command line arguments */ public static int[] FindMaxCrossingSubarray(int A[], int low, int mid, int high) { //All the elements on the left of the mid: int Sum = 0; int leftSum = Integer.MIN_VALUE; int maxLeft = mid; //i is the lowest # for( int i = mid; i >= low; i--) { Sum = Sum + A[i]; if (Sum > leftSum) { leftSum = Sum; maxLeft = i; } } //finding the right max subarray int rightSum = Integer.MIN_VALUE; Sum = 0; int maxRight = mid + 1; //j is the high # for (int j = mid +1; j <= high; j++) { Sum = Sum + A[j]; if(Sum > rightSum) { rightSum = Sum; maxRight = j; } } //Return statement that returns the sum of elements on left and the right of the mid int[] result = { maxLeft, maxRight, leftSum + rightSum }; return result; } public static int[] FindMaximumSubarray(int A[], int low, int high) { //FindMaximumSubarray(A, low, high); //if high == to low its an array = to 1 if (low == high) //the base case:when theres only 1 element { return new int[]{A[low]}; } //this is the recursive case: //calculating the mid index: int mid = (low + high)/2; //Finds the element in the middle when array is from low to high //left subarrays max sum int[] leftSum = FindMaximumSubarray(A, low, mid); //right subarray max sum int[] rightSum = FindMaximumSubarray(A,mid+1,high); //the middle # int[] crossSum = FindMaxCrossingSubarray(A,low,mid,high); if (leftSum[2] >= rightSum[2] && leftSum[2] >= crossSum[2]) { return leftSum; } else if (rightSum[2] >= leftSum[2] && rightSum[2] >= crossSum[2]) { return rightSum; } else { return crossSum; } } //main driver for the program!! public static void main(String[] args) { //day array of 100 day variables. int A[]=new int[100];
//price $$ array of 100 variables. int price[]=new int[100]; //this makes those 100 cariables from array above into #s btwn 50 & 120 for (int i=0; i < price.length; i++) { price[i]= 50 + (int) (Math.random() * 120); }
//price $$ array of 100 variables. int changes[]=new int[99]; //this calculates the change btwn each of those 100 variables from the price array above for (int i=1; i < changes.length ;i++) { changes[i]= price[i] - price[i-1]; }
System.out.println(); System.out.println("Day: " + A); System.out.println("Price: " + price); System.out.println("Change: " + changes); System.out.println();
a, b, c = FindMaximumSubarray(changes + 0 + len(changes)-1); System.out.println("The largest sum is from Day" + a+1 + "to " + b+1 + "of $" + c); //System.out.println("The Maximum sum of the subarray is " + FindMaximumSubarray(A, 0, A.length - 1));
} }
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