Question

In: Computer Science

In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In...

In C++, please check for memory leaks

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.

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.

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.

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.)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.

Solutions

Expert Solution

#include <iostream.h>

#include<stdio.h>

#include <iomanip.h>

#include<stdlib.h>

class str

{

public:

void rev(char []);

int sum(int *,int);

void tri(int);

};

void str::rev(char *s) // recursive function for reverse of a string

{

if (*s)

{

rev(s+1);

cout<<*s;

}

}

int str::sum(int *arr,int len) // recursive function for sum of array elements

{

if(len<=0)

return 0;

return(sum(arr,len-1)+arr[len-1]);

}

void str :: tri(int n) // recursive function for printing triangular elements

{

static int i=1,z;

if(i>n)

goto p;

else

{

cout<<endl<<i*(i+1)/2;

i++;

tri(n);

}

p:

i=1;

}

void main()

{

char s[100];

int *a,size;

int i,no,opt;

str obj;

while(1)

{

cout<<endl<<"WHICH OPERATION DO YOU WANT";

cout<<endl<<"1. STRING REVERSE 2. SUM OF ARRAY ELEMENTS 3. TRIANGULAR NUMBERS 0. EXIT";

cin>>opt;

if(opt==1)

{

cout<<endl<<"enter a string";

gets(s);

obj.rev(s);

}

else

if(opt==2)

{

cout<<endl<<"enter the size of array";

cin>>size;

a=new int[size];

for(i=0;i<size;i++)

{

cout<<endl<<"enter a number";

cin>>*(a+i);

}

cout<<obj.sum(a,size);

}

else

if(opt==3)

{

cout<<endl<<"enter a number";

cin>>no;

obj.tri(no);

}

else

if(opt==0)

break;

else

cout<<endl<<"invalid choice...";

}

}

output


Related Solutions

C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and...
C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and no vectors. Thank you Write a program that solves the knapsack problem recursively for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. Hint: The arguments to the knapsack function are target weight and the array index where the remaining items start. The knapsack problem in its simplest form involves trying to fit items of different weights...
Please C++ create a program that will do one of two functions using a menu, like...
Please C++ create a program that will do one of two functions using a menu, like so: 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 1 Enter Catalan number to calculate: 3 Catalan number at 3 is 5 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 2 Enter Fibonacci number to calculate: 6 Fibonacci number 6 is 8 Create a function of catalan that will take a parameter and return...
with C# create a console application project that outputs the number of bytes in memory that...
with C# create a console application project that outputs the number of bytes in memory that each of the following number types use, and the minimum and maximum values they can have: sbyte, byte, short, ushort, int, uint, long, ulong, float, double, and decimal. Try formatting the values into a nice-looking table! More Information: You can always read the documentation, available at https://docs.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting for Composite Formatting to learn how to align text in a console application. Your output should look...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory...
How to convert Sudo Code to C language? keyboard_process() {//insert a string. //check for shared memory If(shared memory not full) { //sendthe string to the shared memory //send signal to process; }
C# Create a console application project that outputs the number of bytes in memory that each...
C# Create a console application project that outputs the number of bytes in memory that each of the following number types use, and the minimum and maximum values they can have: sbyte, byte, short, ushort, int, uint, long, ulong, float, double, and decimal. Try formatting the values into a nice-looking table! Your output should look something like this: | Type | Bytes of Memory | Min | Max |
Research Meaningful Use and the requirements for this national IT initiative. Create goals and a training...
Research Meaningful Use and the requirements for this national IT initiative. Create goals and a training plan and schedule for a Meaningful Use compliance training program for a large multi-physician clinic. Create the training outline to include main topics and appropriate subtopics.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT