enter image description hereThe following is code for the merge sort. I am getting an error on the last line. I have also added a comment. I am unable to retrieve the array returned by the method merge_function(int[] a, int[] b). The error says " non-static method merge_sort(int[],int,int) cannot be referenced from a static context at demo1.Merge.main". Please help..!
public class Merge {
int[] merge_sort(int[] arr, int s, int e){
if(arr.length==1)
return arr;
int m=(s+e)/2;
int []a= merge_sort(arr,s,m);
int []b= merge_sort(arr,m+1,e);
int []c= merge(a,b);
print(arr);
return c;
}
public void print(int[] arr)
{
System.out.println("Elements after sorting:");
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
int[] merge(int[] a, int[] b)
{
int i=0,j=0,k=0;
int []r=new int[a.length+b.length];
while(i!=a.length && j!=b.length)
{
if(a[i]<b[j])
{
r[k]=a[i];
i++; j++; k++;
}
else if(a[i]==b[j])
{
r[k]=a[i];
i++; j++; k++;
}
else if(a[i]>b[j])
{
r[k]=b[j];
j++; k++;
}
}
return r;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the length of an array.");
int n=in.nextInt();
System.out.println("Enter the numbers.");
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=in.nextInt();
}
//error is here
int []r = merge_sort(arr,0,arr.length-1);
}
}
you cannot call non-static methods from a static context without using an instance of the class containing those methods.
Related
So this is my assignemt, I have to write a function that accepts a number less than 100 and returns the fibonacci element at that position. I don't understand what is wrong with my code. please note that I wrote only getFibonacciElementAt function code
import java.util.Scanner;
public class FibonacciNumber {
public long getFibonacciElementAt(int index) {
Scanner sc= new Scanner(System.in);
System.out.println("Please enter the number");
int n= sc.nextInt();
if(n<0)
return -1;
int a=0;
int b=1;
int i;
for(i=2; i<=n; i++)
{
int temp=a;
a=b;
b=temp;
}
return a;
}
public void printFibonacciElementAt(int index) {
System.out.println(getFibonacciElementAt(index));
}
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Exactly 1 inputs required.");
return;
}
try {
int num = Integer.parseInt(args[0]);
FibonacciNumber obj = new FibonacciNumber();
obj.printFibonacciElementAt(num);
} catch (NumberFormatException e) {
System.out.println("Only integers allowed.");
}
}
}
Okay, so re-posting a better recursive version using memoization so runtime of n<=100 is less than 1 second:
public static long getFibonacciElementAt(int n, long[] d) {
if (n == 0 || n == 1)
return n;
if (d[n] == 0)
d[n] = getFibonacciElementAt(n - 1, d) + getFibonacciElementAt(n - 2, d);
return d[n];
}
But once you call the method just pass a new array of the size n+1 as the following:
System.out.println(getFibonacciElementAt(n, new long[n+1]));
You are just exchanging, not adding the previous two numbers. So try following:
int a=0;
int b=1;
int temp;
for(int i=2; i<=n; i++)
{
temp=a+b;
a=b;
b=temp;
}
return b;
I am getting these errors it's a runtime error; I have no idea why it is happening I tried to find the solution but couldn't find it.
Since I am new in java please help me to find
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 34
at UnionAndIntersection.BinarySearch(UnionAndIntersection.java:90)
at UnionAndIntersection.BinarySearch(UnionAndIntersection.java:96)
at UnionAndIntersection.BinarySearch(UnionAndIntersection.java:100)
at UnionAndIntersection.BinarySearch(UnionAndIntersection.java:100)
at UnionAndIntersection.BinarySearch(UnionAndIntersection.java:100)
at UnionAndIntersection.BinarySearch(UnionAndIntersection.java:100)
at UnionAndIntersection.Union(UnionAndIntersection.java:53)
at UnionAndIntersection.main(UnionAndIntersection.java:26)
please help me to fix this
import java.util.Scanner;
import java.util.Arrays;
class UnionAndIntersection {
public static void main(String args[]) throws Exception {
int arr1[]=new int[20];
int arr2[]=new int[20];
int m,n,i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter no of elements in first array");
m=sc.nextInt();
System.out.println("Enter no of element of second Array");
n=sc.nextInt();
System.out.println("Enter Elements in 1st Array");
for(i=0;i<m;i++) {
arr1[i]=sc.nextInt();
}
System.out.println("Enter Elements in 2nd Array");
for(j=0;j<n;j++) {
arr2[j]=sc.nextInt();
}
UnionAndIntersection ui=new UnionAndIntersection();
ui.Union(arr1,arr2,m,n);
ui.Intersection(arr1,arr2,m,n);
}
void Union(int arr1[],int arr2[],int m,int n) {
if(m>n) {
int tempa[]=arr1;
arr1=arr2;
arr2=tempa;
int temp=m;
m=n;
n=temp;
}
Arrays.sort(arr1);
for(int i=0;i<m;i++)
{
System.out.println(arr1[i]+"");
}
for(int i=0;i<2;i++)
{
if(BinarySearch(arr1,0,m-1,arr2[i])== -1)
System.out.println(arr2[i]+"");
}
}
void Intersection(int arr1[],int arr2[],int m,int n)
{
if(m>n)
{
int tempa[]=arr1;
arr1=arr2;
arr2=tempa;
int temp=m;
m=n;
n=temp;
}
Arrays.sort(arr1);
for(int i=0;i<m;i++)
{
System.out.println(arr1[i]+"");
}
for(int i=0;i<2;i++)
{
if(BinarySearch(arr1,0,m-1,arr2[i])!= -1)
System.out.println(arr2[i]+"");
}
}
int BinarySearch(int arr[],int l,int h,int x)
{
if(h>=1)
{
int mid=l+h-1/2;
if(arr[mid]==x)
{
return mid;
}
else if(arr[mid]>x)
{
return BinarySearch(arr,l,mid-1,x);
}
else{
return BinarySearch(arr,mid+1,h,x);
}
}
return -1;
}
}
You set the size of the array to 20, not to the user input. If the user puts in a number larger than 20, you get the error.
So change:
int arr1[]=new int[20];
int arr2[]=new int[20];
to
int arr1[]=new int[m];
int arr2[]=new int[n];
I have written a code in java for quicksort while considering the first element as pivot. The code is giving an ArrayOutOfBoundsException if I consider the first element as the largest element of the array. While the logic is running perfectly when implemented the same logic in C++.
import java.util.Scanner;
class QSort
{
private int n;
private int a[] = new int[n];
public QSort(int[] x)
{
n = x.length;
a = x;
}
private int qs(int low,int up)
{
int pivot = a[low];
int p = low+1,q = up;
while(q>=p)
{
while(pivot>=a[p])
p++;
while(pivot<a[q])
q--;
if(q>p)
{
a[p]=a[p]+a[q]-(a[q]=a[p]); //swapping(a[p],a[q])
p++;
q--;
}
}
a[low]=a[low]+a[q]-(a[q]=a[low]); //swapping(a[low],a[q])
return q;
}
public void quicksort(int low,int up)
{
if(low<up)
{
int i = qs(low,up);
quicksort(low,i-1);
quicksort(i+1,up);
}
}
public void print()
{
System.out.println("\nThe sorted array is :");
for(int i=0; i<a.length; i++)
{
System.out.println(a[i]);
}
}
}
class Quick
{
public static void main(String args[])
{
System.out.print("\nEnter the no. of values : ");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x[] = new int[n];
System.out.println("\nEnter the elements :");
for(int i=0; i<n; i++)
x[i] = sc.nextInt();
QSort s = new QSort(x);
s.quicksort(0,n-1);
s.print();
}
}
import java.util.Scanner;
class Sorting
{
static int m,n;
static int x=0;`enter code here`
static int b[]=new int[m*n];
static int a[][]=new int[m][n];
static void print()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(+a[i][j]);
}
System.out.println();
}
}
static void convertBack()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=b[x];
x++;
}
}
}
static void sort()
{
for(int i=0;i<m*n;i++)
{
for(int j=0;j<m*n;j++)
{
if(b[i]>b[j])
{
int temp=b[i];
b[i]=b[j];
b[j]=temp;
}
else
{
continue;
}
}
}
}
static void convert()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
b[x]=a[i][j];
x++;
}
}
}
static void enterArray()
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the elements");
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=Integer.parseInt(in.nextLine());
}
}
}
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of rows");
m=Integer.parseInt(in.nextLine());
System.out.println("Enter the number of columns");
n=Integer.parseInt(in.nextLine());
enterArray();
convert();
sort();
convertBack();
print();
}
}
The above code compiles fine however on running i am getting an error as follows:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Sorting.enterArray(2dsort.java:76)
at Sorting.main(2dsort.java:87)
please help !
You are initializing your arrays before setting the values of m and n, so you get empty arrays :
static int m,n; // both are 0 by default
static int b[]=new int[m*n]; // equivalent to new int [0];
static int a[][]=new int[m][n]; // equivalent to new int [0][0];
You should create your arrays after initializing m and n :
m=Integer.parseInt(in.nextLine());
System.out.println("Enter the number of columns");
n=Integer.parseInt(in.nextLine());
b=new int[m*n];
a=new int[m][n];
...
i am trying to find lcm of n numbers and i want my recursion to run upto the last element, but i cant find an efficient method to limit it from going out of bounds at line 41.
please suggest any trick.
import java.io.*;
class lcm
{
static int x=1,k=1;
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
System.out.println("enter the limit");
int n= Integer.parseInt(br.readLine());
System.out.println("enter the numbers");
int L;
int arr[]=new int[n];
for(int i=0;i<n;i++)
arr[i]=Integer.parseInt(br.readLine());
L=lc( arr[0],arr[1],arr,n);
System.out.println(L);
}
static int lc(int min, int max, int arr[], int n)
{
int fact;
if(x<=n-1 )
{
for(int i=1;i<=min;i++)
{
fact=max*i;
if(fact%min==0)
{
k=fact;
break;
}
}
//System.out.println("values of x"+x);
//System.out.println("values of k"+k);
x++;
return lc(arr[x],k,arr,n);
}
else
{
//System.out.println("vale of x"+x);
return k;
}
}
}
i found a solution though i still want to know if the code i wrote is efficient or not?
static int lc(int min, int max, int arr[], int n)
{
int fact;
if(x<n-1 )
{
for(int i=1;i<=min;i++)
{
fact=max*i;
if(fact%min==0)
{
k=fact;
break;
}
}
System.out.println("values of x"+x);
System.out.println("values of k"+k);
x++;
return lc(arr[x],k,arr,n);
}
else
{
min=arr[x];
max=k;
for(int i=1;i<=min;i++)
{
fact=max*i;
if(fact%min==0)
{
k=fact;
break;
}
}
return k;
}
}
}