SOMEONE PLEASE HELP. SOMEONE GAVE ME WRONG ANSWERS. $120.00 FOR CORRECT ANSWERS
ID: 3587377 • Letter: S
Question
SOMEONE PLEASE HELP. SOMEONE GAVE ME WRONG ANSWERS. $120.00 FOR CORRECT ANSWERS
A) Write a pseudo-code description for a method of finding the smallest and largest numbers in an array of integers. Compare your pseudo-code to a Java method that would do the same thing.
(B) Write a short Java program that outputs all of the possible strings formed using each of the characters ‘c’, ‘a’, ‘t’, ‘d’, ‘o’, and ‘g’ exactly once.
(C) Draw the recursion trace for the execution of the method PuzzleSolve(3, S, U) (Code Fragment 5.11), where S is empty and U = {a,b,c,d}.
(D) Describe a fast recursive algorithm for reversing a singly linked list L, so that the ordering of the nodes becomes opposite to what it was before.
(E) Describe a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary n (see Creativity Exercise C-5.16 for more details).
(F) Give a pseudo-code description for the O(n)-time algorithm for computing the power function, p(x,n). Draw the recursion trace of this algorithm to compute p(2,5).
(G) Given a set A = {a1, a2, …, an} of n integers, use pseudo-code to describe an efficient method of computing each of the partial sums sk = a1 + a2 + …+ ak for k = 1,2,…,n. What is the running time for this method?
Explanation / Answer
A) Write a pseudo-code description for a method of finding the smallest and largest numbers in an array of integers. Compare your pseudo-code to a Java method that would do the same thing.
Psedue code
Smallest(arr)
min = -infinite
for element in arr
if element < min
update min <- element
return min
Largest(arr)
max = infinite
for element in arr
if element > max
update max <- element
return max
Java methods
int smallest(int[] arr) {
int min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < min) min = arr[i];
}
return min;
}
int largest(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
return max;
}
(B) Write a short Java program that outputs all of the possible strings formed using each of the characters ‘c’, ‘a’, ‘t’, ‘d’, ‘o’, and ‘g’ exactly once.
public static String getNewStringBySwap(String str, int i, int j)
{
char[] arr = str.toCharArray();
char temp;
temp = arr[i] ;
arr[i] = arr[j];
arr[j] = temp;
return String.valueOf(arr);
}
private static void printAllString(String str, int start, int end)
{
if (start == end)
System.out.println(str);
else
{
for (int i = start; i <= end; i++)
{
str = getNewStringBySwap(str,start,i);
printAllString(str, start+1, end);
str = getNewStringBySwap(str,start,i);
}
}
}
public static void main(String[] args)
{
printAllString("catdog", 0, 5);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.