Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Cut-and-paste the incomplete program given below into your editor, and fill in t

ID: 3631166 • Letter: C

Question

Cut-and-paste the incomplete program given below into your editor, and fill in the for loops (shown in red) to complete a program that creates a list of N random integers and counts the number of inversions in the list (the first loop is identical to the first loop you wrote for Part 1 of the lab).

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Random;

public class InversionCount
{
public static void main(String[] args)
{
PrintStream out = System.out;

// N is the number of elements in the initial list
final int N = Integer.parseInt(args[0]);

// the list
ArrayList list = new ArrayList();

// add N random integers between 1 and MAX to the list
final int MAX = 100;
Random rng = new Random();
for()
{

}
out.println(list);

int inversionCount = 0;

for()
{
for()
{




}
}
out.println(inversionCount + " inversions");
}
}

Explanation / Answer

Since you said not to correct anything except filling in the red part, I didn't.
The line final int N = Integer.parseInt(args[0]); generates an error though so I put it behind two slashes.

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Random;

public class InversionCount{
public static void main(String[] args){
PrintStream out = System.out;

// N is the number of elements in the initial list
// final int N = Integer.parseInt(args[0]);

// the list
ArrayList list = new ArrayList();

// add N random integers between 1 and MAX to the list
final int MAX = 100;
Random rng = new Random();
for(int i=0; i<MAX; i++){
list.add(rng.nextInt());
}

out.println(list);

int inversionCount = 0;

for (int i=0 ; i<MAX-1 ; i++){
for (int j=i+1 ; j<MAX; j++){
if ((Integer)list.get(i)>(Integer)list.get(j)) inversionCount++;
}
}
out.println(inversionCount + " inversions");
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote