0

I'm trying to loop through my array to find the maximum value and print the value. However, nothing is being printed to the console. Can you please take a look at my code below to see what I've done incorrectly.

for (c = 0; c < n; c++) //loops through array until each index has had a value input by the user
  array[c] = in.nextInt();

maxInt = array[0];
minInt = array[0];

for (c = 0; c < n; c++) {
    if (array[c] > maxInt) {
        maxInt = array[c];
    }
    else {
        break;
    }
}
System.out.println("Max int is: " + maxInt);
}

EDIT:

Full class:

import java.util.Scanner;

public class MaxMinOfArray {
public static void main(String[] args) {
int c, n, search, array[];
int maxInt, minInt;

Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt(); //asks user to specify array size
array = new int[n]; //creates array of specified array size

System.out.println("Enter " + n + " integers");

for (c = 0; c < n; c++) //loops through array until each index has had a value input by the user
array[c] = in.nextInt();

maxInt = array[0];
minInt = array[0];

for (c = 1; c < n; c++) {
if (array[c] > maxInt) {
    maxInt = array[c];
}
}
System.out.println("Max int is: " + maxInt);
}
}
1
  • It's working for me. How are you running it--through an IDE, Linux shell, Windows Command Prompt, ? Commented Jul 8, 2017 at 22:16

4 Answers 4

3

Remove:

else {
        break;
    }

And start from c=1

Sign up to request clarification or add additional context in comments.

4 Comments

Is your scanner defined properly?
@Spartan123 That is strange, because it works perfectly Check this: ideone.com/PqF8vW...
@Spartan123 Are you able to give inputs? Can you show an image of runtime?
Nevermind, my mistake, I specifed 300 elements but I was copying the values off somewhere and didn't highlight all 300 values. My bad lol
1

Remove your this part of code.

else {
    break;
}

Because when c==0 in that time array[c] == maxInt. So it goes to else part and break your for loop.

1 Comment

Hi, it's still not printing to the class, I've edited the OP to add the full class code. Please take a look.
1

As others indicated, you don't want to do

else {
    break;
}

That means that it'll stop looping as soon as it finds a number that isn't larger than the current max. Since you're starting with the first item in the list, which trivially isn't larger than itself, you break immediately.

Even if you changed it to start at c = 1, the only case where this code could possibly work as written is if the user entered numbers in ascending order. (In that case, doing a linear search like this would be pointless anyway since you could literally just find the last item in the array and know that it'll be the largest item).

Also, you should check to see if array[c] is smaller than the current minimum value in your for loop; there's no reason at all to do this in a separate loop.

Remember, if you're doing a linear search for the max value of an unsorted array, you always must go through the entire array to make sure you didn't miss a greater value. For example, if you only search half of the array, how do you know that the half that you didn't search doesn't contain the max value?

1 Comment

Hi, it's still not printing to the class, I've edited the OP to add the full class code. Please take a look.
0

Your second loop compares for each element in array if it is greater than maxInt, but maxInt has just been set to the first element of array. This fails the condition on the first iteration of the loop, executing the break in the else block, which ends the loop.

Taking out the else block fixes this:

for (c = 0; c < n; ++c)
{
    if (array[c] > maxInt)
        maxInt = array[c];
}

Or alternatively:

for (c = 0; c < n; ++c)
    maxInt = Math.max(maxInt, array[c]);

As for the console message not appearing, make sure the code is properly executed by setting a breakpoint and stepping through the code (depends on the IDE you're using).

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.