0% found this document useful (0 votes)
43 views3 pages

Program To Find The NTH Largest Number in The Array Using Command Line Arguments

The document is a Java program that finds the Nth largest number in an array using command line arguments. It takes the desired Nth value as a command line argument, stores sample data in an array, iterates through the array to find the maximum values and store their indexes, and prints out the Nth largest value. It handles exceptions if the Nth value is out of bounds of the array length.

Uploaded by

GokulKannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views3 pages

Program To Find The NTH Largest Number in The Array Using Command Line Arguments

The document is a Java program that finds the Nth largest number in an array using command line arguments. It takes the desired Nth value as a command line argument, stores sample data in an array, iterates through the array to find the maximum values and store their indexes, and prints out the Nth largest value. It handles exceptions if the Nth value is out of bounds of the array length.

Uploaded by

GokulKannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*Program To Find The Nth Largest Number In The Array Using

Command Line Arguments*/


public class NthLargest
{
int array[] = {10,19,2,3,1,98,75,65,8500,850000};
public static void main(String args[]) throws MyException{
new NthLargest(Integer.parseInt(args[0]));
}
public NthLargest(int n)throws MyException{
int skip[] = new int[n];
int i,j,k,l,skipIndex=0,maxJ=0;
int nthMaxValue=0;
boolean skipLoop = false;
if(n>array.length) throw new MyException("Out of scope to grab this
number");
for(i=0; i<array.length; i++){
int val=array[i];
maxJ = i;
for(j=0; j<array.length; j++) {
for(k=0; k<skip.length; k++)
if(skip[k] == j)
skipLoop = true;
if(skipLoop){
skipLoop = false;
continue;
}
if(val < array[j]) {
val = array[j];
maxJ = j;
}
}

if(skipIndex == n){
nthMaxValue = array[skip[n-1]];
break;
}
skip[skipIndex++]=maxJ;
}
System.out.println("I/p values in a given array:");
for(int w=0;w<array.length;w++)
{
System.out.println(array[w]);
}
System.out.println("Input The Nth Largest number to find: "+n);
System.out.println("The"+n+" largest value is : "+nthMaxValue);
}
}
class MyException extends Exception{
String msg="";
MyException(String msg){
this.msg = msg;
}
public String getMessage(){
return msg;
}
}

C:\navin>edit NthLargest.java
C:\navin>javac NthLargest.java
C:\navin>java NthLargest 3
I/p values in a given array:
10
19
2
3
1
98
75
65
8500
850000
Input The Nth Largest number to find:3
The3 largest value is : 98

You might also like