Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this c++ assignment. I am supposed to write the recursive versions of these iterative functions. The recursive functions have all been

I need help with this c++ assignment. I am supposed to write the recursive versions of these iterative functions. The recursive functions have all been set to return 0 so it will compile. Thank you.

// CS 250 - Recursion Practice2 #include #include using namespace::std;

struct Node { int data; Node * next; };

void makeLinkedList(Node * &, int data[], int cellsUsed);

void printR(const int data[], int cellsUsed); void printR(const Node * start);

// based on a count int factI(int); int factR(int);

int sumI(const int data[], int cellsUsed); int sumR(const int data[], int cellsUsed);

// Based on a data value in an array int stringLengthI(const char * p); int stringLengthR(const char * p);

// Based on an address in a linked list int linkedListLengthI(const Node * start); int linkedListLengthR(const Node * start);

// array of int with a count int largestI(const int data[], int cellsUsed); int largestR(const int data[], int cellsUsed);

// array of char with a specific last value char largestI(const char * p); char largestR(const char * p);

// linked list of int with a specific last address int largestI(const Node * start); int largestR(const Node * start);

void main() { int info[7] = { 6, 3, 5, 10, 2, 1, 9 }; Node * start; makeLinkedList(start, info,7);

printR(info, 7); cout << endl; printR(start); cout << endl;

cout << factI(6) << endl; cout << factR(6) << endl;

cout << sumI(info, 7) << endl; cout << sumR(info, 7) << endl;

cout << stringLengthI("C++ is a programming language") << endl; cout << stringLengthR("C++ is a programming language") << endl;

cout << linkedListLengthI(start) << endl; cout << linkedListLengthR(start) << endl;

// array of int with a count cout << largestI(info, 7) << endl; cout << largestR(info, 7) << endl;

// array of char with a specific last value cout << largestI("C++ is a programming language") << endl; cout << largestR("C++ is a programming language") << endl;

// linked list of int with a specific last address cout << largestI(start) << endl; cout << largestR(start) << endl; return; }

void printR(const int data[], int cellsUsed) { if (cellsUsed == 0) return; cout << data[0] << " "; printR(data + 1, cellsUsed - 1); }

void printR(const Node * start) { if (start == nullptr) return; cout << start->data << " "; printR(start->next); } void makeLinkedList(Node * & start, int data[], int cellsUsed) { if (cellsUsed == 0) return; Node * p = new Node; p->data = data[0]; p->next = nullptr; start = p; makeLinkedList(start->next, data + 1, cellsUsed - 1); }

// based on a count int factI(int n) { int ans = 1;

for (int i = 1; i <= n; i++) ans *= i;

return ans;

} int factR(int n) { if (n == 0)return 1; else return n * factR(n - 1); } int sumI(const int data[], int cellsUsed) { int ans = 0; for (int i = 0; i < cellsUsed; i++) { ans += data[i]; } return ans; } int sumR(const int data[], int cellsUsed) { return 0; }

// Based on a data value in an array int stringLengthI(const char * p) { int ans = strlen(p); return ans; } int stringLengthR(const char * p) { if (*p == '\0') return 0; else return 1 + stringLengthR(p + 1); }

// Based on an address in a linked list int linkedListLengthI(const Node * start) { int ans = 0; const Node * p = start; while (p != nullptr) { ans++; p = p->next; } return ans; } int linkedListLengthR(const Node * start) { if (start == nullptr) return 0; else return 1 + linkedListLengthR(start->next); }

// array of int with a count int largestI(const int data[], int cellsUsed) { int ans = data[0]; for (int i = 1; i < cellsUsed; i++) { if (ans < data[i]) { ans = data[i]; } } return ans; } int largestR(const int data[], int cellsUsed) { return 0; }

// array of char with a specific last value char largestI(const char * p) { int i = 1; char ans = (*p); char temp = (*p); while(temp != '\0') { if (ans < (*p) + i) { ans = (*p) + i; } i++; temp = temp + i; } return ans; //return '0'; } char largestR(const char * p) { return '0'; }

// linked list of int with a specific last address int largestI(const Node * start)//I haven't quite figured this one out yet either. { int ans = 0; const Node * p = start; const Node * temp = start; while(temp != nullptr) { if (p->data < p->next->data) { ans = p->next->data; } //p = p->next->next; temp = temp->next; } return ans; } int largestR(const Node * start) { return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Principles Programming And Performance

Authors: Patrick O'Neil

1st Edition

1558603921, 978-1558603929

More Books

Students also viewed these Databases questions

Question

=+Trainers from headquarters? Local trainers? Independent trainers?

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago