Write a JAVA program that rotates a 1-dimensional by 2 elements. For example [1,2,3,4,5,6] rotated by two becomes [3,4,5,6,1,2].
File Name:
LeftRotatoin.java
Program:
import java.util.Scanner;
class LeftRotation
{
public static void main(String[] args) {
// Declaring a Scanner variable
for taking input
Scanner in = new
Scanner(System.in);
// Taking the no.of elements of the
array and storing
// it in variable n.
System.out.println("Enter no.of
values");
int n=in.nextInt();
// Taking the elements of the
Array and storing it in the
// array arr.
System.out.println("Enter
numbers");
int[] arr = new int[n];
for(int i=0; i<n; i++)
{
arr[i] =
in.nextInt();
}
// Printing the array that we've
taken.
System.out.println("\nArray that
you've entered:");
for(int i=0; i<n; i++)
{
System.out.print(arr[i]+" ");
}
// Create a new array to store
the resulting values.
int[] result = new int[n];
// If we rotate an array of
lenth "n" by "r" positions,
// then the values at an index "i"
will be moved to an index
// position which is equal to
(i-r+n)%n which means that,
// "i" is moved to "r" places left
and then, since, we have to
// neutralize the negative sign
(Which might come) we
// are adding "n" and since, the values
should be in
// range 0...n-1, we are finding modulus
of the obtained
// number (i-r+n) with n. Thus making the
new index in range of the 0...n-1
//here r is 2
for(int i=0; i<n; i++)
{
// you can
replace 2 with whatever integer
// and this
rotates the array that number times.
result[(i-2+n)%n] = arr[i];
}
// Printing the array after
rotation.
System.out.println("\n\nAfter
Rotaion: ");
for(int i=0; i<n; i++)
{
System.out.print(result[i]+" ");
}
}
}
Output:
Please appreciate solution if you like it,
feel free to ask doubts in comment section.
Write a JAVA program that rotates a 1-dimensional by 2 elements. For example [1,2,3,4,5,6] rotated by...
Write a java program that create a 2 dimensional array of type double of size 10 by 10. Fill the array with random numbers using a random number generator. Print the array contents. Write a java program that creates a file called data.txt that uses the PrintWriter class. Write the numbers 1 to 42 into the file. Close the file. Attach both files. Thank you in advance
Write a JAVA program to sort a given array of integers (1 Dimensional) in ascending order (from smallest to largest). You can either get the array as input or hardcode it inside your program.
1. Write a java program to find the sum of the elements on the diagonal of a matrix. 2. Write a java program to add two matrices. 3. Write a java program to find a given value in a matrix. 4. Write a java program to multiply two matrices. 5. Write a java program to find the transpose of a matrix.
Write a java program to remove duplicate elements from an array. Do not use Java library utilities or 3rd party tools to solve this problem. You need to use the “for loop” and manage the index as you traverse thru the array [i ] while looking for duplicates. For example: Example 1 --------------------- Original Array: 105 123 -921 -1 123 8 8 8 -921 Array with unique values: 105 123 -921 -1 8 Example 2 --------------------------- Original Array: 10 22...
Using Java. Write a program that creates a “triangular” two-dimensional array A of 10 rows. The first row has length 1, the second row has length 2, the third row has length 3, and so on. Then initialize the array using nested for loops so that the value of A[i][j] is i+j. Finally, print out the array in a nice triangular form.
In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...
Write a java program that specifies three parallel one dimensional arrays name length, width, and area. Each array should be capable of holding a number elements provided by user input. Using a for loop input values for length and width arrays. The entries in the area arrays should be the corresponding values in the length and width arrays (thus, area[i] = length [i]* width [i]) after data has been entered display the following output: (in Java)
Write a java Program Initialize Array. Write a Java method initArray() with the following header to initialize a one-dimensional array a such that the element values (integers) are twice the index value. For example, if i = 23, then a23 = 46. The function is void with one parameter -- the integer array of a[]. Then write a main() with an int[] array2i size 100 to call
write a java program that will perform the following. Given two finite sets, list all elements in the Cartesian product of these two sets. Given a finite set, list all elements of its power set.
Please write this in JAVA ONLY. Write a method with a void return value that inverts all the elements of a two-dimensional array of booleans (true becomes false and false becomes true). Include code to test your method. Your output should show all the values of the array prior to inverting them and afterwards.