Recursive Functions Goals Create and use recursive functions Write a C++ program
ID: 3599907 • Letter: R
Question
Recursive Functions
Goals
Create and use recursive functions
Write a C++ program that uses three recursive functions.
Requirements:
Important: You must use an array. Please DO NOT use vectors.
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 need 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 calculate the triangular number of an integer N.
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 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 choose 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 choose 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 option to quit the program. You can add input validation functions into these the menu to make it robust.
Explanation / Answer
#define _CRT_SECURE_NO_DEPRECATE
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
void print_reverse(char *str);
int sum_integers(int *ptr, int size);
int calculate_triangular_number(int n);
int main()
{
int choice;
do
{
printf(" ########################Menu############################################## ");
printf("1.Print a string in reverse 2.Calculate sum of integers in an array 3.calculate the triangular number of an integer N. 4.Quit.. ");
printf("Enter you choice afrom 1-4: ");
fflush(stdin);
scanf("%d", &choice);
switch (choice)
{
case 1:
char str[20];
printf("Enter the string to print in reverse: ");
fflush(stdin);
gets(str);
print_reverse(str);
break;
case 2:
int n,i;
int *arr,sum;
do
{
printf("Enter number of elements in an array: ");
scanf("%d", &n);
if (n < 0)
{
printf("Number is negetive , pls eneter positive number ");
}
} while (n < 0);
//allocate memory for array
arr = (int*)malloc(n*sizeof(int));
for (i = 0; i < n; i++)
{
printf("Enter arr[%d] =", i);
scanf("%d", &arr[i]);
}
sum = sum_integers(arr, n);
printf("Sum of integers in an array = %d ", sum);
break;
case 3:
int number;
do
{
printf("Enter the triangular number: ");
fflush(stdin);
scanf("%d", &number);
if (number < 0)
{
printf("Enter positive number ");
}
} while (number < 0);
printf(" triangular number of an integer %d =%d ", number, calculate_triangular_number(number));
break;
}
} while (choice != 4);
}
void print_reverse(char *str)
{
if (*str!='')
{
print_reverse(str + 1);
printf("%c", *str);
}
}
int sum_integers(int *ptr, int size)
{
if (size <= 0)
return 0;
return (sum_integers(ptr, size - 1) + ptr[size - 1]);
}
int calculate_triangular_number(int n)
{
if (n < 1)
return 0;
return calculate_triangular_number(n-1) + n;
}
--------------------------------------------------------------------------------------
//output
########################Menu##############################################
1.Print a string in reverse
2.Calculate sum of integers in an array
3.calculate the triangular number of an integer N.
4.Quit..
Enter you choice afrom 1-4: 1
Enter the string to print in reverse: Hello, World
dlroW ,olleH
########################Menu##############################################
1.Print a string in reverse
2.Calculate sum of integers in an array
3.calculate the triangular number of an integer N.
4.Quit..
Enter you choice afrom 1-4: 2
Enter number of elements in an array: -2
Number is negetive , pls eneter positive number
Enter number of elements in an array: 4
Enter arr[0] =1
Enter arr[1] =2
Enter arr[2] =3
Enter arr[3] =4
Sum of integers in an array = 10
########################Menu##############################################
1.Print a string in reverse
2.Calculate sum of integers in an array
3.calculate the triangular number of an integer N.
4.Quit..
Enter you choice afrom 1-4: 3
Enter the triangular number: -2
Enter positive number
Enter the triangular number: 3
triangular number of an integer 3 =6
########################Menu##############################################
1.Print a string in reverse
2.Calculate sum of integers in an array
3.calculate the triangular number of an integer N.
4.Quit..
Enter you choice afrom 1-4: 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.