compute two largest number
i created an array. i am able to compute the largest number in array but my problem now is how can i compute the second largest number? do i need an if/else statement again to compute the second largest number if i have to then i am pretty much sure i need a condition so that it will not print the 1st largest number again. or do i need another for loop to compute the second largest number ? any idea how can i do that i dont want to post the code since i want to complete the exercise by myself. after your suggestions if i cannot solve it then i will be happy to post my code. But i will solve it :).
Thanks
Message was edited by:
fastmike
[670 byte] By [
fastmikea] at [2007-11-26 14:17:13]

int maxNum = maximum number in array
int currentMax = minimum number in array
for (int i : array)
{
if (i > currentMax && i < maxNum)
currentMax = i;
}
So after the for statement, currentMax is the highest number that is less than the real max...(ie the second highest)
You could create a method using conlan's idea, which takes maxNum as an argument.
thanks conan. Cap i am almost done with my program and its like 12:15 am in the morning and had to go to work at 6:00am. i wish i have time to change my whole program with methods. so for the time being concentrating on conlan idea so i tried his code it still outputs wrong so just to make sure maxNum is the total length of an array or the highest number in an array and same goes for the currentMax also. and do i need another for loop or i can use the same for loop which i am using to calculate the highest number in an array which is :
for ( int index=0 ; index < data.length; index++)
{
if(data[index]>max ) {
max = data[index]; }
Message was edited by:
fastmike
> Cap i am almost done with my program
> and its like 12:15 am in the morning
It's 1:15 am here, I'm about to go to bed.
> i wish i have time to change my whole
> program with methods.
Aren't you teaching yourself? Get some sleep and try again in the morning. Splitting the program up into different makes it much easier.
> so just to make sure maxNumber is
> the total length of an array or the hight number in
> an array and same goes for the currentMax also.
No. MaxNumber should be the highest number in the array (If you're looking for the second highest). It only sets currentMax if the number is below maxNumber. If you're looking for the highest number in the array, set maxNumber to Integer.MAX_VALUE or something like that.
got it.
import java.io.* ;
class TwoLargest
{
public static void main ( String[] args ) throws IOException
{
int[] data = {3, 1, 5, 7, 4, 12, -3, 8, -2};
int max = data[0];
int max1 = data[0];
int min = data[0];
// compute the two largest
for ( int index=0 ; index < data.length; index++)
{
if(data[index]>max ) {
max = data[index]; }
if(data[index]<min) {
min = data[index];
}
if(data[index]>max1 && data[index]<max) {
max1 = data[index];
}
}
// write out the two largest
System.out.println( "the max is " + max + "The largest is " + max1);
}
}
Thanks a bunch guys time to go to bed now thanks conan and Cap.>
The method could look something like this:
public void findMax(int maxNum, int[] a)
{
int currentMax = Integer.MIN_VALUE;
for(int i=0; i<a.length; i++)
{
if(a[i] > currentMax && a[i] < maxNum)
{
currentMax = a[i];
}
}
return currentMax;
}
It's easy. Try it sometime.
Thanks cap. Now i am thinking of writing each and every program by the rules of encapsulation in java this will increase my skills in encapsulation but since i just started learning arrays i was just worried not to use methods unless i fully understand the funtionality. and yes i am teaching java by myself its been like 5 months solving each and every exercise problem and sometimes helping in java forums also to other newbies. hope i should be done in a month or 2 just to learn the fundamental concepts of java which includes array, queue, list, stakcs, loops, while, inheritance and polumorphism. hope to succeed.