Here I am Getting an ERROR
Cannot make a static reference to the non-static method leftRotatebyOne(int[], int) from the type LeftRotation
Here is my code..
public static int[] arrayLeftRotation(int[] arr, int n, int k)
{
int i;
for (i = 0; i < k; i++)
leftRotatebyOne(arr, n);
}
void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[i] = temp;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int a[] = new int[n];
for(int a_i=0; a_i < n; a_i++)
{
a[a_i] = in.nextInt();
}
int[] output = new int[n];
output = arrayLeftRotation(a, n, k);
for(int i = 0; i < n; i++)
System.out.print(output[i] + " ");
}
Can anybody tell to proceed me further.
In the main method you are calling arrayLeftRotation method (which is static) which in turn is calling leftRotatebyOne, here leftRotatebyOne is non-static method, which is being called through a static method which is not allowed in java.
Change the declaration of the method leftRotatebyOne to static as follows
static void leftRotatebyOne(int arr[], int n)
Related
The purpose of this project is to create two arrays of random numbers and run a quick sort and heap sort of them. Keep track of the number of comparison's and then compare them. Both sorts work, but my heap sort wont keep track of the comparison's. it just says 0. My quick sort works and puts the comparisons in an array. How do i fix this?
package sorting;
import java.util.Arrays;
//import java.util.Random;
import java.util.*;
public class project2
{
static int [] heap_sort_comparison = new int[21];
static int [] quick_sort_comparison = new int[21];
static int [] array1 = new int [20];
static int [] array2 = new int [20];
static int compares = 0;
static int heap_compares = 0;
private static void quickSort(int[] array1, int l, int h) {
if(l < h ) {
compares++;
int position = partition(array1, l, h);
quickSort(array1,l, position -1);
quickSort(array1, position +1, h);
}
}
private static int partition(int[] array1, int i, int j) {
int pivot = array1[j] -1;
int small = i -1;
for(int k = i; k < j; k++) {
if(array1[k] <= pivot) {
compares++;
small++;
swap(array1, k, small);
}
}
swap(array1, j, small + 1);
//System.out.println("Pivot = " + array1[small + 1]);
print_quick_sort(array1);
return small + 1;
}
public static void swap(int[] array1, int a, int b) {
int temp;
temp = array1[a];
array1[a] = array1[b];
array1[b] = temp;
}
public static void print_quick_sort(int[] array1) {
for(int i = 0; i < array1.length; i++) {
System.out.print(array1[i] + " ");
}
System.out.println();
}
//HEAP SORT
public void build(int array2[]) {
int length = array2.length;
for(int i = length/2-2; i >=0; i--) {
bubble_down(array2, i, array2.length-1);
heap_compares++;
}
for(int i = length-1; i>= 0; i--) {
swap2(array2, 0,i);
bubble_down(array2,i,0);
heap_compares++;
}
}
void bubble_down(int[] array2, int parent, int size) {
int left = parent*2+1;
int right = 2*parent+2;
int largest = 0;
if(left <= size && array2[left] > array2[largest]) {
largest = left;
heap_compares++;
}
if(right <= size && array2[right] > array2[largest]) {
largest = right;
heap_compares++;
}
if(largest != parent) {
swap2(array2,parent, largest);
bubble_down(array2,largest,size);
heap_compares++;
}
}
public static void swap2(int[] array2, int a, int b) {
int temp = array2[a];
array2[a] = array2[b];
array2[b] = temp;
}
public static void print_heap_sort(int[] array2) {
for(int i = 0; i < array2.length; i++) {
System.out.print(array2[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 20; y++) {
for(int i = 0; i < array1.length; i++) {
array1[i] = array2[i]= (int)(Math.random()*20 + 0);
}
System.out.println("Numbers Generated in Array 1: " + Arrays.toString(array1));
System.out.println("");
System.out.println("Numbers Generated in Array 2: " + Arrays.toString(array2));
System.out.println("");
//quickSort
print_quick_sort(array1);
quickSort(array1, 0, array1.length -1);
System.out.println("The number of comparisons in quick sort: "+ compares);
System.out.println("=============================");
quick_sort_comparison[x] = compares;
compares = 0;
System.out.println("Array of quick sort comparison's: ");
System.out.println(Arrays.toString(quick_sort_comparison));
System.out.println("=============================");
//Heap Sort
System.out.println("Before Heap Sort: ");
System.out.println(Arrays.toString(array2));
heap_sort_comparison[y] = heap_compares;
heap_compares = 0;
HeapSort ob = new HeapSort();
ob.sort(array2);
System.out.println("Sorted array is (heap Sort): ");
print_heap_sort(array2);
System.out.println("=============================");
System.out.println("Array of heap sort comparison's: " + heap_compares);
System.out.println(Arrays.toString(heap_sort_comparison));
}
}
}
}
You do not even call the HeapSort method that you've built.
look here...
HeapSort ob = new HeapSort();
ob.sort(array2);
I think you are trying to use a built in sorting method from HeapSort class, So how do you think the counter heap_compares will increase!
My method should return the combination of array "a" and "b" returning the combination as a ordered array "c".
For a[5] and b[4,6] c[] should return [4,5,6]
In the end for these case my code is returning [4,5],i think there is a problem in the sorting method that i can't see.
package combinaum;
import java.util.Scanner;
/**
*
* #author 201627010262
*/
public class CombinaUm {
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
Scanner teclado = new Scanner (System.in);
int n1 = teclado.nextInt();
int n2 = teclado.nextInt();
int[] a = new int [n1];
int[] b = new int [n2];
for(int i = 0; i < n1; i++)
{
a[i] = teclado.nextInt();
}
for(int i = 0; i < n2; i++)
{
b[i] = teclado.nextInt();
}
System.out.println(Arrays.toString(Combine(a, b, n1, n2)));
}
public static int Combine(int a[], int b[], int n1, int n2)
{
int e = 0;
int d = 0;
int i = 0;
int n3 = n1+n2;
int [] c = new int [n3];
while(e < a.length && d <b.length )
{
if(a[e] < b[d])
{
c[i] = a[e];
e++;
i++;
}
else
{
c[i] = b[d];
d++;
i++;
}
}
while(e < a.length)
{
e++;
}
while(d <b.length)
{
d++;
}
return Arrays.copyOf(c, i);
}
}
You don't have to pass n1 and n2 parameters. Arrays know their size, and you can get it using a.length and b.length. Also you want to return int[] to have the whole array instead of single int.
public static int[] Combine(int a[], int b[]) {
int[] c = Arrays.copyOf(a, a.length + b.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
I have a simple rotation function which takes an array and a number to rotate the numbers left
e.g. [1,2,3,4,5] & 2 - output: [3,4,5,1,2].
I want to know the most efficient way of completing this function, whether it would be to convert the int array into a string a splice it or whether to copy the array or to convert to an List<Integer>.
If anyone wants additional information please ask!
my solution at the moment:
static int[] rotLeft(int[] a, int d) {
int lengthOfArray = a.length;
int[] temp = new int[lengthOfArray];
for(int i = 0; i < lengthOfArray; i++){
int newLocation = (i + (lengthOfArray - d)) % lengthOfArray;
temp[newLocation] = a[i];
}
return temp;
}
Simple way to do it with O(n) complexity is as below along with handling of valid shifts int[] arr: is an int array, n=length of an array, d=how many shifts required.
public int[] leftRotate(int[] arr, int n, int d) {
int rot = 0;
int[] marr = new int[n];
if (d < 0 || d == 0 || d>n) {
return arr;
}
else {
for (int i = 0; i < n; i++) {
if (i < n - d) {
marr[i] = arr[i + d];
} else {
marr[i] = arr[rot];
rot++;
}
}
return marr;
}
}
public void GetArray(int[] arr, int n, int d) {
int[] arr1 = leftRotate(arr, n, d);
for (int j : arr1) {
System.out.println(j);
}
}
public static void main(String args[]) {
int[] arr = { 1,2,3,4,5 };
int n = arr.length;
Test2 obj = new Test2();
obj.GetArray(arr, n, 2);
}
Why don't you try this one
void Rotate(int arr[], int d, int n)
{
for (int i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n - 1; i++)
arr[i] = arr[i + 1];
arr[i] = temp;
}
and to call this invoke method like below
int arr[] = { 1, 2, 3, 4, 5 };
Rotate(arr, 2, 5);
I am unclear as to what this is asking me to do, and how I execute it, in reference to the int [] copyAndReplaceLessThan(int []a, int b) method. Any help would be greatly appreciated, thanks.
Task:
Test this method in main. Print both the original array and the returned array which the method, copyAndReplaceLessThan, returns. Since this method returns a value (an integer array), you need to “catch” the returned value in a local variable (of type int []) in the calling method (main).
For example: Assuming you pass the array “x” to the method and use array “y” to catch the returned value. Print x before you call the method and then print both x and y after calling the method.
public class ArrayExamples{
public static void swapInts(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}
public static void swapIntArrays(int [] a, int [] b){
int x;
for (int i=0; i<a.length; i++){
x = a[i];
a[i]=b[i];
b[i]=x;
}
}
public static void printArray(int[] a){
for(int i = 0; i < a.length; i++)
{
System.out.print(a[i] + " ");
}
System.out.println("");
}
public static void main(String args[]){
int [] one ={3,4};
int [] two ={7,8};
printArray(one);
printArray(two);
swapIntArrays(one,two);
printArray(one);
printArray(two);
System.out.println();
int [] x ={3,45,17,2,-1,44,9,23,67,2,-6,-23,-100,12,5,1212};
int b = 12;
printArray(x);
replaceLessThan(x,b);
printArray(x);
printArray(x);
copyAndReplaceLessThan(x,b);
printArray(x);
printArray(y);
}
public static void replaceLessThan(int []a, int b){
for(int i=0; i < a.length; i++){
if(a[i] < b){
a[i] = b;
}
else{
a[i] = a[i];
}
}
}
public static int[] copyAndReplaceLessThan(int []a, int b){
int []x = a;
int[]y = x;
for(int i=0; i < a.length; i++){
if(a[i] < b){
a[i] = b;
y[i] = b;
}
else{
a[i] = a[i];
y[i] = a[i];
}
}
return y;
}
}
public static int[] copyAndReplaceLessThan(int[] a, int b)
{
int[] x = a.clone();
for (int i = 0; i < x.length; i++)
{
if (x[i] < b)
{
x[i] = b;
}
}
return x;
}
Some problems:
You're supposed to leave the original array unaltered, yet you're setting its elements with
a[i] = b;
And there's no point to doing this
a[i] = a[i];
as you're just resetting the value to itself.
But your biggest problem is that you're not creating a new array, as you're supposed to. Instead, your "new" array (y) is referring to the original one (a), and therefore any changes to y are actually being made in the original.
I also recommend that you either test or try a for nullness.
I know this question was answered before, but I cant still handle it with my code.
Please can someone pointing out how can I fix it on this particular code. Id like to call trace(); but dont know where to call new trace. I tried different stuff from here but it does not work for me. Thank you!
package matr;
import java.util.Scanner;
final public class Matrix {
private final int M;
private final int N;
private double[][] data;
public Matrix(int M, int N) {
this.M = M;
this.N = N;
data = new double[M][N];
}
public Matrix(double[][] data) {
M = data.length;
N = data[0].length;
this.data = new double[M][N];
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
this.data[i][j] = data[i][j];
}
private Matrix(Matrix A) {
this(A.data);
}
public static Matrix random(int M, int N, int r) {
Matrix A = new Matrix(M, N);
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
A.data[i][j] = (Math.random() * r);
}
}
return A;
}
public double trace() {
// trace a = new trace();
double t = 0;
for (int i = 0; i < Math.min(M, N); i++) {
t += data[i][i];
}
return t;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("rows: ");
try {
int x = Math.abs(scan.nextInt());
System.out.println("columns: ");
int y = Math.abs(scan.nextInt());
System.out
.println("generate: ");
int r = scan.nextInt();
Matrix A = Matrix.random(x, y, r);
System.out.println("random A");
trace();
} catch (java.util.InputMismatchException e) {
System.out.println("Please enter a valid int");
}
}
}
call A.trace()
use lower-case names for variables and fields
your main method is static. and your trace() method is a non-static method of the Matrix class. That means you have to call trace() on an instance of Matrix.
You are trying to call a non-static method trace() from a static method main(). Since 'main' is static it can only refer to static variables and static methods within the class. You will need to use an instance of Matrix to call trace. for example:
Matrix A = Matrix.random(x,y,r);
A.trace();
You need to call the trace() method on an instance of the type Matrix.
You could do this simply by:
A.trace();