0

Hello ,

I have a 2D array. It has two columns. I have to find the top 3 elements of the second column and return the corresponding elements of the first column.
for example say the 2 D array is as follows:


1 0.33
2 0.4
3 0.6
4 0.2
5 0.32
6 0.54
7 0.34
8 0.8
9 0.52
10 0.1

in this the top 3 elements in column 2 are 0.8 , 0.6 and 0.54.
The output should return 8, 3 and 6 in sequence or store it in another array in sequence.

Thanks in advance

Rajhans

3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by JamesCherrill
0
public class LargestElementinArray {

	public static void main(String[] args) {
	// TODO Auto-generated method stub
//public int  LargestIndex(double[] data, int length)	{
//System.out.println("rajhans");
	double data[]=new double[5];
	 data[0]=0.33;
	 data[1]=0.80;
	 data[2]=0.34;
	 data[3]=0.33;
	 data[4]=0.50;
	// int index = length;// number of categories. 
	 int index = 5;
	 double largest1;
	 double largest2;
	 double largest3;
	 largest1 = data[0];
	 largest2 = data[0];
	 largest3 = data[0];
	 int largestIndex1=0;
	 int largestIndex2=0; //  This corresponds to the index of the largest element of the array.
	 int largestIndex3=0;
	 for(int i =0; i < index; i++)
	 if(largest1 < data[i])
	 {
	 largest1 = data[i];
	 largestIndex1=i;
	 
	// return weight[i];
	 //System.out.println(largest);
	 
     }
	// System.out.println(largestIndex);
	 for(int i =0; i < index  && (i!= largestIndex1); i++ ) 
		 if(largest2 < data[i])
		 {
		 largest2 = data[i];
		 largestIndex2=i;
		 
		// return weight[i];
		 //System.out.println(largest);
		 
	     } 
	 for(int i =0; i < index  && (i!= largestIndex1)&&(i!= largestIndex2); i++ ) 
		 if(largest3 < data[i])
		 {
		 largest3 = data[i];
		 largestIndex3=i;
		 
		// return weight[i];
		 //System.out.println(largest);
		 
	     } 
	 
//return largestIndex1;
	 System.out.println("first highest index = " +largestIndex1);
	 System.out.println("second highest index = " +largestIndex2);
	 System.out.println("third highest index = " +largestIndex3);
	 
	 
}
}

my output is
first highest index = 1
second highest index = 0
third highest index = 0

Expected output

first highest index = 1
second highest index = 4
third highest index = 2

0

Problem solved no need to reply. It was a silly mistake

0

`

public class LargeNumberArray {
 public static void main(String args[]){
   int a[]={7,2,8,1,3,5,10,0,12,11};
   int large=a[0],second=a[0],third=a[0];
   int index=0,index1=0,index2=0;
   for (int i=0;i
   {
     if(a[i]>=large)
     {
        large=a[i];
        index=i;  
     }
   }
   for (int i=0;i
   {
      if(a[i]second)
      {
         second=a[i];
         index1=i;
      }
    }
   for (int i=0;i
   {
      if(a[i]third)
      {
         third=a[i];
         index2=i; 
      }
   }
   System.out.println("Large Number: "+large+ " Index: "+index);
   System.out.println("Large Number: "+second+ " Index: "+index1);
   System.out.println("Large Number: "+third+ " Index: "+index2);
  }
}

Edited by JamesCherrill: Do not mention, plug or refer to any product, service, or website you are affiliated with anywhere outside of Business Exchange forums specifically designated for this purpose

0

If you post code please check that
1. it compiles
2. it works
This code 1. doesn't compile and 2 wouldn't work even if yhou fix the syntax errors.
Unless, of course, you are asking for help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.