package homework;
import java.util.Arrays;
import stdlib.*;
/**
CSC300Homework4 version 1.0
*
*
*
* Find the 3 Sections marked TODO:
*
* TODO #1: SumOddsRecursive
* TODO #2: ReverseArray
* TODO #3: MergeArrays
*
* You must not add static variables. You MAY add static
functions.
*
* It is okay to add functions, such as
*
* <pre>
* public static double sumOfOddsHelper (double[] list, int i)
{
* </pre>
*
* but it is NOT okay to add static variables, such as
*
* <pre>
* public static int x;
* </pre>
*
* As in homework 1,2,3 you must not change the declaration of any
method.
*
* You can edit the main function all you want. I will not run your
main
* function when grading. For example, you can "comment out"
sections of main
* when testing your functions
*
* This program does contain *some* testing code - however it is not
guaranteed to be exhaustive
* and you will need to manually examine the output to see if the
tests succeed or not
*/
public class CSC300Homework4 {
/**
* As a model for Problem 1, here are two functions to
find the minimum value of an array of ints
* an iterative version and a recursive version
*
* precondition: list is not empty
/** iterative version */
public static double minValueIterative (int[] list)
{
int result = list[0];
int i = 1;
while (i < list.length) {
if (list[i] <
result) result = list[i];
i = i + 1;
}
return result;
}
/** recursive version
* Find minimum of a list of size N starting at
location 0
* Smaller problem is : Find minimum of list of size
N-1, starting at 0
*
* precondition: list is not empty
*/
public static int minValueRecursive (int[] list)
{
return minValueHelper (list,
list.length);
}
private static int minValueHelper (int[] list, int n)
{
if (n == 1) // the list of size 1
is the single element list[0]
return list[0];
// the minimum of this list is just that element.
// else: find minimum of smaller list
int minOfSmallerList = minValueHelper( list, n-1); // recursive call, 'smaller' list
// now compare min of smaller
list to 'last' element of this list
// the list is of size n, the
'last' element is at position n-1
// because indexes start at
0.
int theMin;
if ( list[n-1] <
minOfSmallerList)
theMin =
list[n-1];
else
theMin =
minOfSmallerList;
return theMin;
}
/**
* PROBLEM 1: Translate the following summing function
from iterative to
* recursive.
*
* You should write a helper method. You may not use
any "fields" to solve
* this problem (a field is a variable that is declared
"outside" of the
* function declaration --- either before or
after).
* You may not use other Java classes/algorithms, e.g.
Arrays.sort
*
* Precondition: a list of ints, - maybe empty!
* Postcondition: the sum of the odd values is
returned
*/
public static int sumOfOdds (int[] a) {
int result = 0;
int i = 0;
while (i < a.length) {
if ( a[i] %2 ==
1)
result = result + a[i];
i = i + 1;
}
return result;
}
public static int sumOfOddsRecursive (int[] a)
{
return -1; // TODO 1 replace this
by a call to your helper function, then write the helper function
below
}
// this would be a good place to put the helper function for
#1
/**
* Here is an in-place iterative function to reverse an
array.
*
* in-place means: we don't create an extra array to
make our coding easier
*
*/
public static void reverseIterative (int[] a) {
int hi = a.length - 1;
int lo = 0;
while (lo < hi) {
int loVal =
a[lo];
int hiVal =
a[hi];
a[hi] =
loVal;
a[lo] =
hiVal;
lo = lo +
1;
hi = hi -
1;
}
}
/*
* * PROBLEM 2: Convert the above iterative function to a recursive
version
*
* You should write a helper method. You may not use
any "fields" to solve
* this problem (a field is a variable that is declared
"outside" of the
* function declaration --- either before or
after).
* You may not use any other methods
*
* Your helper function must be parameterized to allow
a smaller problem to
* be specified. How do you reverse an array of size
N?
* (the answer is likely NOT: reverse an array of size
N-1 ! Study the iterative
* version above.
*/
public static void reverseArray (int[] a) {
return; // TODO 2 replace this by a
call to your recursive helper function, then write the helper
function below
}
// a good place for your helper function for #2
/**
* PROBLEM 3: merge together two sorted arrays of char
into a new array.
*
* The examples below
* Example1 merge: [a c e g ] with [ b d f h] would
yield [a b c d e f g h]
* Example2 merge: [a f ] with [ b c h i] would yield
[a b c f h i]
* There is no guarantee about the size of either
array. When/if you run out of elements in
* either array, copy all the remaining elements from
the nonempty array to the the new array
* preconditions:
* both arrays are sorted low to high
* there are no duplicate values among the two
arrays
* either array may be empty
* postcondition: an array with all elements from both
arrays sorted from low to high
*
* You may not use any additional methods, sorting
routines etc
* For full credit, your solution may only go through
each array one time ( so in particular - no nested loops)
*
* You will need to create a new array inside the
function
* You do not have to write this recursively.
*/
public static char[] mergeArrays( char[] a, char[] b) {
char[] answer = new char[0]; //
an empty array to have something to return
return answer; // ToDo 3 . Fix
this.
}
/*
* testing functions and main.
* There are no Todo's for you in the code below.
*/
public static void mergeArrayTests() {
char one[] = new char[]
{'a','c','e','g','i','k'};
char two[] = new char[]
{'b','d','f'};
char[] combinedArray = mergeArrays(
one,two);
StdOut.println("merging: "+
Arrays.toString(one) + " " + Arrays.toString(two));
StdOut.println(" --> " +
Arrays.toString(combinedArray));
one = new char[]
{'a','c','e','g','i','k'};
two = new char[] {'b','d'};
combinedArray = mergeArrays( one,
two);
StdOut.println("merging: "+
Arrays.toString(one) + " " + Arrays.toString(two));
StdOut.println(" --> " +
Arrays.toString(combinedArray));
one = new char[]
{'a','c','e','g','i','k'};
two = new char[] {};
combinedArray = mergeArrays(
one,two);
StdOut.println("merging: "+
Arrays.toString(one) + " " + Arrays.toString(two));
StdOut.println(" --> " +
Arrays.toString(combinedArray));
one = new char[]
{'b','k'};
two = new char[]
{'a','d','f','h'};
combinedArray = mergeArrays(
one,two);
StdOut.println("merging: "+
Arrays.toString(one) + " " + Arrays.toString(two));
StdOut.println(" --> " +
Arrays.toString(combinedArray));
}
public static void main (String[] args) {
int[] list0 = new int[] {};
int[] list1 = new int[] { 5
};
int[] list2 = new int[] { 3, 4
};
int[] list3 = new int[] { 2, 3, 4
};
int[] list4 = new int[] { 1, 2, 4,
5 };
int[] list5 = new int[] { 6, 1, 2,
3, 8 };
StdOut.format(" list: %s sum of
odds: %d\n",Arrays.toString(list0), sumOfOddsRecursive
(list0));
StdOut.format(" list: %s sum of
odds: %d\n",Arrays.toString(list1), sumOfOddsRecursive
(list1));
StdOut.format(" list: %s sum of
odds: %d\n",Arrays.toString(list2), sumOfOddsRecursive
(list2));
StdOut.format(" list: %s sum of
odds: %d\n",Arrays.toString(list3), sumOfOddsRecursive
(list3));
StdOut.format(" list: %s sum of
odds: %d\n",Arrays.toString(list4), sumOfOddsRecursive
(list4));
StdOut.format(" list: %s sum of
odds: %d\n",Arrays.toString(list5), sumOfOddsRecursive
(list5));
StdOut.println();
StdOut.println ("Reverse:
Before: " + Arrays.toString(list1 ) );
reverseArray (list1);
StdOut.println (" After: " +
Arrays.toString (list1) + "\n" );
StdOut.println ("Reverse:
Before: " + Arrays.toString(list2 ) );
reverseArray (list2);
StdOut.println (" After: " +
Arrays.toString (list2) + "\n");
StdOut.println ("Reverse:
Before: " + Arrays.toString(list3 ) );
reverseArray (list3);
StdOut.println (" After: " +
Arrays.toString (list3) + "\n");
StdOut.println ("Reverse:
Before: " + Arrays.toString(list4 ) );
reverseArray (list4);
StdOut.println (" After: " +
Arrays.toString (list4) + "\n");
StdOut.println ("Reverse:
Before: " + Arrays.toString(list5 ) );
reverseArray (list5);
StdOut.println (" After: " +
Arrays.toString (list5) + "\n");
mergeArrayTests();
}
}
import java.util.Arrays;
/**
CSC300Homework4 version 1.0
*
*
*
* Find the 3 Sections marked TODO:
*
* TODO #1: SumOddsRecursive
* TODO #2: ReverseArray
* TODO #3: MergeArrays
*
* You must not add static variables. You MAY add static
functions.
*
* It is okay to add functions, such as
*
* <pre>
* public static double sumOfOddsHelper (double[] list, int i)
{
* </pre>
*
* but it is NOT okay to add static variables, such as
*
* <pre>
* public static int x;
* </pre>
*
* As in homework 1,2,3 you must not change the declaration of any
method.
*
* You can edit the main function all you want. I will not run your
main
* function when grading. For example, you can "comment out"
sections of main
* when testing your functions
*
* This program does contain *some* testing code - however it is not
guaranteed to be exhaustive
* and you will need to manually examine the output to see if the
tests succeed or not
*/
public class CSC300Homework4 {
/**
* As a model for Problem 1, here are two functions to find the
minimum value of an array of ints
* an iterative version and a recursive version
*
* precondition: list is not empty
/** iterative version */
public static double minValueIterative (int[] list) {
int result = list[0];
int i = 1;
while (i < list.length) {
if (list[i] < result) result = list[i];
i = i + 1;
}
return result;
}
/** recursive version
* Find minimum of a list of size N starting at location 0
* Smaller problem is : Find minimum of list of size N-1, starting
at 0
*
* precondition: list is not empty
*/
public static int minValueRecursive (int[] list) {
return minValueHelper (list, list.length);
}
private static int minValueHelper (int[] list, int n) {
if (n == 1) // the list of size 1 is the single element
list[0]
return list[0]; // the minimum of this list is just that
element.
// else: find minimum of smaller list
int minOfSmallerList = minValueHelper( list, n-1); // recursive call, 'smaller' list
// now compare min of smaller list to 'last' element of this
list
// the list is of size n, the 'last' element is at position
n-1
// because indexes start at 0.
int theMin;
if ( list[n-1] < minOfSmallerList)
theMin = list[n-1];
else
theMin = minOfSmallerList;
return theMin;
}
/**
* PROBLEM 1: Translate the following summing function from
iterative to
* recursive.
*
* You should write a helper method. You may not use any "fields" to
solve
* this problem (a field is a variable that is declared "outside" of
the
* function declaration --- either before or after).
* You may not use other Java classes/algorithms, e.g.
Arrays.sort
*
* Precondition: a list of ints, - maybe empty!
* Postcondition: the sum of the odd values is returned
*/
public static int sumOfOdds (int[] a) {
int result = 0;
int i = 0;
while (i < a.length) {
if ( a[i] %2 == 1)
result = result + a[i];
i = i + 1;
}
return result;
}
public static int sumOfOddsRecursive (int[] a) {
return sumOfOddsHelper(a,a.length); // TODO 1 replace this by a
call to your helper function, then write the helper function
below
}
// this would be a good place to put the helper function for
#1
public static int sumOfOddsHelper(int[] list, int i) {
if(i==0)
return 0;
else {
int var = list[i-1];
if(var%2==0)
var=0;
return var+sumOfOddsHelper(list,
i-1);
}
}
/**
* Here is an in-place iterative function to reverse an array.
*
* in-place means: we don't create an extra array to make our coding
easier
*
*/
public static void reverseIterative (int[] a) {
int hi = a.length - 1;
int lo = 0;
while (lo < hi) {
int loVal = a[lo];
int hiVal = a[hi];
a[hi] = loVal;
a[lo] = hiVal;
lo = lo + 1;
hi = hi - 1;
}
}
/*
* * PROBLEM 2: Convert the above iterative function to a recursive
version
*
* You should write a helper method. You may not use any "fields" to
solve
* this problem (a field is a variable that is declared "outside" of
the
* function declaration --- either before or after).
* You may not use any other methods
*
* Your helper function must be parameterized to allow a smaller
problem to
* be specified. How do you reverse an array of size N?
* (the answer is likely NOT: reverse an array of size N-1 ! Study
the iterative
* version above.
*/
public static void reverseArray (int[] a) {
reverseArrayHelper(a,0,a.length-1);// TODO 2 replace this by a call
to your recursive helper function, then write the helper function
below
}
// a good place for your helper function for #2
public static void reverseArrayHelper(int a[], int start, int
end)
{
int temp;
if (start >= end)
return;
temp = a[start];
a[start] = a[end];
a[end] = temp;
reverseArrayHelper(a, start+1, end-1);
}
/**
* PROBLEM 3: merge together two sorted arrays of char into a new
array.
*
* The examples below
* Example1 merge: [a c e g ] with [ b d f h] would yield [a b c d e
f g h]
* Example2 merge: [a f ] with [ b c h i] would yield [a b c f h
i]
* There is no guarantee about the size of either array. When/if you
run out of elements in
* either array, copy all the remaining elements from the nonempty
array to the the new array
* preconditions:
* both arrays are sorted low to high
* there are no duplicate values among the two arrays
* either array may be empty
* postcondition: an array with all elements from both arrays sorted
from low to high
*
* You may not use any additional methods, sorting routines
etc
* For full credit, your solution may only go through each array one
time ( so in particular - no nested loops)
*
* You will need to create a new array inside the function
* You do not have to write this recursively.
*/
public static char[] mergeArrays( char[] a, char[] b) {
int l1 = a.length;
int l2 = b.length;
char[] answer = new char[l1+l2]; // an empty array to have
something to return
int i = 0, j = 0, k = 0;
while (i<l1 && j <l2)
{
if (a[i] < b[j])
answer[k++] = a[i++];
else
answer[k++] = b[j++];
}
while (i < l1)
answer[k++] = a[i++];
while (j < l2)
answer[k++] = b[j++];
return answer; // ToDo 3 . Fix this.
}
/*
* testing functions and main.
* There are no Todo's for you in the code below.
*/
public static void mergeArrayTests() {
char one[] = new char[] {'a','c','e','g','i','k'};
char two[] = new char[] {'b','d','f'};
char[] combinedArray = mergeArrays( one,two);
System.out.println("merging: "+ Arrays.toString(one) + " " +
Arrays.toString(two));
System.out.println(" --> " +
Arrays.toString(combinedArray));
one = new char[] {'a','c','e','g','i','k'};
two = new char[] {'b','d'};
combinedArray = mergeArrays( one, two);
System.out.println("merging: "+ Arrays.toString(one) + " " +
Arrays.toString(two));
System.out.println(" --> " +
Arrays.toString(combinedArray));
one = new char[] {'a','c','e','g','i','k'};
two = new char[] {};
combinedArray = mergeArrays( one,two);
System.out.println("merging: "+ Arrays.toString(one) + " " +
Arrays.toString(two));
System.out.println(" --> " +
Arrays.toString(combinedArray));
one = new char[] {'b','k'};
two = new char[] {'a','d','f','h'};
combinedArray = mergeArrays( one,two);
System.out.println("merging: "+ Arrays.toString(one) + " " +
Arrays.toString(two));
System.out.println(" --> " +
Arrays.toString(combinedArray));
}
public static void main (String[] args) {
int[] list0 = new int[] {};
int[] list1 = new int[] { 5 };
int[] list2 = new int[] { 3, 4 };
int[] list3 = new int[] { 2, 3, 4 };
int[] list4 = new int[] { 1, 2, 4, 5 };
int[] list5 = new int[] { 6, 1, 2, 3, 8 };
System.out.format(" list: %s sum of odds:
%d\n",Arrays.toString(list0), sumOfOddsRecursive (list0));
System.out.format(" list: %s sum of odds:
%d\n",Arrays.toString(list1), sumOfOddsRecursive (list1));
System.out.format(" list: %s sum of odds:
%d\n",Arrays.toString(list2), sumOfOddsRecursive (list2));
System.out.format(" list: %s sum of odds:
%d\n",Arrays.toString(list3), sumOfOddsRecursive (list3));
System.out.format(" list: %s sum of odds:
%d\n",Arrays.toString(list4), sumOfOddsRecursive (list4));
System.out.format(" list: %s sum of odds:
%d\n",Arrays.toString(list5), sumOfOddsRecursive (list5));
System.out.println();
System.out.println ("Reverse: Before: " + Arrays.toString(list1
) );
reverseArray (list1);
System.out.println (" After: " + Arrays.toString (list1) + "\n"
);
System.out.println ("Reverse: Before: " + Arrays.toString(list2
) );
reverseArray (list2);
System.out.println (" After: " + Arrays.toString (list2) +
"\n");
System.out.println ("Reverse: Before: " + Arrays.toString(list3
) );
reverseArray (list3);
System.out.println (" After: " + Arrays.toString (list3) +
"\n");
System.out.println ("Reverse: Before: " + Arrays.toString(list4
) );
reverseArray (list4);
System.out.println (" After: " + Arrays.toString (list4) +
"\n");
System.out.println ("Reverse: Before: " + Arrays.toString(list5
) );
reverseArray (list5);
System.out.println (" After: " + Arrays.toString (list5) +
"\n");
mergeArrayTests();
}
}
Feel free to ask any doubts, if you face any difficulty in understanding.
Please upvote the answer if you find it
helpful
package homework; import java.util.Arrays; import stdlib.*; /** CSC300Homework4 version 1.0 * * * * Find...
import java.util.Arrays; import stdlib.*; public class CSC300Homework4 { /** * As a model for Problem 1, here are two functions to find the minimum value of an array of ints * an iterative version and a recursive version * * precondition: list is not empty /** iterative version */ public static double minValueIterative (int[] list) { int result = list[0]; int i = 1; while (i <...
Java Given the array, which has been converted to a linked list, I need to invoke a remove method to remove a number from the list. The output for the first array should come out like this Now list1 = 24->18->3-7->null Now list2 = 31->-9->5->21->4->0->8->null Now lsit3 = 5->0->1->null but i keep getting this output Now list1= 12->3->7->null Now list2= -7->null Now list3= 5->0->1->null int arr1[] = {24, 18, 12, 3, 7}; int arr2[] = {31, -9, 5, 21, 4,...
Fix all sections marked TO DO. Must show output. package JavaExcercise; import java.util.Arrays; import stdlib.*; /** * Do not change the declaration of any method. * Each of the functions below is meant to be SELF CONTAINED. This means that * you should use no other functions or classes unless otherwise indicated. * You should not use any HashSets or ArrayLists, or anything else. * In addition, each of your functions should go * through the argument array at most...
package exampleassignment; import java.util.Arrays; import stdlib.*; public class assignment { /** * valRange returns the difference between the maximum and minimum values in the * array; Max-Min. Precondition: the array is nonempty. Your solution must go * through the array at most once. * * Here are some examples (using "==" informally): * * <pre> * 0 == valRange (new double[] { -7 }) * 10 == valRange (new double[]...
import java.util.Arrays; import java.util.Random; import java.util.Scanner; /** * TODO Write a summary of the role of this class in the * MasterMind program. * * @author TODO add your name here */ public class MasterMind { /** * Prompts the user for a value by displaying prompt. * Note: This method should not add a new line to the output of prompt. * * After prompting the user, the method will consume an entire * line of input while reading...
Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList { private class Node { private Object data; //Assume data implemented Comparable private Node next, prev; private Node(Object data, Node pref, Node next) { this.data = data; ...
package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...
Solver.java package hw7; import java.util.Iterator; import edu.princeton.cs.algs4.Graph; import edu.princeton.cs.algs4.BreadthFirstPaths; public class Solver { public static String solve(char[][] grid) { // TODO /* * 1. Construct a graph using grid * 2. Use BFS to find shortest path from start to finish * 3. Return the sequence of moves to get from start to finish */ // Hardcoded solution to toyTest return...
I have the following classes SearchMain.java import java.security.SecureRandom; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * * DO NOT CHANGE THIS CODE * */ public class SearchMain { public static void main(String[] args) { ArraySearcher arraySearch = new ArraySearchImp(); SecureRandom secureRandom = new SecureRandom(); int[] sortedArray = generateRandomSortedIntArray(10); System.out.println(Arrays.toString(sortedArray)); Set<Integer> intSet = new HashSet<Integer>(); for(int i = 0; i < 5; i++) { ...