In: Computer Science
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.
#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