Answer:
Explanation:
public class Lab8
{
public static double median(double list[])
{
int size=list.length;
if(size%2!=0)
return list[size/2];
else
return(list[size/2-1]+list[size/2])/2.0;
}
public static boolean issorted(double list[])
{
for(int i=0;i<list.length-1;i++)
{
if(list[i]>list[i+1])
return false;
}
return true;
}
public static void findcommon(int a1[],int a2[],int common[])
{
int c=0;
for(int i=0;i<a1.length;i++)
{
boolean isFilled=false;
for(int j=0;j<c;j++)
{
if(a1[i]==common[j])
{
isFilled=true;
break;
}
}
if(isFilled)
{
for(int k=0;k<a2.length;k++)
{
if(a1[i]==a2[i])
{
common[c]=a1[i];
c++;
break;
}
}
}
}
while(c<common.length)
{
common[c]=0;
c++;
}
}
public static void rotateRight(int list[])
{
int temp=list[list.length-1];
for(int i=list.length-1;i>0;i--)
{
list[i]=list[i-1];
}
list[0]=temp;
}
public static int count(int list[],int n)
{
int ncount=0;
for(int i=0;i<list.length;i++)
{
if(list[i]==n)
ncount++;
}
return ncount;
}
public static int[] stretch(int list[])
{
int stretchedList[]=new int[list.length*2];
int k=0;
for(int i=0;i<list.length;i++)
{
int n=list[i];
if(n%2==0)
{
stretchedList[k]=n/2+1;
stretchedList[k+1]=n/2;
}
k+=2;
}
return stretchedList;
}
public static void printDoubleArray(double arr[])
{
System.out.print("[");
for(int i=0;i<arr.length;i++)
{
if(i!=arr.length-1)
System.out.print(arr[i]+",");
else
System.out.print(arr[i]);
}
System.out.println("]");
}
public static void printIntArray(int arr[])
{
System.out.print("[");
for(int i=0;i<arr.length;i++)
{
if(i!=arr.length-1)
System.out.print(arr[i]+",");
else
System.out.print(arr[i]);
}
System.out.println("]");
}
public static void main(String[] args)
{
double list1[]={1.1,2.2,3.3,4.4,5.5,6.6};
double med=median(list1);
System.out.println("List1:");
printDoubleArray(list1);
System.out.println("Median of List1:"+ med);
double list2[]={1.1,2.2,3.3,4.4,6.6,5.5};
boolean result=issorted(list2);
System.out.println("List2:");
printDoubleArray(list2);
System.out.println("List2 is a sorted array.(T/F):"+ result);
int list3[]={3,8,5,6,5,8,9,2};
int list4[]={5,15,4,6,7,3,9,11,9,3,12,13,14,9,5,3,13};
int list5[]=null;
if(list3.length<=list4.length)
list5=new int[list3.length];
else
list5=new int[list4.length];
findcommon(list3,list4,list5);
System.out.println("\n List3:");
printIntArray(list3);
System.out.println("\n List4:");
printIntArray(list4);
System.out.println("\n Array with common value,List5:");
printIntArray(list5);
int list6[]={3,8,19,7};
System.out.println("\n List6:");
printIntArray(list6);
rotateRight(list6);
System.out.println("\n the array after rotate right:");
printIntArray(list6);
int list7[]={3,5,2,1,92,38,3,14,5,73};
int n=count(list7,3);
System.out.println("\n List7:");
printIntArray(list7);
System.out.println("\n the number of occurences of the value 3 in the list 7:"+ n);
int list8[]={18,7,4,24,11};
int list9[]=stretch(list8);
printIntArray(list8);
System.out.println("\n The array after stretching:");
printIntArray(list9);
}
}