The code snippet below is an example of pass by reference. We also provide a sample method
setZeroAt, which sets 0 at a position i in the array, to illustrate pass by reference.
public class Sample {
public static void setZeroAt(int [] arr, int i){
arr[i] = 0;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int sample[] = {1, 2, 3};
setZeroAt(sample, 1);
//Now sample looks like {1, 0, 3}
}
}
Write a Java method that takes an array and two integer indices a and b. If a and b are
valid integer indices for the array, swap the values in that position.
Also ensure that the two indices are valid. If not, do not swap the values. Remember that
when you pass an array as a parameter, it is passed by reference. Use the code snippet below.
For example, consider the array {0, 1, 2, 3}. For a = 0, b = 2, the swapped array must be
{2, 1, 0, 3}. You can swap with a = 2, b = 0 on the original array to get the same result.
import java.util.Scanner;
public class SwapArrayIndex {
public static void print_array(int [] arr){
for(int i = 0; i < arr.length; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//Start of Sample Code
int sample[] = {1, 2, 3};
setZeroAt(sample, 1);
//Now sample looks like {1, 0, 3}
//End of Sample Code
int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
print_array(arr);
int a = input.nextInt();
int b = input.nextInt();
swap(arr, a, b);
print_array(arr);
}
}
Part B: Random Shuffling
------------------------
Use swap method to perform random shuffling. Write a shuffle method that takes an array. It
generates two valid indices randomly, that can be swapped and calls the swap method to swap
the positions in the array. Look at question 1 for generating a valid index (but look out for
an obvious pitfall).
We need at least 10 more requests to produce the answer.
0 / 10 have requested this problem solution
The more requests, the faster the answer.
The code snippet below is an example of pass by reference. We also provide a sample...
Aim To improve the algorithm provided in Snippet 2.3 by reducing the number of passes. public void sortImprovement1(int[] numbers) { for (int i = 1; i < numbers.length; i++) { for (int j = 0; j < numbers.length - i; j++) { if (numbers[j] > numbers[j + 1]) { swap(numbers, j, j + 1); } } } } Snippet 2.3: Bubble sort improvement 1 Change the bubble sort method so that it stops sorting if the array is untouched after...
The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...
the code needs to be modifed. require a output for the
code
Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...
I need a java flowchart for the following code: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the input size: "); int n=sc.nextInt(); int arr[]=new int[n]; System.out.print("Enter the sequence: "); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(isConsecutiveFour(arr)) { System.out.print("yes the array contain consecutive number:"); for(int i=0;i<n;i++) System.out.print(arr[i]+" "); ...
need help editing or rewriting java code, I have this program
running that creates random numbers and finds min, max, median ect.
from a group of numbers,array. I need to use a data class and a
constructor to run the code instead of how I have it written right
now. this is an example of what i'm being asked
for.
This is my code:
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
// method to find the minimum number in...
Computer Science - Java
Explain the code step by step (in detailed order). Also explain
what the program required. Thanks
7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...
(a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) { System.out.println ("Hello"); x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) { sum = sum + i; i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...
You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; } There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...
must provide the following public interface: public static void insertSort(int [] arr); public static void selectSort(int [] arr); public static void quickSort(int [] arr); public static void mergeSort(int [] arr); The quick sort and merge sort must be implemented by using recursive thinking. So the students may provide the following private static methods: //merge method //merge two sorted portions of given array arr, namely, from start to middle //and from middle + 1 to end into one sorted portion, namely,...
Must be written in JAVA Code Modify Fig. 19.2 to use recursive method recursiveLinear-Search to perform a linear search of the array. The method should receive the search key and starting index as arguments. If the search key is found, return its index in the array; otherwise, return –1. Each call to the recursive method should check one index in the array. Figure 19.2 import java.security.SecureRandom; import java.util.Arrays; import java.util.Scanner; public class LinearSearchTest{ public static int linearSearch(int data[], int searchKey)...