Optimal Power Line Location Power distribution company is pl

Optimal Power Line Location .Power distribution company is planning a main power line from east to west (x-axis) across its distribution area The area has n houses The company wants to connect each house directly to the main power line with smaller power lines in north-south direction (y-axis) Your task is to estimate the optimal position (on the y-axis) of the main power line, so that the total length of smaller power lines is . the shortest. 9/27/16

Solution

Code:

package cmsc401;

import java.util.Scanner;

public class Cmsc401 {
   // function to call quick sort
public static void sorting(int[] a)
{
quick_sort_array(a, 0, a.length - 1);
}
   // recursive funnction quick sort
public static void quick_sort_array(int a[], int l, int h)
{
int i = l, j = h;
int tmp;
       // find pivot element
int pivot = a[(l + h) / 2];
       //partition the array
while (i <= j)
{
while (a[i] < pivot)
i++;
while (a[j] > pivot)
j--;
if (i <= j)
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;

i++;
j--;
}
}
if (l < j)
quick_sort_array(a, l, j);
if (i < h)
quick_sort_array(a, i, h);
}

public static void main(String[] args)
{
Scanner s = new Scanner( System.in );
int n=s.nextInt();
int[] a=new int[n];
for(int i=0;i<n ;i++)
{
a[i]=s.nextInt();
}
       // call sorting function
sorting(a);
int num;
       // if total number of elements are even
if (n%2==0)
{
           // if sum of two middle elements is even take half of it
if((a[n/2]+a[n/2-1])%2==0)
{
num=(a[n/2]+a[n/2-1])/2;
}
           // if not even add 1 to it to get upper bound element
else
num=(a[n/2]+a[n/2-1])/2+1;
}
       // otherwise take the middle element
else
{
num=a[n/2+1];
}
System.out.println(num);
}
}

Output:

6
1
8
3
6
2
7
5

 Optimal Power Line Location .Power distribution company is planning a main power line from east to west (x-axis) across its distribution area The area has n ho
 Optimal Power Line Location .Power distribution company is planning a main power line from east to west (x-axis) across its distribution area The area has n ho

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site