0

I have the following code from a previous past paper:

int n = arr.length; 
double min = 0; 
int minLocation=0; 

for(int i = 1; i <= n; i++) { 
    if( arr[i] < min ) {
        min = arr[i]; 
    }
    minLocation = i; 
} 

System.out.print("The minimal value is arr["); 
System.out.println(minLocation + "] = " + min);

I have to answer the following questions from the code:

(i) The line on which the error appears 

(ii) What effect the error would have 

(iii) How the error should be corrected. 


You may assume arr[] is a non-empty array of double values that has been 
properly declared and initialized. 

I know there will be a runtime error at the line with the if statement which i'm still not sure how. As I am unable to correct it I can't see how I get the undesired results. I can see it's such a straight forward question and I am missing out something extremely obvious but I have been looking at it for a while and i'm completely missing it.

3
  • 1
    Have you tried to compile and run the code? Commented Apr 9, 2014 at 11:03
  • @DanielBarbaria I have put the code into an IDE and as I said I get the arrayindex error for some reason. Commented Apr 9, 2014 at 11:04
  • I see your post about a runtime error, nothing about any arrayindex error. Commented Apr 9, 2014 at 11:06

6 Answers 6

1

There are several mistakes in that code. Here a list of them including explanation of impact and how to fix them:

  • You are initialising min with 0
    Effect: If your array e.g. only consists of {1.1, 2.2} your code would claim that 0 is the minimum, what obviously is wrong because there doesn't exist an index i that arr[i] == 0
    Fix: double min = Double.MAX_VALUE;

  • Your for-loop starts at index 1 for (int i = 1; ...)
    Effect: You are skipping arr[0]
    Fix: for (int i = 0 ...)

  • Your loop iterates once to often because of the condition or ( ... ; <= n; ...)
    Effect: You will encounter an AIOOBE (ArrayIndexOutOfBoundsException)
    Fix: for ( ... ; i < n; ...)

  • Your are overwriting minLocation in every iteration of the loop
    Effect: After the last iteration of the loop, minLocation will always be = n
    Fix: Place minLocation = i; inside the if-statement.

Corrected version

int      n           = arr.length;
double   min         = Double.MAX_VALUE;
int      minLocation = 0;

for(int i = 0; i < n; i++) {
    if(arr[i] < min) {
        min         = arr[i];
        minLocation = i;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I know there will be a runtime error at the line with the if statement which i'm still not sure how

This runtime error will be IndexOutOfBoundsException. This error occur because array index is one less than array size. Your loop should be like.

  for(int i = 0; i < n; i++)

Comments

1

Did you intend your for() loop to start at index 1? By convention it starts at 0;

It should really be: for(int i=0;i

Comments

1

Your for loop needs to be:

for(int i = 0; i < n; i++)

Otherwise you will get an IndexOutOfBoundsException since you access the array outside of its range.

Also you should change your min value initialiaztion to:

double min = Double.MAX_VALUE;

Otherwise you will only find the minimum if your array only contains negative values.

2 Comments

I see now. Such a silly mistake the length is 5 and because it is <= it will go to arr[5] and thats why the array is out of bounds. Thank you
@GregorKoukkoullis There are several more bugs (see my post)
1

There are logical errors in your program, if it is intended to find the minimal value and position of given array.

  • Initialize

    double min = arr[0];

  • End for loop at

    i < n

  • Include

    minLocation = i;

as

if( arr[i] < min ) 
{
    min = arr[i]; 
    minLocation = i;
}

Comments

0

Java array indices start at 0. So the for loop should be for( int i = 0; i < n; i++ ). The minimum is only computed if the the elements of the array are non-negative.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.