Have an array of integers with user input instead of given

Assignment Help JAVA Programming
Reference no: EM13162556

 change the current code to have an array of integers with user input intead of given input from the main where it says int[] a=....; And also from a text file but the same numbers as what is given in main.

public final class MaxSumTest

{

static private int seqStart = 0;

static private int seqEnd = -1;

 

/**

* Cubic maximum contiguous subsequence sum algorithm.

* seqStart and seqEnd represent the actual best sequence.

*/

public static int maxSubSum1( int [ ] a )

{

int maxSum = 0;

 

for( int i = 0; i < a.length; i++ )

for( int j = i; j < a.length; j++ )

{

int thisSum = 0;

 

for( int k = i; k <= j; k++ )

thisSum += a[ k ];

 

if( thisSum > maxSum )

{

maxSum = thisSum;

seqStart = i;

seqEnd = j;

}

}

 

return maxSum;

}

 

/**

* Quadratic maximum contiguous subsequence sum algorithm.

* seqStart and seqEnd represent the actual best sequence.

*/

public static int maxSubSum2( int [ ] a )

{

int maxSum = 0;

 

for( int i = 0; i < a.length; i++ )

{

int thisSum = 0;

for( int j = i; j < a.length; j++ )

{

thisSum += a[ j ];

 

if( thisSum > maxSum )

{

maxSum = thisSum;

seqStart = i;

seqEnd = j;

}

}

}

 

return maxSum;

}

 

/**

* Linear-time maximum contiguous subsequence sum algorithm.

* seqStart and seqEnd represent the actual best sequence.

*/

public static int maxSubSum3( int [ ] a )

{

int maxSum = 0;

int thisSum = 0;

 

for( int i = 0, j = 0; j < a.length; j++ )

{

thisSum += a[ j ];

 

if( thisSum > maxSum )

{

maxSum = thisSum;

seqStart = i;

seqEnd = j;

}

else if( thisSum < 0 )

{

i = j + 1;

thisSum = 0;

}

}

return maxSum; }

/**

* Recursive maximum contiguous subsequence sum algorithm.

* Finds maximum sum in subarray spanning a[left..right].

* Does not attempt to maintain actual best sequence.

*/

private static int maxSumRec( int [ ] a, int left, int right )

{

int maxLeftBorderSum = 0, maxRightBorderSum = 0;

int leftBorderSum = 0, rightBorderSum = 0;

int center = ( left + right ) / 2;

 

if( left == right ) // Base case

return a[ left ] > 0 ? a[ left ] : 0;

 

int maxLeftSum = maxSumRec( a, left, center );

int maxRightSum = maxSumRec( a, center + 1, right );

 

for( int i = center; i >= left; i-- )

{

leftBorderSum += a[ i ];

if( leftBorderSum > maxLeftBorderSum )

maxLeftBorderSum = leftBorderSum;

}

 

for( int i = center + 1; i <= right; i++ )

{

rightBorderSum += a[ i ];

if( rightBorderSum > maxRightBorderSum )

maxRightBorderSum = rightBorderSum;

}

 

return max3( maxLeftSum, maxRightSum,

maxLeftBorderSum + maxRightBorderSum );

}

 

/**

* Return maximum of three integers.

*/

private static int max3( int a, int b, int c )

{

return a > b ? a > c ? a : c : b > c ? b : c;

}

 

/**

* Driver for divide-and-conquer maximum contiguous

* subsequence sum algorithm.

*/

public static int maxSubSum4( int [ ] a )

{

return a.length > 0 ? maxSumRec( a, 0, a.length - 1 ) : 0;

}

 

/**

* Simple test program.

*/

public static void main( String [ ] args )

{

int a[ ] = { 4, -3, 5, -2, -1, 2, 6, -2 };

int maxSum;

 

maxSum = maxSubSum1( a );

System.out.println( "Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd );

maxSum = maxSubSum2( a );

System.out.println( "Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd );

maxSum = maxSubSum3( a );

System.out.println( "Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd );

maxSum = maxSubSum4( a );

System.out.println( "Max sum is " + maxSum );

}

}

Reference no: EM13162556

Questions Cloud

How many moles of oxygen were in the sample : If 5.50 of the unknown compound contained 0.183 of and 0.367 of , how many moles of oxygen, , were in the sample.
Derive an expression for the subsequent concentration : Derive an expression for the subsequent concentration of sodium chloride c in terms of c0, t, Q, and V. Make a sketch of c versus t and label the main features.
Which of your current costs are implicit : Which of your current costs are implicit, and which are explicit and suppose The Breakfast Club, Inc. offers to pay $800/month to use the building
State what is the initial ph of buffer : What is the initial pH of this buffer? 2. What is the pH of the buffer in question
Have an array of integers with user input instead of given : change the current code to have an array of integers with user input intead of given input from the main where it says int[] a=....; And also from a text file but the same numbers as what is given in main.
State sodium thiosulfate solution : A chemist must dilute 80.5mL of 126mM aqueous sodium thiosulfate solution until the concentration falls to 110.mM . He'll do this by adding distilled water to the solution until it reaches a certain final volume.
What are the percentages of co2 and ne in the sample by mole : If the sample contains a combined total of 1.90 mol and has a total mass of 60.3g, what are the percentages of CO2 and Ne in the sample by mole?
What was the concentration of the solution : A 515- sample of unknown solution reacts completely with to form 20.1 . What was the concentration of the solution?
What was the initial concentration : A zero-order reaction has a constant rate of 1.90×10-4 . If after 40.0 seconds the concentration has dropped to 6.50×10-2 , what was the initial concentration?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Develop a reliable transfer protocol over udp

Develop a reliable transfer protocol over UDP. Focus on a Stop- and-Wait protocol.

  Represent one book in java

Represent one book in java

  Given an array with 100,000 entries to sort

Sorting  given an array with 100,000 entries to sort. Under what circumstances would you use each of the following: insertion sort,mergesort'quicksort

  Develop class which implements interface

Let the ADT called SquareMatrix. (The matrix can be represented by 2-D array of ints w/ n rows and n columns.) Write specification for ADT as Java interface. Develop the class which implements interface.

  Design and implement a small and simple email server

Design and implement a small and simple email server using the concept of web based information system (WBIS).

  Method called printpowersof2 that accepts a maximum number

Write a method called printPowersOf2 that accepts a maximum number as an argument and prints each power of 2 from 20 (1) up to that maximum power, inclusive. For example, consider the following calls: printPowersOf2(3); printPowersOf2(10)

  Minimal spanning tree decreasing edge dismissal

Minimal Spanning Tree Decreasing Edge Dismissal, Reverse-delete algorithm. Develop an implementation that computes the MST

  Java program to store temperatures in two-dimensional array

Write the Java program which uses two-dimensional array to store highest and lowest temperatures for each month of the year. Program must output average high,average low,

  Develop a graphical user interface based java program

Develop a Java program that can communicate with a real SMTP email server for sending emails. TNE 60003 - introduction to network programming, You program should provide a GUI and can successfully send the SMTP commands to the mail server

  Write java program which simulates flipping of coin

Write a Java program which simulates flipping of coin 1000 times and prints total number of heads and tails. You should create a class.

  Alter the prototype form page by javascript function

Alter the prototype form page so that when JavaScript function has verified that all the required fields have been filled, cookie is added to user's computer.

  Eclipse or netbeans environments

As recommendation, you can try to use Eclipse or NetBeans environments that are used also for other courses as: Introduction in Java programming and Intermediate programming.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd