Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help completing my program. Write a recursive function that displays the

ID: 3911335 • Letter: I

Question

I need help completing my program.

Write a recursive function that displays the contents of an array in reverse order (from the bottom

up).  

The function declaration may look something like this:

void displayReverse(char list[], int size, int startingIndex);

The parameters are described as follows:

list: The array to be displayed.

size: The number of elements in the array.

startingIndex: The element of the array currently being examined by the algorithm.

The call to the function from main should look something like this, assuming that your array is called list and it contains 5 elements.

displayReverse(list, 5, 0);

#pragma once

class Node

{

private:

char data;

Node *next;

public:

Node()

{

data = ' ';

next = 0;

}

Node(char c)

{

data = c;

next = 0;

}

char getData() { return data; }

Node* getNext() { return next; }

void setNext(Node *n) { next = n; }

};

*********************************************************************************

#pragma once

#include "Node.h"

class LinkedList

{

private:

Node *first;

void displayReversed(Node *);

public:

LinkedList();

bool isEmpty();

void addFront(char c);

void addEnd(char c);

char removeFront();

char removeEnd();

Node* find(char c);

void display();

void displayReversed();

};

**********************************************************************************

#include "LinkedList.h"

#include <iostream>

// Create a new list with dummy header

LinkedList::LinkedList()

{

first = new Node();

}

bool LinkedList::isEmpty()

{

return first->getNext() == 0;

}

// Add to beginning of list

void LinkedList::addFront(char c)

{

// Create a new node.

Node *newNode = new Node(c);

// Attach new node to list

newNode->setNext(first->getNext());

first->setNext(newNode);

}

// Add to end of list

void LinkedList::addEnd(char c)

{

// Declare a traversal pointer and start it at the beginning

Node *temp = first;

// Find the end of the list

while (temp->getNext() != 0)

temp = temp->getNext();

Node *newNode = new Node(c); // Create a new node.

temp->setNext(newNode); // Attach new node to end of list

}

// Remove from beginning of list

char LinkedList::removeFront()

{

// Point a temp pointer to the first node.

Node *temp = first->getNext();

// Reassign dummy node's link to the following node.

first->setNext(temp->getNext());

char rc = temp->getData();

// Delete the temp pointer.

delete temp;

return rc;

}

// Remove from end of list

char LinkedList::removeEnd()

{

// Point trailer pointer to dummy

Node* trailer = first;

// Point temp pointer to trailer's next

Node* temp = trailer->getNext();

// Traverse list until temp is on last node

while (temp->getNext() != 0)

{

trailer = temp;

temp = temp->getNext();

}

// Set trailer's next to null

trailer->setNext(0);

char rc = temp->getData();

// Delete temp

delete temp;

return rc;

}

// Find item in list

Node* LinkedList::find(char c)

{

// Point temp pointer to dummy's next

Node* temp = first->getNext();

// while temp hasn't reached the end of the list

while (temp != nullptr)

{

// if temp's data is the value we're looking for

if (temp->getData() == c)

return temp;

temp = temp->getNext();

}

return 0;

}

void LinkedList::display()

{

Node *temp = first->getNext();

while (temp != 0)

{

std::cout << temp->getData() << " ";

temp = temp->getNext();

}

std::cout << std::endl;

}

void LinkedList::displayReversed()

{

displayReversed(first->getNext());

}

void LinkedList::displayReversed(Node *node)

{

if (node->getNext() != 0)

displayReversed(node->getNext());

std::cout << node->getData() << ' ';

}

*************************************************************************************

#include <iostream>

#include <cmath>

#include "LinkedList.h"

using namespace std;

long factorial(long n)

{

// Base case

if (n <= 1)

return 1;

// Recursive step

return n * factorial(n - 1);

}

long fibonacci(long n)

{

// Base case

if (n == 0 || n == 1)

return n;

// Recursive step

return fibonacci(n - 1) + fibonacci(n - 2);

}

void writeVertical(int n)

{

if (n < 0)

{

cout << '-' << endl;

writeVertical(abs(n));

}

else if (n < 10)

cout << n << endl;

else

{

writeVertical(n / 10);

cout << (n % 10) << endl;

}

}

double exponent(double x, int n)

{

if (n == 0)

return 1;

if (x == 0)

return 0;

if (n > 0)

return x * exponent(x, n - 1);

else

return 1 / exponent(x, -n);

}

void main()

{

cout << "Factorial Test" << endl << endl;

for (long i = 0; i < 10; i++)

cout << i << "! = " << factorial(i) << endl;

cout << " Fibonacci Test" << endl << endl;

for (long i = 0; i < 20; i++)

cout << "f(" << i << ") = "

<< fibonacci(i) << endl;

cout << " writeVertical Test" << endl;

writeVertical(6);

cout << endl;

writeVertical(-12);

cout << endl;

writeVertical(27);

cout << endl;

writeVertical(256);

cout << endl;

cout << " Exponent Test" << endl;

cout << "0^2 = " << exponent(0, 2) << endl;

cout << "5^0 = " << exponent(5, 0) << endl;

cout << "3^4 = " << exponent(3, 4) << endl;

cout << "6^-2 = " << exponent(6, -2) << endl;

cout << " Reverse Display Test" << endl;

LinkedList list;

list.addEnd('A');

list.addEnd('B');

list.addEnd('C');

list.addEnd('D');

cout << " display() ";

list.display();

cout << " displayReversed() ";

list.displayReversed();

cout << endl;

system("pause");

}

Explanation / Answer


Given below is the updated code for the question.
Please do rate the answer if it was helpful. Thank you


main.cpp
==========
#include <iostream>
#include <cmath>
#include "LinkedList.h"
using namespace std;
long factorial(long n)
{

// Base case

if (n <= 1)

return 1;

// Recursive step

return n * factorial(n - 1);

}

long fibonacci(long n)

{

// Base case

if (n == 0 || n == 1)

return n;

// Recursive step

return fibonacci(n - 1) + fibonacci(n - 2);

}

void writeVertical(int n)

{

if (n < 0)

{

cout << '-' << endl;

writeVertical(abs(n));

}

else if (n < 10)

cout << n << endl;

else

{

writeVertical(n / 10);

cout << (n % 10) << endl;

}

}

double exponent(double x, int n)

{

if (n == 0)

return 1;

if (x == 0)

return 0;

if (n > 0)

return x * exponent(x, n - 1);

else

return 1 / exponent(x, -n);

}

void displayReverse(char list[], int size, int startingIndex)
{
if(startingIndex >= size) //base case
return;
displayReverse(list, size, startingIndex+1);
cout << list[startingIndex] << " ";
}


void main()

{

cout << "Factorial Test" << endl << endl;

for (long i = 0; i < 10; i++)

cout << i << "! = " << factorial(i) << endl;

cout << " Fibonacci Test" << endl << endl;

for (long i = 0; i < 20; i++)

cout << "f(" << i << ") = "

<< fibonacci(i) << endl;

cout << " writeVertical Test" << endl;

writeVertical(6);

cout << endl;

writeVertical(-12);

cout << endl;

writeVertical(27);

cout << endl;

writeVertical(256);

cout << endl;

cout << " Exponent Test" << endl;

cout << "0^2 = " << exponent(0, 2) << endl;

cout << "5^0 = " << exponent(5, 0) << endl;

cout << "3^4 = " << exponent(3, 4) << endl;

cout << "6^-2 = " << exponent(6, -2) << endl;

cout << " Reverse Display Test" << endl;

LinkedList list;

list.addEnd('A');

list.addEnd('B');

list.addEnd('C');

list.addEnd('D');

cout << " display() ";

list.display();

cout << " displayReversed() ";

list.displayReversed();

cout << endl;

cout << "Display array in reversed order" << endl;
char arr[5] = {'a', 'b', 'c', 'd', 'e'};
displayReverse(arr, 5, 0);
cout << endl;
system("pause");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote