Question

In: Computer Science

[jAVA] Assume the following array is defined and initialized (it will already contain numbers before your...

[jAVA] Assume the following array is defined and initialized (it will already contain numbers before your code runs):

public static void main( String [ ] args ) {

int [ ] numbers = . . . ; // An array of integers

1) Provide code that will print the entire contents of numbers in reverse order (from the end to the beginning)

2) Provide code that will print true if the numbers are in strictly ascending order (that is, if starting from the start of the array each item has a larger value than the one that came before it) and print false otherwise.

Solutions

Expert Solution

Java Code

public class Main
{
static int arraySortedOrNot(int numbers[], int n)
{
// Array has one or no element or the
// rest are already checked and approved.
if (n == 1 || n == 0)
return 1;

// Unsorted pair found (Equal values allowed)
if (numbers[n - 1] < numbers[n - 2])
return 0;

// Last pair was sorted
// Keep on checking
return arraySortedOrNot(numbers, n - 1);
}

// main function
public static void main(String[] args)
{
int numbers[] = { 20, 23, 23, 45, 78, 88 };
int n = numbers.length;
int i;
System.out.println("Original array: ");
for (i = 0; i < numbers.length; i++)
   {
System.out.print(numbers[i] + " ");
}
System.out.println();
System.out.println("Array in reverse order: ");
for (i = numbers.length-1; i >= 0; i--) //Loop through the array in reverse order
   {
System.out.print(numbers[i] + " ");
}
System.out.println();
System.out.println("Array is sorted or not :");
if (arraySortedOrNot(numbers, n) != 0)//To check array is sorted or not
System.out.println("True");
else
System.out.println("False");
}
}

Output

Original array:
20 23 23 45 78 88
Array in reverse order:
88 78 45 23 23 20
Array is sorted or not :
True


Related Solutions

IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Java programming language Array - Single identifier which can store multiple values Example initialized with values:...
Java programming language Array - Single identifier which can store multiple values Example initialized with values: int[] mySimpleArray = {4, 17, 99}; Example with fixed length but no values: int[] myFixedArray = new int[11]; The brackets identify the index. Each value has its own index number. NOTE: First element is the zeroth element: mySimpleArray[0] is 4, [1] is 17 Make a new Netbeans project called ArraysAndLoops Create the simple array with 4, 17, 99. Use System.out.println with the variable with...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the...
Programming Language: JAVA In this assignment you will be sorting an array of numbers using the bubble sort algorithm. You must be able to sort both integers and doubles, and to do this you must overload a method. Bubble sort work by repeatedly going over the array, and when 2 numbers are found to be out of order, you swap those two numbers. This can be done by looping until there are no more swaps being made, or using a...
Java Language The array s of ints contain integers each of which is between 1 and...
Java Language The array s of ints contain integers each of which is between 1 and 1000 (inclusive). Write code that stores in the variable ordinals the array of Strings consisting of each number followed by its ordinal number abbreviation, "st", "nd", "rd", or "th". For example if s is the array { 1, 2, 3, 4, 5, 10, 11, 12, 13, 21, 22, 973, 1000 } then your code should set ordinals to the array { "1st", "2nd", "3rd",...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain strings             red, orange, yellow, blue, indigo, violet write the statement to insert the String element “pink” to the end of the list. Assume the front of the list is on the left. 2. Outline the basic steps to remove a node from the beginning of a list. Completed answers will be given an immediate upvote :)
Java Programming I need an application that collects the user input numbers into an array and...
Java Programming I need an application that collects the user input numbers into an array and after that calls a method that sums all the elements of this array. and display the array elements and the total to the user. The user decides when to stop inputting the numbers. Thanks for your help!
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
Find the median (middle value) of an array of numbers, which we will assume are all...
Find the median (middle value) of an array of numbers, which we will assume are all distinct. For example, if there are 27 elements, then you must return the 14th smallest (which is also the 14th largest). The strategy used to solve this problem is somewhat like the quicksort algorithm, but has some important differences. Consider choosing a pivot element and partitioning the input so that the elements less than the pivot are to its left, and those larger than...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers...
Using import java.util.Random, create a simple java code that populates an array with 10 random numbers and then outputs the largest number.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT