// a function to compute the size of the list using recursion // empty list has
ID: 3594620 • Letter: #
Question
// a function to compute the size of the list using recursion
// empty list has size 0
// You will want to create a helper function to do the recursion
public int sizeRecursive () {
}
// a function to compute the position of the first occurrence of NUM in the list,
// where NUM is passed as a parameter
// and positions are counted as an offset from the beginning.
//
// if NUM is 5.0 and 5.0 is the FIRST element, the position is 0
// if NUM is 5.0 and it does not appear, return -1
//
// you can write this iteratively or recursively, but you should only have one loop or recursive helper
// I would expect
// [0,1,2,5,5,5,5,5,8,9].positionOfFirstNumFromBeginning(5) == 3
// [0,1,3,4,5,5,2,5,8,9].positionOfFirstNumFromBeginning(2) == 6
public int positionOfFirstNumOccurrence (double num) {
return StdRandom.uniform (100); //TODO 3: fix this
}
// return the second-to-last value in the list,
// Precondition: the list has at least 2 values
// you can write this iteratively or recursively, but you should only have one loop or recursive helper
// you may not call any other method (e.g. size )
//
// [0,1,2,5,5,5,5,5,8,9].secondToLastValue() == 8
// [0,1].secondToLastValue() == 0
public double secondToLastValue ( ) {
return StdRandom.uniform (100); //TODO 4: fix this
}
// a function to delete the second node if it exists, if not, the list is unchanged
// [0,1,2,8,9].deleteSecondIfPossible() --> [0,2,8,9]
// [0,9].deleteSecondIfPossible() --> [0]
// [].deleteSecondIfPossible() --> []
public void deleteSecondIfPossible () {
// TODO 5: fix this
}
Explanation / Answer
import java.util.*;
//Class ArrayOperation
public class ArrayOperation
{
//Creates an array
double arr[] = {0,1,2,5,5,5,5,5,12,9};
//To store search number
double num;
//Counter
int c = 0;
int len = arr.length;
//Method to calculate length of string using recursion
public int sizeRecursive(String s)
{
//Checks if string is null return zero
if (s.equals(""))
return 0;
//Otherwise call the method recursively
else
return sizeRecursive(s.substring(1)) + 1;
}//End of method sizeRecursive()
//Method to search a number in array using recursion
public int positionOfFirstNumOccurrence (double num)
{
//Checks if the count value is equal to array length
if(c == arr.length-1)
//Return -1
return -1;
//checks if the current index position value is equal to the number given as parameter
else if(arr[c + 1] == num)
//Returns the index position
return (c+1);
//Otherwise
else
{
//Increase the counter by one
c++;
//Calls recursively
return positionOfFirstNumOccurrence(num);
}//End of else
}//End of main method positionOfFirstNumOccurrence()
//Method to return second last value using recursion
public double secondToLastValue ()
{
//Checks if the length is zero
if(len == 0)
//Return -1
return -1;
//Checks if the length is equals to length minus two
else if(len == arr.length-2)
//Returns the value at len position
return (arr[len]);
//Otherwise
else
{
//Decrease the length by one by one
len--;
//Calls recursively
return secondToLastValue();
}//End of else
}//End of method
//Stores the length of the array
int Length = arr.length;
public void deleteSecondIfPossible ()
{
//Checks if the length is zero
if(len == 0)
return;
//Checks if the length is equals to length minus two
else if(len == arr.length-2)
{
//Stores the next position value in the 2nd position
arr[len] = arr[len+1];
//Decrease the length by one
Length = arr.length-1;
return;
}//End of else
//Otherwise
else
{
//Decrease the len by one
len--;
//Calls recursively
secondToLastValue();
}//End of else
}//End of method deleteSecondIfPossible ()
//main method definition
public static void main(String[] args)
{
//Scanner class object to accept number to search
Scanner sc = new Scanner(System.in);
//ArrayOperation class object created
ArrayOperation a = new ArrayOperation();
//Creates a sting
String ss = "this is";
//To display size
System.out.println("Length of the String " + ss + " = " + a.sizeRecursive(ss));
//Accept a number to search
System.out.println(" Enter a number to search");
a.num = sc.nextDouble();
//To display the index position of the searched element
System.out.println("Searched number " + a.num + " found at " + a.positionOfFirstNumOccurrence(a.num));
//To display second last element
System.out.println("Second last value = " + a.secondToLastValue());
//To delete the second last element
System.out.print(" Bofore: ");
for(int x = 0; x < a.Length; x++)
System.out.print(a.arr[x] + ", ");
a.deleteSecondIfPossible ();
System.out.print(" After : ");
for(int x = 0; x < a.Length; x++)
System.out.print(a.arr[x] + ", ");
}//End of main method
}//End of class
Sample Run:
Length of the String this is = 7
Enter a number to search 3
Searched number 3.0 found at -1
Second last value = 12.0
Bofore: 0.0, 1.0, 2.0, 5.0, 5.0, 5.0, 5.0, 5.0, 12.0, 9.0,
After : 0.0, 1.0, 2.0, 5.0, 5.0, 5.0, 5.0, 5.0, 9.0,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.