Question

In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

In C++ please! Please include .cpp and .hpp files! Thank you!

Recursive Functions

Goals

  • Create and use recursive functions

In this lab, we will write a program that uses three recursive functions.

Requirements:

Important: You must use the array for this lab, no vectors allowed.

  1. First Recursive Function

Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character.

Example: Input of “Hello, world” should output “dlrow ,olleH(newline)”.

Note: Your recursive function just needs to print the reverse of the string rather than actually reversing the string itself.

  1. Second Recursive Function

Write a function that recursively calculates the sum of an array of integers. The function has 2 parameters:

  • A pointer to the integer array
  • An integer for the number of elements in the array.

The function must use a recursive call to sum the value of all integers.

  1. Third Recursive Function

Write a function that recursively calculates the triangular number of an integer N. You can set an up-limit for N. You don’t need to handle extra large integers.

The function has one parameter that take in integer N.
Example: If the integer N is 3, the function should output the triangular number 6, since 1 + 2 + 3 = 6.

For more information on triangular number: https://en.wikipedia.org/wiki/Triangular_number (Links to an external site.)

Menu

Your program needs to demonstrate all three functions by providing a menu.

The menu should provide user choices to select one of the three functions to call, after prompting user input for function call and the function outputs results, the menu should go back to the first menu to let the user continue choosing functions to call.

If user choose function #1, the menu should prompt the user to enter a string and your program reversely prints the string

Note: Use getline() in standard library so the input takes space characters.

If user chooses function #2, the menu should first prompt the user an integer input for the number of integers in the array, then a series of integers to fill the array. Afterwards, the program prints sum of the array of integers.

If the user chooses function #3, the menu should prompt the user an integer, then the program prints the triangular number of that number.

In addition to the 3 function options inside the first menu, the menu must also provide the option to quit the program. You can add input validation functions into these the menu to make it robust.

File organization

You can put all your recursive functions in a single .cpp file or separate them into different .cpp files. However, you must separate the implementation and declaration in .cpp and .hpp files.

Note: You do not need to make a class for this lab.

What you need to submit

  • All the program files including header and source files (.cpp/.hpp)
  • Makefile

Important: Put all the files in a single .zip file and submit it on Canvas.

Grading

  • Programming style – 10%
  • Implement the function to reversely print string – 20%
  • Implement the function to calculate the sum of the array – 25%
  • Implement the function to calculate the triangular number – 25%
  • Implement a program with a menu to call each function or to exit – 20%
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Recusrsion.h

#include <iostream>

using namespace std;

void recursiveStringReverse(const string &str);

int recursiveSumOfArrayOfIntegers(int *, int);

int recursiveTriangularNumber(int);

void printMenu();

Recursion.cpp

#include "Recursion.h"

void recursiveStringReverse(const string& str)

{

int numOfChars = str.size();

if(numOfChars == 1)

cout << str << endl;

else

{

cout << str[numOfChars - 1];

recursiveStringReverse(str.substr(0, numOfChars - 1));

}

}

int recursiveSumOfArrayOfIntegers(int *arr, int n)

{

if(n <= 0)

return 0;

return(recursiveSumOfArrayOfIntegers(arr, n - 1) + *(arr + n - 1));

}

int recursiveTriangularNumber(int N)

{

if(N >= 1)

return (N + recursiveTriangularNumber(N - 1));

else

return 0;

}

void printMenu()

{

cout << "1. Reverse a string\n2. Sum of an array of integers\n3. Calculate the triangular number of N\n4. Exit\nEnter your choice: ";

}

main.cpp

#include "Recursion.h"

int main()

{

cout << "\n==================================================";

cout << "\n* Welcome to the program of Recursion *";

cout << "\n==================================================" << endl;

int choice = 0;

do

{

printMenu();

cin >> choice;

cin.ignore();

switch(choice)

{

case 1:

{

string str;

cout << "Input a string to reverse: ";

getline(cin, str);

cout << "Reversed string: ";

recursiveStringReverse(str);

cout << endl;

break;

}

case 2:

{

int N = 0;

cout << "Enter the number of integers in the array: ";

cin >> N;

int *const arr = new int[N];

cout << "Enter the numbers: " << endl;

for(int i = 0; i < N; i++)

{

cin >> *(arr + i);

}

cout << "The sum of all integers in the array: " << recursiveSumOfArrayOfIntegers(arr, N) << endl << endl;

delete [] arr;

break;

}

case 3:

{

int N;

cout << "Enter N for calculating the triangular number: ";

cin >> N;

cout << "The triangular number for N = " << N << " is: " << recursiveTriangularNumber(N) << endl << endl;

break;

}

case 4:

{

cout << endl << "Thank you\nGood Bye!" << endl << endl;

exit(1);

}

default:

cout << endl << "Invalid choice" << endl << endl;

}

}while(choice != 4);

return 0;

}

******************************************************************* SCREENSHOT ********************************************************

Add a comment
Know the answer?
Add Answer to:
In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • **IN C*** * In this lab, you will write a program with three recursive functions you...

    **IN C*** * In this lab, you will write a program with three recursive functions you will call in your main. For the purposes of this lab, keep all functions in a single source file: main.c Here are the three functions you will write. For each function, the output example is for this array: int array[ ] = { 35, 25, 20, 15, 10 }; • Function 1: This function is named printReverse(). It takes in an array of integers...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h...

    Files given in this assignment (right-click on the file to download) Assignment7.cpp (need to complete) Student.h (Given. Just use it, don't change it!) Student.cpp (Partially filled, need to complete) 1. Assignment description In this assignment, you will write a simple class roster management system for ASU CSE100. Step #1: First, you will need to finish the design of class Student. See the following UML diagram for Student class, the relevant header file (class declaration) is given to you as Student.h,...

  • Java Program Create a class to store an array of with enough space to store 10 integer values. Us...

    Java Program Create a class to store an array of with enough space to store 10 integer values. Using the principle of recursion, implement the following: *getSize : returns the size of the array. *get (i): returns the i-th element of the array. If the element does not exist, it throws a "NoSuchElementException” which is a subclass of Java class RunTimeException. *add (val): inserts value as the last element of the array. If necessary, double the size of the current...

  • Goals: Practicing functions and parameter types. Problem: You are to create a program that will aid...

    Goals: Practicing functions and parameter types. Problem: You are to create a program that will aid in a farmer in monitoring the amount of corn to feed livestock each week. There is a template named "Labs1-5template.cpp" posted on Canvas which you should download and complete. Follow the instructions given in the template and follow the assignment guidelines for the course. Do not use global variables and do not use the same variable name in multiple functions. Your finished code should...

  • Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only....

    Please i need the .H, .cpp and main.cpp files. Please use vector. its for my midterm so correct code that can run only. Thank you. Pointers and Dynamic Memory – Chapter 11 1. Write code using pointers and the Accounts class from week 1 assignment as follows – create a program in which you a. Create 10 accounts using an array with id’s 0 to 9 and refer to each account using pointer notation b. Add an initial balance of...

  • Programming Fundamental (C++) Lab C++ Lab Ra'fat Amareh Faculty of Engineering and Information Technology Assignment Write...

    Programming Fundamental (C++) Lab C++ Lab Ra'fat Amareh Faculty of Engineering and Information Technology Assignment Write a C++ program that performs the following: Prints on screen a selection menu as follow: a. Sin (x), Series b. Sum odd digit, Print Digits, Print Shape c. Array d. Exit If user presses a or A, the following sub menu should be displayed 1- Sin (x) (Program should solve the following series x - x'/3! + x®/5! – x/7...x"m!) 2- F (Program should...

  • In C only Please! This lab is to write a program that will sort an array...

    In C only Please! This lab is to write a program that will sort an array of structs. Use the functions.h header file with your program. Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like though it is recommended that you use bubble sort, insertion sort, or selection sort...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT