Background: 1. Consider the following list of 8 integers, with indices shown in
ID: 3621898 • Letter: B
Question
Background:
1. Consider the following list of 8 integers, with indices shown in the top row, and the
numbers shown in the second row:
1 2 3 4 5 6 7 8
67 89 7 51 28 49 91 30
If we wish to order this list, we could swap pairs of values in the list, until the list of
numbers is in order.
Swapping elements 7 and 8, we have:
1 2 3 4 5 6 7 8
67 89 7 51 28 49 30 91
Swapping elements 3 and 1, we have:
1 2 3 4 5 6 7 8
7 89 67 51 28 49 30 91
Swapping elements 2 and 7, we have:
1 2 3 4 5 6 7 8
7 30 67 51 28 49 89 91
Swapping elements 5 and 2, we have:
1 2 3 4 5 6 7 8
7 28 67 51 30 49 89 91
Swapping elements 3 and 5, we have:
1 2 3 4 5 6 7 8
7 28 30 51 67 49 89 91
Swapping elements 6 and 5, we have:
1 2 3 4 5 6 7 8
7 28 30 51 49 67 89 91
Swapping elements 4 and 5, we have:
1 2 3 4 5 6 7 8
7 28 30 49 51 67 89 91
The list is now in order, and it took 7 swaps for us to put the list in order.
Assignment:
1. Write a program that creates an array of 8 random positive integers from 1 to 100.
This array should then be displayed to the user.
2. Your program should then prompt the user to enter two indices (the two indices
should be distinct and from 1 to 8, be sure to error-check for this).
3. Your program should swap the two indicated numbers, then check to see if the list is
in order. If the list is not in order, the user should be prompted again to enter two
indices for the numbers to swap. Repeat this process until the list of integers is in
order.
4. The program should terminate with the list printed, in order, and the number of swaps
printed to the screen.
Instructions:
1. Make sure that your output looks like the following 3 sample run outputs. User
input is shown in bold:
____________________________________________________________________
Welcome to the Game of SWAP! Here is your array of 8 integers:
1 2 3 4 5 6 7 8
59 22 16 94 53 83 9 51
Enter an index value to swap: 4
Enter another index value to swap: 8
1 2 3 4 5 6 7 8
59 22 16 51 53 83 9 94
Enter an index value to swap: 7
Enter another index value to swap: 7
Enter another index value to swap: 1
1 2 3 4 5 6 7 8
9 22 16 51 53 83 59 94
Enter an index value to swap: 9
Enter an index value to swap: 0
Enter an index value to swap: 6
Enter another index value to swap: 7
1 2 3 4 5 6 7 8
9 22 16 51 53 59 83 94
Enter an index value to swap: 3
Enter another index value to swap: 2
1 2 3 4 5 6 7 8
9 16 22 51 53 59 83 94
Good work! Your array is in order, and it took 4 swaps.
____________________________________________________________________
____________________________________________________________________
Welcome to the Game of SWAP! Here is your array of 8 integers:
1 2 3 4 5 6 7 8
28 59 82 28 41 2 79 28
Enter an index value to swap: 1
Enter another index value to swap: 6
1 2 3 4 5 6 7 8
2 59 82 28 41 28 79 28
Enter an index value to swap: 8
Enter another index value to swap: 3
1 2 3 4 5 6 7 8
2 59 28 28 41 28 79 82
Enter an index value to swap: 2
Enter another index value to swap: 6
1 2 3 4 5 6 7 8
2 28 28 28 41 59 79 82
Good work! Your array is in order, and it took 3 swaps.
____________________________________________________________________
Welcome to the Game of SWAP! Here is your array of 8 integers:
1 2 3 4 5 6 7 8
29 13 61 62 6 99 43 52
Enter an index value to swap: 6
Enter another index value to swap: 8
1 2 3 4 5 6 7 8
29 13 61 62 6 52 43 99
Enter an index value to swap: 4
Enter another index value to swap: -1
Enter another index value to swap: 24
Enter another index value to swap: 7
1 2 3 4 5 6 7 8
29 13 61 43 6 52 62 99
Enter an index value to swap: 3
Enter another index value to swap: 3
Enter another index value to swap: 6
1 2 3 4 5 6 7 8
29 13 52 43 6 61 62 99
Enter an index value to swap: 3
Enter another index value to swap: 5
1 2 3 4 5 6 7 8
29 13 6 43 52 61 62 99
Enter an index value to swap: 1
Enter another index value to swap: 22
Enter another index value to swap: 3
1 2 3 4 5 6 7 8
6 13 29 43 52 61 62 99
Good work! Your array is in order, and it took 5 swaps.
____________________________________________________________________
Thanks!!!
Explanation / Answer
please rate - thanks
import java.util.*;
public class main
{public static void main(String[] args)
{int []a=new int[8];
Random r = new Random();
Scanner in= new Scanner(System.in);
int i,n,m,t,swap=0;
for(i=0;i<8;i++)
a[i]=r.nextInt(100)+1;
System.out.println("Welcome to the Game of SWAP! Here is your array of 8 integers:");
do
{print(a);
System.out.print("Enter an index value to swap: ");
n=in.nextInt();
do
{System.out.print("Enter another index value to swap: ");
m=in.nextInt();
}while(n==m);
t=a[n];
a[n]=a[m];
a[m]=t;
swap++;
}while(!sorted(a));
print(a);
System.out.println("Good work! Your array is in order, and it took "+swap+" swaps.");
}
public static void print (int a[])
{int i;
for(i=0;i<8;i++)
System.out.print(i+" ");
System.out.println();
for(i=0;i<8;i++)
System.out.print(a[i]+" ");
System.out.println();
}
public static boolean sorted(int a[])
{int i;
for(i=0;i<7;i++)
if(a[i]>a[i+1])
return false;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.