Reputation: 23
I am trying to do my homework but something is wrong in my code. I have 3 arrays to get the max value of each one. I am using Scanner to get the Int numbers of each array. I divide de code in 3 Sequences, where I will find the max number of my arrays.
I input this numbers: 30 40 50 60 30 40 15 10 20 -54 -134 1 1 1 1
Problem appears in Sequence 2, where I get 0 as max number.
Scanner in = new Scanner(System.in);
int[] seq1 = new int [10];
int[] seq2 = new int [2];
int[] seq3 = new int [4];
int i, max1, max2, max3;
max2=Integer.MIN_VALUE;
//SEQ1
max1=seq1[0];
for(i = 0; i < seq1.length; i++ ) {
seq1[i] = in.nextInt();
if (seq1[i] > max1) max1 = seq1[i];
}
//SEQ2
max2=seq2[0];
for(i = 1; i < seq2.length; i++) {
seq2[i] = in.nextInt();
if (seq2[i] > max2) max2 = seq2[i];
}
//SEQ3
max3=seq3[0];
for(i = 0; i < seq3.length; i++ ) {
seq3[i] = in.nextInt();
if (seq3[i] > max3) max3 = seq3[i];
}
System.out.println(max1);
System.out.println(-(max2));
System.out.println(max3);
Upvotes: 0
Views: 854
Reputation: 1234
You should initialize all of your max values to Integer.MIN_VALUE and then remove the second initialization (max2=seq2[0]). seq2[0] is initialized to zero by default.
Also, your second loop starts with 1 when it should start with 0.
Here is what that section of code should look like:
int max1 = Integer.MIN_VALUE;
int max2 = Integer.MIN_VALUE;
int max3 = Integer.MIN_VALUE;
//SEQ1
for(i = 0; i < seq1.length; i++ ) {
seq1[i] = in.nextInt();
if (seq1[i] > max1) max1 = seq1[i];
}
//SEQ2
for(i = 0; i < seq2.length; i++) {
seq2[i] = in.nextInt();
if (seq2[i] > max2) max2 = seq2[i];
}
//SEQ3
for(i = 0; i < seq3.length; i++ ) {
seq3[i] = in.nextInt();
if (seq3[i] > max3) max3 = seq3[i];
}
What IDE are you using? You really need to learn to debug with your IDE because the error would jump out at you immediately if you stepped through this code. If you aren't using an IDE, I highly suggest you learn one. Eclipse is free, if somewhat bloated for simple java projects like this. Otherwise, you could simply place a System.out.println after every occurence of max3.
Upvotes: 0
Reputation: 195
The thing is that your second input is showing 0 because you initializate max2 at 0, and if you compare to a minus number like -54 is going to show 0 because is a bigger value. You can try to do this way, using Interger.MIN.VALUE.
int max1=Integer.MIN.VALUE;
int max2=Integer.MIN.VALUE;
int max3=Integer.MIN.VALUE;
// Just for having values
int[] seq1 = {10,20,30,101,40,55,66,77,88,99};
int[] seq2 = {-56,-232};
int[] seq3 = {1,1,1,1};
for (int counter = 1; counter < seq1.length; counter++)
{
if (seq1[counter] > max1)
{
max1 = seq1[counter];
}
}
for (int counter = 1; counter < seq1.length; counter++)
{
if (seq2[counter] > max2)
{
max1 = seq2[counter];
}
}
for (int counter = 1; counter < seq1.length; counter++)
{
if (seq3[counter] > max2)
{
max3 = seq3[counter];
}
}
System.out.println(max1);
System.out.println(max2);
System.out.println(max3);
I hope it helped.
Upvotes: 2
Reputation: 46
The problem of zero is that you are initiating your max variables with the first arrays elements, which are primitives. A primitive in java does not accept the value of NULL, so you are putting zero in max2 before the for loop, and since the array contains negative values, the value 0 is always greater than negative values so max2 will always contain 0.
Here is an example of what you should be using:
Scanner in = new Scanner(System.in);
int[] seq1 = new int [10];
int[] seq2 = new int [2];
int[] seq3 = new int [4];
int i;
Integer max1, max2, max3;
//SEQ1
max1=null;
for(i = 0; i < seq1.length; i++ ) {
seq1[i] = in.nextInt();
if (max1 == null || seq1[i] > max1) max1 = seq1[i];
}
//SEQ2
max2=null;
for(i = 1; i < seq2.length; i++) {
seq2[i] = in.nextInt();
if (max2 == null || seq2[i] > max2) max2 = seq2[i];
}
//SEQ3
max3=null;
for(i = 0; i < seq3.length; i++ ) {
seq3[i] = in.nextInt();
if (max3 == null || seq3[i] > max3) max3 = seq3[i];
}
System.out.println(max1);
System.out.println(-(max2));
System.out.println(max3);
Upvotes: 0
Reputation: 616
You initialise the max variables to the first element of each array before you add anything into the arrays. Since the default value of an element in an int array is 0, max2 gets initialised as 0, which is by definition greater than any negative number.
Also, i starts at 1 for the second loop but at 0 for the other loops.
Upvotes: 0