209k views
3 votes
The RecursionP2 class will have only one class level variables: an ArrayList of Integers that will store a data set on which you will be performing different operations using recursive function. All these functions can also be done using iteration, however, you are restricted to only use recursion for full credit. Your program might be randomly checked and any non-recursive approach will be marked down.

The following non-static public methods have to be declared in your class:

Constructor (1-arg)

The constructor will accept in an 1-D array of type int of unknown length and add every element to the class level ArrayList.
reverseList(ArrayList)

The method will accept in an ArrayList of type Integer and return a new ArrayList of type Integer that has all the elements in the reverse order. You are only allowed to use recursion for this method.
reverseList()

The method will use the class level ArrayList and return a new ArrayList of type Integer that has all the elements in the reverse order. You are only allowed to use recursion for this method.
Hint: This method is a special case of reverseList(ArrayList) and you are allowed to use that method if it helps.
toOddList(ArrayList )

The method will use the class level ArrayList as a parameter and return a new ArrayList of type Integer that contains all the odd indexed numbered elements of the class level ArrayList. You are only allowed to use recursion for this method.
toOddList()

The method will accept in an ArrayList of type Integer and return a new ArrayList of type Integer that contains all the odd indexed numbered elements of the class level ArrayList.
Hint: This method is a special case of toOddList(ArrayList) and you are allowed to use that method if it helps.
toEvenRevList(ArrayList )

The method will accept in an ArrayList of type Integer as a parameter and return a new ArrayList of type Integer that contains all the even indexed numbered elements of the class level ArrayList in reverse order. You are only allowed to use recursion for this method.
You can use the reverseList() method as a helper method.
toEvenRevList()

The method will use the class level ArrayList and return a new ArrayList of type Integer that contains all the even indexed numbered elements of the class level ArrayList in reverse order. You are only allowed to use recursion for this method.
Hint: You can use toEvenRevList(ArrayList) or reverseList(ArrayList) as a helper method.
retPenultimate(ArrayList )

The method will accept in an ArrayList of type Integer and return an int which is the last element of the ArrayList.If the list is empty/null, it should return -1. As usual, you are only allowed to use recursion for this method and the use of reverseList() is prohibited.
getList()

The method would return the class level ArrayList. The return type is ArrayList.
Example

Original: [2, 4, 6, 8, 10]
reverseList(): [10, 8, 6, 4, 2]
toOddList(): [4, 8]
toEvenRevList(): [10, 6, 2]
retPenultimate(): 10

User Fishfood
by
4.2k points

2 Answers

2 votes

Answer:

C++.

Step-by-step explanation:

#include <iostream>

#include <iterator>

using namespace std;

/////////////////////////////////////////////////

class RecursionP2 {

int array_size, count;

int array_list[8];

public:

RecursionP2(int array_list[], int array_size) {

this->array_size = array_size;

count = 0;

for (int i = 0; i < array_size; i++) {

this->array_list[i] = array_list[i];

}

}

/////////////////////////////////////////////

void reverseList(int array_list[], int size) {

if (size <= 1)

return;

else {

int temp;

temp = array_list[0];

array_list[0] = array_list[size-1];

array_list[size-1] = temp;

return reverseList(array_list+1, size-2);

}

}

void reverseList() {

this->reverseList(array_list, array_size);

cout<<"{";

for (int i = 0; i < array_size-1; i++) {

cout<<array_list[i]<<", ";

}

cout<<array_list[array_size-1]<<"}";

}

/////////////////////////////////////////////

void oddList(int array_list[], int index) {

if (index >= array_size)

return;

else {

int temp = array_list[index];

array_list[index] = array_list[count];

array_list[count] = temp;

count = count + 1;

return oddList(array_list, index+2);

}

}

void oddList() {

this->oddList(array_list, 1);

cout<<"{";

for (int i = 0; i < count-1; i++) {

cout<<array_list[i]<<", ";

}

cout<<array_list[count-1]<<"}";

}

/////////////////////////////////////////////

void evenRevList(int array_list[], int index) {

if (index >= array_size)

return;

else {

int temp = array_list[index];

array_list[index] = array_list[count];

array_list[count] = temp;

count = count + 1;

return oddList(array_list, index+2);

}

}

void evenRevList() {

this->evenRevList(array_list, 0);

this->reverseList(array_list, count);

cout<<"{";

for (int i = 0; i < count-1; i++) {

cout<<array_list[i]<<", ";

}

cout<<array_list[count-1]<<"}";

}

/////////////////////////////////////////////

int retPenultimate(int array_list[], int size) {

if (size < 1) {

return -1;

}

else if (size == 1) {

return array_list[0];

}

else {

return retPenultimate(array_list+1, size-1);

}

}

/////////////////////////////////////////////

int* getList() {

return array_list;

}

};

/////////////////////////////////////////////////

int main() {

int array_list[] = {2, 4, 6, 8, 10, 12 ,4, 1};

int array_size = distance(begin(array_list), end(array_list));

RecursionP2 recursionp2_1(array_list, array_size);

/////////////////////////////////////////////

//recursionp2_1.reverseList();

//recursionp2_1.oddList();

//recursionp2_1.evenRevList();

cout<<recursionp2_1.retPenultimate(array_list, array_size);

/////////////////////////////////////////////

return 0;

}

User Cullimorer
by
5.7k points
2 votes

Answer / Explanation:

Let us define what a Recursion process is before we go ahead to constructing:

Recursion method is a process of solving problems where the solution focus on resolutions to minor or smaller instances of the same problem.

Thus:

public class RecursionP2 {

//constructor

ArrayList<Integer> classList = new ArrayList <Integer> (0);

RecursionP2 (int [] list) {

for(int i = 0; i < list.length; i++) {

classList.add(list [i]);

}

}

public int[] toEvenRevList(int[] list) {

if (list.length == 0) {

i = false;

return new int[0];

}

i = !i;

boolean j = i;

int[] temp = toEvenRevList(Arrays.copyOfRange(list, 1, list.length));

if (j) {

temp = Arrays.copyOf(temp, temp.length + 1);

temp[temp.length - 1] = list[0];

}

return temp;

}

public int[] toEvenRevList() {

return toEvenRevList(ArrayList);

// returns last element

public int retPenultimate(int[] list) {

if (list.length == 1)

return list[0];

return retPenultimate(Arrays.copyOfRange(list, 1, list.length));

}

public int[] getList() {

return ArrayList;

}

On running this, the output below is generated:

RecursionP2(int[] list) {

ArrayList = new int [list.length];

for (int i = 0; i < list.length; i++)

ArrayList[i] = list[i];

}

Consequentially, the reverseList method as public int[] reverseList(int[] list) {

if (list.length == 0)

return new int[0];

int[] value = reverseList(Arrays.copyOfRange(list, 1, list.length));

value = Arrays.copyOf(value, value.length);

value [value.length - 1] = list[0];

return value;

}

User Dennis Pashkov
by
5.0k points