Forum : i m not gettin the output can you please help
Brief description  about Online courses   join in Online courses
View vindya Ballenahally Nagaraju 's Profile

i m not gettin the output can you please help

class Compare {
public static void main ( String args[] ) {
int i;
int[] array = new int[10];
int[] largest;
array[0] = 10;
array[1] = 60;
array[2] = 20;
array[3] = 50;
array[4] = 80;
array[5] = 40;
array[6] = 90;
array[7] = 70;
array[8] = 30;
array[9] = 100;

for ( i = 0; i < array.length; i ) {
if ( array[0] > array[1] ) largest[i] = array[0];
else largest[i] = array[1];

if ( array[1] > array[2] )
largest[i] = array[1];
else largest[i] = array[2];

if ( array[2] > array[3] ) largest[i] = array[2];
else largest[i] = array[3];

if ( array[3] > array[4] ) largest[i] = array[3];
else largest[i] = array[4];

if ( array[4] > array[5] ) largest[i] = array[4];
else largest[i] = array[5];

if ( array[5] > array[6] ) largest[i] = array[5];
else largest[i] = array[6];

if ( array[6] > array[7] ) largest[i] = array[6];
else largest[i] = array[7];

if ( array[7] > array[8] ) largest[i] = array[7];
else largest[i] = array[8];

if ( array[8] > array[9] ) largest[i] = array[8];
else largest[i] = array[9];

}
System.out.println(largest[i]);
}
}
Asked by vindya Ballenahally Nagaraju | Dec 20, 2010 |  Reply now
Replies (5)
View vindya ballenahally nagaraju 's Profile
thank u :)
Dec 21, 2010
View akhmad durdibayevich qutlimuratov 's Profile
The previous code compares only 2 elements of array in each time, so it is printing last element, because last element is bigger than previous. I done it mate, here u go, it finds largest value and prints it:

class Compare {
public static void main ( String args[] ) {
int i;
int[] array = new int[10];
int largest = 0;
array[0] = 10;
array[1] = 60;
array[2] = 20;
array[3] = 100;
array[4] = 80;
array[5] = 40;
array[6] = 90;
array[7] = 70;
array[8] = 30;
array[9] = 50;

for ( i = 0; i < array.length; i++ ) {
if (array[i] > largest) {
largest = array[i];
}

}
System.out.println(largest);
}
}
Dec 21, 2010
View vindya ballenahally nagaraju 's Profile
hi actly ur code is not workn, its printin only the last element
Dec 21, 2010
View akhmad durdibayevich qutlimuratov 's Profile
Actually this code is not 100% correct, please try to find mistakes
Dec 20, 2010
View akhmad durdibayevich qutlimuratov 's Profile
Hi vindya Ballenahally Nagaraju
u got some errors out there in your code.
you forget to put '++' after last i inside your loop,
for ( i = 0; i < array.length; i++ )
also if you use loop, you dont need to compare all array elements one by one, if you do that u dont need any loop.
The real code have to be like that in below:

class Compare {
public static void main ( String args[] ) {
int i;
int[] array = new int[10];
// we have to inicialize variable largest
int largest = 0;
array[0] = 10;
array[1] = 60;
array[2] = 20;
array[3] = 50;
array[4] = 80;
array[5] = 40;
array[6] = 90;
array[7] = 70;
array[8] = 30;
array[9] = 100;

for ( i = 0; i < array.length; i++ ) {
// here both conditions have to be true,
if (array[i] < array.length && array[i+1] > array[i]) {
largest = array[i];
}
else {
largest = array[i];
}

}
System.out.println(largest);
}
}
Dec 20, 2010