Ajay Narvekar
BAN USERHello,
This code has bug...
This code will give wrong output for input array, say (-1,2,3,4,-2)
output for this input should be -1 but your code will give 2 as output.
Try again. Have a look on the code which I've posted having 3 likes on the top of this page.
Try this one....
import java.io.*;
class Mindist
{
public static void main(String args[]) throws java.io.IOException
{
System.out.println("Enter the number of elements in the array: ");
int k=Integer.parseInt(System.console().readLine());
int a[]=new int[k];
int min,i;
int f=0;
int p=0;
System.out.println("Enter the array of numbers: ");
for(i=0;i<a.length;i++)
{
a[i]=Integer.parseInt(System.console().readLine());
}
min=a[0];
for(i=0;i<a.length;i++)
{
if(Math.abs(a[i])<Math.abs(min))
{
min=a[i];
p=i;
}
else
continue;
}
for(i=0;i<a.length;i++)
{
if(i!=p && a[i]==Math.abs(min))
{
f=1;
}
else
continue;
}
System.out.println("Minimum distance value is ");
if(f==1)
{
System.out.println(+Math.abs(min));
}
else
{
System.out.println(+min);
}
}
}
OK... Thanks DaBugger....
Working on it....
See the solution in next reply to the question...
try this one:
import java.io.*;
class Mindist
{
public static void main(String args[]) throws java.io.IOException
{
int[] a=new int[5];
int min,i;
System.out.println("Enter the array of numbers: ");
for(i=0;i<a.length;i++)
{
a[i]=Integer.parseInt(System.console().readLine());
}
min=Math.abs(a[0]);
for(i=0;i<a.length;i++)
{
if(Math.abs(a[i])<min)
{
min=Math.abs(a[i]);
}
else
continue;
}
System.out.println("Minimum distance value is "+min);
}
}
also this will give wrong output for the input like (-1, 2,3,4,5).
- Ajay Narvekar September 11, 2013here correct answer is -1 but your code will give 2 as o/p