Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please correct my code. I don't know what to write to express this if (messyArray[i] >= messyArray[j]) // AND ELEMENT IS NOT ALREADY IN orderedArray

Please correct my code. I don't know what to write to express this

if (messyArray[i] >= messyArray[j]) // AND ELEMENT IS NOT ALREADY IN orderedArray

I am trying to order messyArray by creating a new array called orderedArray and iterating through each element to determine if its the biggest or not. This should (in theory) give me

orderedArray: [5,4,3,2,1]

I know I suck in Java. Sorry about that

image

723 1.2 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Run: y *+ H A A 4-4-4 import java.util.Arrays; public class tryit { public static void main(String[] args) { int[] messyArray; messyArray = new int[]{3, 4, 2, 1, 5}; int[] orderedArray; 11 orderedArray = new int[messyArray.Length]; System.out.println("messyArray: + Arrays.toString(messyArray)); System.out.println("ordered Array: +Arrays.toString (orderedArray)); for (int i-0; i < messyArray.length-1; i++) { if (messyArray[i] > messyArray[i+1]) { 11 System.out.println(messy Array[i] + > + messyArray[i+1]); A tryit X } for (int j-0; j < messy Array.length-1; j++) { if (messyArray[i] >= messyArray[j]) // AND ELEMENT IS NOT ALREADY IN orderedArray messyArray[i]; } } orderedArray [j] System.out.println("messyArray: System.out.println("orderedArray: messyArray: [3, 4, 2, 1, 5] ordered Array: [0, 0, 0, 0, 0] 4 > 2 2 > 1 messyArray: [3, 4, 2, 1, 5] orderedArray: [4, 4, 2, 2, 0] = /Library/Java/JavaVirtualMachines/amazon-corretto-11.jdk/Contents/Home/bin/java -javaagent: /Applications/IntelliJ Process finished with exit code 0 + Arrays.toString (messyArray)); +Arrays.tostring(ordered Array));

Step by Step Solution

There are 3 Steps involved in it

Step: 1

It looks like you are attempting to implement a simple sorting algorithm to create an ordered array ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting

Authors: Carl S. Warren, James M. Reeve, Jonathan E. Duchac

22nd Edition

324401841, 978-0-324-6250, 0-324-62509-X, 978-0324401844

More Books

Students also viewed these Programming questions

Question

7. What is the relationship between birth order and homosexuality?

Answered: 1 week ago

Question

5. What does the enzyme 5a-reductase 2 do?

Answered: 1 week ago