Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Week 14 Lab
Solutions
Question 1:
Write program RandomArrray.java that asks the user to enter a positive integer n.
The program then create an array of integer of n element. The program then fill the
array if n random variable between 0 to 49.
Solution:
import java.util.Random;
import java.util.Scanner;
class question1 {
public static void main(String[] args) {
Random generator = new Random();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first positive integer:");
int n=0;
boolean isValid = false;
while(!isValid) // check the input is a valid integer.
{
if( sc.hasNextInt())
{ n = sc.nextInt(); isValid = true;}
else { System.out.println("Error! Invalid integer value. Try again"); }
sc.nextLine();
}
int [] arrint= new int[n];// create array of n elements
for(int i=0;i<n;i++)// fill arrary
arrint[i]= generator.nextInt(50);
for(int i=0;i<n;i++)// print array
System.out.println(arrint[i]);
}
}
Question 2: Modify RandomArrray.java to ArraySort.java so that program sort out the array in
ascending order. Use buble short to sort out the array.
Solution:
import java.util.Random;
import java.util.Scanner;
class question2 {
public static void main(String[] args) {
Random generator = new Random();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first positive integer:");
int n=0;
boolean isValid = false;
while(!isValid) // check the input is a valid integer.
{
if( sc.hasNextInt())
{ n = sc.nextInt(); isValid = true;}
else { System.out.println("Error! Invalid integer value. Try again"); }
sc.nextLine();
}
int [] arrint= new int[n];// create array of n elements
for(int i=0;i<n;i++)// fill arrary
arrint[i]= generator.nextInt(50);
for(int i = 0; i<n; i++) // buble sort
{
for(int j = 0; j < n-1; j++)
{
if(arrint[j]>arrint[j+1])
{
int temp= arrint[j];
arrint[j]=arrint[j+1];
arrint[j+1]=temp;
}
}
}
for(int i=0;i<n;i++)// print array
System.out.println(arrint[i]);
}
}
Question 3: modify RandomArrray.java to ArrayLinSearch.java so the user is asked to put
an extra integer y, and prints. The program should print ‘true’ if y is an element of the array
and false otherwise. use linear search to do this.
Solution:
import java.util.Random;
import java.util.Scanner;
class question3 {
public static void main(String[] args) {
Random generator = new Random();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first positive integer:");
int n=0;
boolean isValid = false;
while(!isValid) // check the input is a valid integer.
{
if( sc.hasNextInt())
{ n = sc.nextInt(); isValid = true;}
else { System.out.println("Error! Invalid integer value. Try again"); }
sc.nextLine();
}
int [] arrint= new int[n];// create array of n elements
for(int i=0;i<n;i++)// fill arrary
arrint[i]= generator.nextInt(50);
System.out.println(" enter the integer of interest:");
int num = sc.nextInt();
boolean exists=false;
for(int i = 0; i<n; i++) // buble sort
{
if(num==arrint[i]) exists=true;
}
for(int i=0;i<n;i++)// print array
System.out.println(arrint[i]);
System.out.println(exists);
}
}
Question 4: modify ArrayLinSearch.java to ArrayBinSearch.java which uses binary instead
of linear search.
Solution:
import java.util.Random;
import java.util.Scanner;
class question4 {
public static void main(String[] args) {
Random generator = new Random();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first positive integer:");
int n=0;
boolean isValid = false;
while(!isValid) // check the input is a valid integer.
{
if( sc.hasNextInt())
{ n = sc.nextInt(); isValid = true;}
else { System.out.println("Error! Invalid integer value. Try again"); }
sc.nextLine();
}
int [] arrint= new int[n];// create array of n elements
for(int i=0;i<n;i++)// fill arrary
arrint[i]= generator.nextInt(50);
// use buble sort to sort array in accending order (very inefficient method)
for(int i = 0; i<n; i++) // buble sort
{
for(int j = 0; j < n-1; j++)
{
if(arrint[j]>arrint[j+1])
{
int temp= arrint[j];
arrint[j]=arrint[j+1];
arrint[j+1]=temp;
}
}
}
System.out.println(" enter the integer of interest:");
int num = sc.nextInt();
// below is the binary search
boolean exists=false;
int start =0;
int end = arrint.length-1;
while(start<=end && !exists)
{
int m = (start + end)/2;
if(num == arrint[m]) {exists =true;}
else if(num<arrint[m]){end=m-1;}
else {start = m+1;}
}
// below is the binary search
for(int i=0;i<n;i++)// print array
System.out.println(arrint[i]);
System.out.println(exists);
}
}