a. Problem 1. Create an array of 30 random numbers that range between 1 and 100. Then, write a function that will receive a number from the user and determine if that number exists in the array or not. For instance, assume the array is: [2, 93, 14, 89, 12, 3, 81, 15, 14, 89, 52, 96, 71, 82, 5, 2, 41, 23, 52, 59, 44, 44, 88, 39, 49, 50, 97, 45, 48, 36] Now, assume the user enters 89, the program should output true. But, if the user enters 77, the program should output false. Approach: We will be implementing this method in two different ways. Both will be recursive. First, implement a method called findA (x,A), where x is the number we are looking for and A is an array. In the body of the function, compare x with the FIRST item that is in the array. If this first item is equal to X, return true. If not, remove the first item from A and call findA(x,A) on the revised list. If you call find on an empty list, you will want to return false. Writing any explicit loop in your code results a 0 for this question. Remember to provide pre- and post-conditions. How many recursive calls will you need to search the entire list? Next, copy the following trace table in your Word file and trace the input values to the function findA(x,A) for when A and x are initially [1, 6, 10, 14, 77, 82, 100] and 77, respectively. As a guideline, we have populated the tables with the values for the first 2 calls and the corresponding returned values for each of those calls. Note that in this example, the call numbers will be populated top down (i.e. time elapses from row x to row x+1) while returned values are populated bottom-up (i.e. the value in row x is returned before the value in rowx-1). You may want to use console.log to observe these values when you write your solution. call# x A value returned to this call 1 77 [1, 6, 10, 14, 77, 82, 100] TRUE 2 77 [6, 10, 14, 77, 82, 100] TRUE c. Problem 3. Yet again, create an array of 30 random numbers that range between 1 and 100. And yet again, write a function that will receive a number from the user and determine if that number exists in the array or not. But this time, start by SORTING your input list. After a sort, the list in problem 1 is as follows: [2, 2, 3, 5, 12, 14, 14, 15, 23, 36, 39, 41, 44, 44, 45, 48, 49, 50, 52, 52, 59, 71, 81, 82, 88, 89, 89, 93, 96, 97] Approach: Implement a method called findC(x, A, i, j), where x is the number we are looking for in array A, the first index of the array is i and the last index is j. We want to determine whether x exists in A anywhere between index i and index j. Your first call to this method will therefore look like this: findC(x, A, 0, A.length-1). In the body of your function, compare x with the item that is in the middle of the array, as you did before. As before, call the middle of index of the array mid. But this time, if x<=a[mid], recursively call your function to search ONLY the first half of the array, i.e. between index i and mid. If x>a[mid], recursively call your function to search ONLY the second half of the array, i.e. from mid+1 to j. If you call findC(x, A, i, j) when i is equal to j, note that there is only one element in the range. In this case, compare x with a[i] (or a[j]). If they are equal, return true, otherwise return false. If you call the function when j

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Please help me with this. So I created a program based on the instruction below but it does not seem to be working. It fix please provide me with an input that I can use an the output that should display. Thank you

Image Problem 1:

// Recursive function to find if a number exists in the array (findA)
function findA(x, A) {
    // Base case: If the array is empty, return false
    if (A.length === 0) {
        return false;
    }
    // Base case: If the first item in the array equals x, return true
    if (A[0] === x) {
        return true;
    }
    // Recursive case: Remove the first item from A and call findA on the revised list
    return findA(x, A.slice(1));
}

// Button click event listeners
document.getElementById("findAButton").addEventListener("click", function() {
    let numberToFind = parseInt(prompt("Enter a number to find in the array:"));
    let result = findA(numberToFind, randomArray) ? "Number exists in the array." : "Number does not exist in the array.";
    displayResult(numberToFind, result);
});

 Image problem 3:

// Recursive function to find if a number exists in the sorted array (findC)
function findC(x, A, i, j) {
    // Base case: If j is less than i, x does not exist in A
    if (j < i) {
        return false;
    }
    // Base case: If i equals j, compare x with A[i]
    if (i === j) {
        return x === A[i];
    }
    // Calculate the middle index of the array
    let mid = Math.floor((i + j) / 2);
    // Compare x with the middle item of the array
    if (x === A[mid]) {
        return true;
    } else if (x < A[mid]) {
        // Recursive case: Search the first half of the array
        return findC(x, A, i, mid);
    } else {
        // Recursive case: Search the second half of the array
        return findC(x, A, mid + 1, j);
    }
}

// Button click event listener for findC
document.getElementById("findCButton").addEventListener("click", function() {
    let numberToFind = parseInt(prompt("Enter a number to find in the sorted array:"));
    let result = findC(numberToFind, sortedArray, 0, sortedArray.length - 1) ? "Number exists in the sorted array." : "Number does not exist in the sorted array.";
    displayResult(numberToFind, result);
});

a. Problem 1. Create an array of 30 random numbers that range between 1 and 100.
Then, write a function that will receive a number from the user and determine if
that number exists in the array or not.
For instance, assume the array is:
[2, 93, 14, 89, 12, 3, 81, 15, 14, 89, 52, 96, 71, 82, 5, 2, 41, 23, 52, 59, 44, 44, 88, 39,
49, 50, 97, 45, 48, 36]
Now, assume the user enters 89, the program should output true. But, if the user
enters 77, the program should output false.
Approach:
We will be implementing this method in two different ways. Both will be recursive.
First, implement a method called findA (x,A), where x is the number we are looking
for and A is an array. In the body of the function, compare x with the FIRST item that
is in the array. If this first item is equal to X, return true. If not, remove the first item
from A and call findA(x,A) on the revised list. If you call find on an empty list, you
will want to return false.
Writing any explicit loop in your code results a 0 for this question. Remember to provide
pre- and post-conditions. How many recursive calls will you need to search the entire
list?
Next, copy the following trace table in your Word file and trace the input values to
the function findA(x,A) for when A and x are initially [1, 6, 10, 14, 77, 82, 100]
and 77, respectively. As a guideline, we have populated the tables with the values for
the first 2 calls and the corresponding returned values for each of those calls. Note
that in this example, the call numbers will be populated top down (i.e. time elapses
from row x to row x+1) while returned values are populated bottom-up (i.e. the
value in row x is returned before the value in rowx-1). You may want to use
console.log to observe these values when you write your solution.
call#
x
A
value returned to
this call
1
77
[1, 6, 10, 14, 77, 82, 100]
TRUE
2
77
[6, 10, 14, 77, 82, 100]
TRUE
Transcribed Image Text:a. Problem 1. Create an array of 30 random numbers that range between 1 and 100. Then, write a function that will receive a number from the user and determine if that number exists in the array or not. For instance, assume the array is: [2, 93, 14, 89, 12, 3, 81, 15, 14, 89, 52, 96, 71, 82, 5, 2, 41, 23, 52, 59, 44, 44, 88, 39, 49, 50, 97, 45, 48, 36] Now, assume the user enters 89, the program should output true. But, if the user enters 77, the program should output false. Approach: We will be implementing this method in two different ways. Both will be recursive. First, implement a method called findA (x,A), where x is the number we are looking for and A is an array. In the body of the function, compare x with the FIRST item that is in the array. If this first item is equal to X, return true. If not, remove the first item from A and call findA(x,A) on the revised list. If you call find on an empty list, you will want to return false. Writing any explicit loop in your code results a 0 for this question. Remember to provide pre- and post-conditions. How many recursive calls will you need to search the entire list? Next, copy the following trace table in your Word file and trace the input values to the function findA(x,A) for when A and x are initially [1, 6, 10, 14, 77, 82, 100] and 77, respectively. As a guideline, we have populated the tables with the values for the first 2 calls and the corresponding returned values for each of those calls. Note that in this example, the call numbers will be populated top down (i.e. time elapses from row x to row x+1) while returned values are populated bottom-up (i.e. the value in row x is returned before the value in rowx-1). You may want to use console.log to observe these values when you write your solution. call# x A value returned to this call 1 77 [1, 6, 10, 14, 77, 82, 100] TRUE 2 77 [6, 10, 14, 77, 82, 100] TRUE
c. Problem 3. Yet again, create an array of 30 random numbers that range between 1
and 100. And yet again, write a function that will receive a number from the user
and determine if that number exists in the array or not. But this time, start by
SORTING your input list. After a sort, the list in problem 1 is as follows:
[2, 2, 3, 5, 12, 14, 14, 15, 23, 36, 39, 41, 44, 44, 45, 48,
49, 50, 52, 52, 59, 71, 81, 82, 88, 89, 89, 93, 96, 97]
Approach:
Implement a method called findC(x, A, i, j), where x is the number we are
looking for in array A, the first index of the array is i and the last index is j. We want
to determine whether x exists in A anywhere between index i and index j. Your first
call to this method will therefore look like this: findC(x, A, 0, A.length-1).
In the body of your function, compare x with the item that is in the middle of the
array, as you did before. As before, call the middle of index of the array mid. But this
time, if x<=a[mid], recursively call your function to search ONLY the first half of the
array, i.e. between index i and mid. If x>a[mid], recursively call your function to
search ONLY the second half of the array, i.e. from mid+1 to j.
If you call findC(x, A, i, j) when i is equal to j, note that there is only one
element in the range. In this case, compare x with a[i] (or a[j]). If they are equal,
return true, otherwise return false. If you call the function when j<i, that also means
x does not exist in A and you should return false.
Writing any explicit loop in your code results a 0 for this question. Remember to provide
pre- and post-conditions. How many recursive calls will you need to search the entire
list? Do you think this implementation will run more quickly than your other
implementations? Why or why not?
Now copy the trace table below in your Word file and trace your function
findC (x,A,i,j) for when A, x, i, and j are initially [1, 6, 10, 14, 77, 82, 100],
77, 0, and 7, respectively. As a guideline, we have populated the tables with the
values for the first 2 calls and the corresponding returned values for each of those
calls.
call#
A
value returned to
this call
1
2
77
77
[1, 6, 10, 14, 77, 82, 100]
TRUE
77
[77, 82, 100]
TRUE
Transcribed Image Text:c. Problem 3. Yet again, create an array of 30 random numbers that range between 1 and 100. And yet again, write a function that will receive a number from the user and determine if that number exists in the array or not. But this time, start by SORTING your input list. After a sort, the list in problem 1 is as follows: [2, 2, 3, 5, 12, 14, 14, 15, 23, 36, 39, 41, 44, 44, 45, 48, 49, 50, 52, 52, 59, 71, 81, 82, 88, 89, 89, 93, 96, 97] Approach: Implement a method called findC(x, A, i, j), where x is the number we are looking for in array A, the first index of the array is i and the last index is j. We want to determine whether x exists in A anywhere between index i and index j. Your first call to this method will therefore look like this: findC(x, A, 0, A.length-1). In the body of your function, compare x with the item that is in the middle of the array, as you did before. As before, call the middle of index of the array mid. But this time, if x<=a[mid], recursively call your function to search ONLY the first half of the array, i.e. between index i and mid. If x>a[mid], recursively call your function to search ONLY the second half of the array, i.e. from mid+1 to j. If you call findC(x, A, i, j) when i is equal to j, note that there is only one element in the range. In this case, compare x with a[i] (or a[j]). If they are equal, return true, otherwise return false. If you call the function when j<i, that also means x does not exist in A and you should return false. Writing any explicit loop in your code results a 0 for this question. Remember to provide pre- and post-conditions. How many recursive calls will you need to search the entire list? Do you think this implementation will run more quickly than your other implementations? Why or why not? Now copy the trace table below in your Word file and trace your function findC (x,A,i,j) for when A, x, i, and j are initially [1, 6, 10, 14, 77, 82, 100], 77, 0, and 7, respectively. As a guideline, we have populated the tables with the values for the first 2 calls and the corresponding returned values for each of those calls. call# A value returned to this call 1 2 77 77 [1, 6, 10, 14, 77, 82, 100] TRUE 77 [77, 82, 100] TRUE
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education