Question

In: Computer Science

6. Write a Java program to rotate an array (length 3) of integers in left direction...

6. Write a Java program to rotate an array (length 3) of integers in left direction
7. Write a Java program to store a collection of integers, sort them in ascending order and print the sorted integers.

Solutions

Expert Solution

Answer 6 ;

JAVA PROGRAM TO ROTATE AN ARRAY(LENGTH 3) OF INTEGERS IN LEFT DIRECTION.

import java.util.Scanner;

public class Main
{
   public static void main(String[] args) {

      Scanner sc=new Scanner(System.in);
      System.out.println("Enter array elements (length 3): ");

      int arr[] =new int[3] ;

      for(int i=0;i<3;i++)
      {
         int n=sc.nextInt();
         arr[i]=n;
      }

      //rotate array of integers in left direction.
      int temp=arr[0];
      arr[0]=arr[1];
      arr[1]=arr[2];
      arr[2]=temp;

      //printing the rotated elements of an array.
      System.out.println("After rotation of an array in left direction: ");
      for(int i=0;i<3;i++)
         System.out.print(arr[i]+" ");

   }
}


EXAMPLE :

Answer 7 ;

JAVA PROGRAM TO STORE A COLLECTION OF INTEGERS , SORT THEM IN ASCENDING ORDER AND PRINT THE SORTED INTEGERS.

import java.util.Scanner;

public class Main {
   public static void main(String[] args) {

      Scanner sc = new Scanner(System.in);
      System.out.println("Enter size of the collection of the integers:  ");
        int n = sc.nextInt();

      System.out.println("Enter the elements: ");
        int arr[]=new int[n];

        //storing a collection of integers.
        for(int i=0;i<n;i++)
        {
           int element=sc.nextInt();
           arr[i]=element;
        }

        //sorting them in ascending order.
      int len = arr.length;
      for (int i = 0; i < len-1; i++) {
         for (int j = 0; j < len - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
               // swap temp and arr[i]
               int temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
            }
         }
      }

      //printing the sorted integers.
      System.out.println("Sorted integers in ascending order: ");
      for(int i=0;i<n;i++)
         System.out.print(arr[i]+" ");

   }
}

EXAMPLE :


Related Solutions

Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0...
Write a Java program with comments that randomly generates an array of 500,000 integers between 0 and 499,999, and then prompts the user for a search key value. Estimate the execution time of invoking the linearSearch method in Listing A below. Sort the array and estimate the execution time of invoking the binarySearch method in Listing B below. You can use the following code template to obtain the execution time: long startTime = System.currentTimeMillis(); perform the task; long endTime =...
Write a program that initializes an array of 6 random integers and then prints 4 lines...
Write a program that initializes an array of 6 random integers and then prints 4 lines of output, containing the following: 1. Only the first and last element 2. Every element at an odd index 3. Every odd element 4. All elements in reverse order
1- Write it with C++ program §Write a function Rotate that rotates an array of size...
1- Write it with C++ program §Write a function Rotate that rotates an array of size n by d elements to the left §Use array as argument §In the main function, call the function Rotate and show the rotated array §Test your code For example: Input: [1 2 3 4 5 6 7], n = 7, d = 2 Output: [3 4 5 6 7 1 2] 2- Write it in C++ §Search Insert Position •Given a sorted array in...
write a program which will prompt an array of 12 integers then print the array as...
write a program which will prompt an array of 12 integers then print the array as an array of 4 columns on 3 rows
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT