• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

***NEED JAVA HELP ASAP***

santana9

Banned
Hi, I have been stuck on this simple array java problem for the longest time and know I am missing a little detail. The code is to find the maximum value in the array and then delete that value. Here is the code that I wrote:

public void removeMax()
{
if (nElems > 0) { // checks if array is empty
long maxSoFar = a[0]; // remembers first value

for (int j=0; j < nElems; j++) // remembers any larger values

{
if (a[j] > maxSoFar)
maxSoFar = a[j];
for (int k = j; k<nElems; k++) // move higher ones down
a[k] = a[k+1];
nElems--; // decrement size
} // end if
} // end if

} // end removeMax
} // end class HigherArray

I think it might have to do with some of the brackets being misarranged. I have to stick with this simple array structure for it is a computer science assignment. Thanks for your input.

 
You need to find where the largest value is before you try to remove anything.

Also, you have 2 closing braces labeled 'end if', but only one opening brace for an if statement.
 
Originally posted by: Yomicron
You need to find where the largest value is before you try to remove anything.

No he doesn't. Looks like he is just going out of bounds of the array.

a[k] = a[k+1]; Will go out of bounds.

It would help if you tell us what problem you are actually having.
 
Originally posted by: Codewiz
Originally posted by: Yomicron
You need to find where the largest value is before you try to remove anything.

No he doesn't. Looks like he is just going out of bounds of the array.

a[k] = a[k+1]; Will go out of bounds.

It would help if you tell us what problem you are actually having.
his code performs a remove every time it goes through the ' j ' loop. If he is missing a brace on the second if, then given the array {1,2,3,4}, it will get to the 2 and then remove it, then it will get to the 3 and remove it, then it will get to the 4 and remove it.

But you are correct that it will go out of bounds when it tries to remove an element.
 
Originally posted by: Yomicron
Originally posted by: Codewiz
Originally posted by: Yomicron
You need to find where the largest value is before you try to remove anything.

No he doesn't. Looks like he is just going out of bounds of the array.

a[k] = a[k+1]; Will go out of bounds.

It would help if you tell us what problem you are actually having.
his code performs a remove every time it goes through the ' j ' loop. If he is missing a brace on the second if, then given the array {1,2,3,4}, it will get to the 2 and then remove it, then it will get to the 3 and remove it, then it will get to the 4 and remove it.

But you are correct that it will go out of bounds when it tries to remove an element.

I think I see what he is attempting to do, I would go about it a totally different way but he could solve the problem you are talking about with a simple return statement after he find the largest and moves everything down.

 
Edited: Showing you what your missing, and let you figure it out. Dont want to just give you the answer or else you wont learn.

Basically you need to keep track of the position of the largest value.

long maxSoFar = a[0]; // remembers first value
int maxpos = 0; // position of the max value

for (int j = 0; j < nElems; j++) { // remembers any larger values
if (a[j] > maxSoFar){
maxSoFar = a[j]; // replace the max number so far with the new one
maxpos = j; // record the position of it
} // end if
}

 
// Figure out which is the biggest value.
int biggestValue = array[0];
ing biggestPosition = 0;
for(int i = 1; i < array.length; i++){
if(array[ i ] > biggestValue){
biggestValue = array[ i ];
biggestPosition = i;
}

// you can't actually remove a value from an array, but we'll move it to the end and set it to 0.
for(int i = biggestPosition; i < (array.length-1); i++){
array[ i ] = array[i+1];
}
array[array.length] = 0;

// At this point, you'll probably want to decrement whatever counter you're using to keep track of the number of
// entries in the array that are actually useful.
 
Back
Top