aCreate 2 arrays (array1 and array2) prompt the user for a number that is greate
ID: 3603736 • Letter: A
Question
aCreate 2 arrays (array1 and array2) prompt the user for a number that is greater than 100 that will serve as the size for the arrays (both arrays will have the same user input size).
bGenerate this amount of random numbers to fill these arrays – the random numbers must range from 1 to 99.
cWrite 1 sequential search voided function (method) name SequentialSearch() that accepts two parameters – array array1 and a target value to search for. Call this method 3 times with a target value of 1, next with a target value of 50 and next with a target value of 100. Each time you call this method, you must invoke a clock to see how long it takes to execute and print out the time with an appropriate message.
dWrite 1 binary search voided function (method) using only loops, name LoopSearch() that accepts two parameters – array2 and a target value to search for. Call this method calling 3 times with a target value of 1, next with a target value of 50 and next with a target value of 100. Each time you call this method, you must invoke a clock to see how long it takes to execute and add the time it took to sort the array1 to this time and print out the time with an appropriate message.
eWrite 1 binary search voided function (method) that uses recursion only, name BinarySearch() that accepts two parameters – array2 and a target value to search for. Call this method calling 3 times with a target value of 1, next with a target value of 50 and next with a target value of 100. Each time you call this method, you must invoke a clock to see how long it takes to execute and add the time it took to sort the array2 to this time and print out the time with an appropriate message.
fAfter you get the time, give an analysis as to which you think is a better algorithm to use and under what circumstance it is better to be used.
Can someone help me solved this please???
Explanation / Answer
>>> N = 5
>>> import random
>>> [[random.random() for i in range(N)] for j in range(N)]
[[0.9520388778975947, 0.29456222450756675, 0.33025941906885714, 0.6154639550493386, 0.11409250305307261], [0.6149070141685593, 0.3579148659939374, 0.031188652624532298, 0.4607597656919963, 0.2523207155544883], [0.6372935479559158, 0.32063181293207754, 0.700897108426278, 0.822287873035571, 0.7721460935656276], [0.31035121801363097, 0.2691153671697625, 0.1185063432179293, 0.14822226436085928, 0.5490604341460457], [0.9650509333411779, 0.7795665950184245, 0.5778752066273084, 0.3868760955504583, 0.5364495147637446]]
>> import numpy as np
>>> np.random.random((N,N))
array([[ 0.26045197, 0.66184973, 0.79957904, 0.82613958, 0.39644677],
[ 0.09284838, 0.59098542, 0.13045167, 0.06170584, 0.01265676],
[ 0.16456109, 0.87820099, 0.79891448, 0.02966868, 0.27810629],
[ 0.03037986, 0.31481138, 0.06477025, 0.37205248, 0.59648463],
[ 0.08084797, 0.10305354, 0.72488268, 0.30258304, 0.230913 ]])
import random
>>> N = 3
>>> [random.random() for i in range(N)]
[0.24578599816668256, 0.34567935734766164, 0.6482845150243465]
>>> M = 3
>>> [[random.random() for i in range(N)] for j in range(M)]
[[0.9883394519621589, 0.6533595743059281, 0.866522328922242], [0.5906410405671291, 0.4429977939796209, 0.9472377762689498], [0.6883677407216132, 0.8215813727822125, 0.9770711299473647]]
import java.util.Random;
public class NumberList {
private static double[] anArray;
public static double[] list(){
anArray = new double[10];
return anArray;
}
public static void print(){
for(double n: anArray){
System.out.println(n+" ");
}
}
public static double randomFill(){
Random rand = new Random();
int randomNum = rand.nextInt();
return randomNum;
}
public static void main(String args[]) {
}
}
public static double[] list(){
anArray = new double[10];
return anArray;
}
public static double[] list() {
anArray = new double[10];
for(int i=0;i<anArray.length;i++)
{
anArray[i] = randomFill();
}
return anArray;
}
import java.util.Random;
public class NumberList {
private static double[] anArray;
public static double[] list(){
anArray = new double[10];
return anArray;
}
public static void print(){
for(double n: anArray){
System.out.println(n+" ");
}
}
public static double randomFill(){
Random rand = new Random();
int randomNum = rand.nextInt();
return randomNum;
}
public static void main(String args[]) {
list();
for(int i = 0; i < anArray.length; i++){
anArray[i] = randomFill();
}
print();
}
}
private static double[] anArray;
public static void main(String args[]) {
anArray = new double[10]; // create the Array with 10 slots
Random rand = new Random(); // create a local variable for Random
for (int i = 0; i < 10; i++) // create a loop that executes 10 times
{
anArray[i] = rand.nextInt(); // each execution through the loop
// generates a new random number and
// stores it in the array at the
// position i of the for-loop
}
printArray(); // print the result
}
private static void printArray() {
for (int i = 0; i < 10; i++) {
System.out.println(anArray[i]);
}
}
public class RandomCentroidLocator {
public static void main(String [] args){
int min =0;
int max = 10;
int numCentroids = 10;
ArrayList<Integer> values = randomCentroids(min, max, numCentroids);
for(Integer i : values){
System.out.print(i+" ");
}
}
private static boolean unique(ArrayList<Integer> arr, int num, int numCentroids) {
boolean status = true;
int count=0;
for(Integer i : arr){
if(i==num){
count++;
}
}
if(count==1){
status = true;
}else if(count>1){
status =false;
}
return status;
}
// generate random centroid Ids -> these Ids can be used to retrieve data
// from the data Points generated
// simply we are picking up random items from the data points
// in this case all the random numbers are unique
// it provides the necessary number of unique and random centroids
private static ArrayList<Integer> randomCentroids(int min, int max, int numCentroids) {
Random random = new Random();
ArrayList<Integer> values = new ArrayList<Integer>();
int num = -1;
int count = 0;
do {
num = random.nextInt(max - min + 1) + min;
values.add(num);
int index =values.size()-1;
if(unique(values, num, numCentroids)){
count++;
}else{
values.remove(index);
}
} while (!( count == numCentroids));
return values;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.