TITLE
RECURSIVE FUNCTIONS - THREE SMALL PROGRAMS in C++
INTRODUCTION
Recursion is a programming technique in which a function calls itself. It is another way of implementing repetitive computations, which we have previously associated with loops. When called to solve a problem, a recursive function passes a smaller version (or versions) of the problem to another instance (or instances) of itself, then uses the results returned by the recursive call or calls to build or complete a solution to the original problem.
DESCRIPTION
Design, implement, and document recursive functions to solve the three problems described below; test those functions by calling them from appropriate main functions; and document the resulting programs and their functions. Note that you will write three small programs.
Keep in mind the questions that arise in designing recursive algorithms:
EXPONENTIATION
Write and exercise a recursive function exp(x,n) that returns the value of the double x raised to the non-negative integer power n. A run of a program that exercises this function might look like this:
Enter a real value: 2.4
Enter an integer: 3
2.4^3 = 13.824
Remember that any non-zero value raised to the power 0 is 1.
DETERMINE IF A VALUE IS PRESENT IN AN ARRAY
Implement and test a recursive function present(a,n,k) that returns TRUE if the value k is present in the array a[0..n-1], FALSE if it is not. Read values from a file into the array. Read values to test from the terminal. Terminate the program with an input of 0.
If the values read into the array are [14, 66, 34, 51, 44, 16, 29, 33], then a run of the program might look like this:
Data file name -> vals.dat
Test value -> 51
Present
Test value -> 63
Not present
Test value -> 29
Present
Test value -> 0
IDENTIFY PALINDROMES
As we saw in Project 1, a palindrome is a string in which the letters read the same in both directions, disregarding non-letters and capitalization. For example, "Eva, can I see bees in a cave?" is this kind of palindrome.
This time, solve the problem of the previous project with a recursive function, say pal(a,low,high). This function will return a boolean value. Note that you can re-use the rest of your program from the first project.
HINT: Count the letters as they are read in.
HINT: Use an array of characters rather than the string type.
Hi,
Note: I've written all functions in one program. U can copy the code separately for each functionality, there is no dependency of variable.
exp(double x,unsigned int n) : recursively calculates the x^n.
1. n is even then x^(n/2) * x^(n/2) will be calculated
2. n is odd the x * x^(n/2) * x^(n/2) will be calculated.
int findElement(int arr[] , int start , int end , int value): Recursively check each index for value in arr.
bool pal(char arr[] , int start , int end) : stored all characters from given input(ignored spaces . , etc). Recursively compared arr[start] == arr[end] if matching called pal function otherwise returned false.
C++ code:
//Program starts here
#include <iostream>
#include<bits/stdc++.h>
#include <fstream>
#include<stdio.h>
#include <stdlib.h>
using namespace std;
double exp(double x,unsigned int n){
if (n == 0)
return 1;
else if (n % 2 == 0) //if n is even number
return exp(x, n / 2) * exp(x, n / 2);
else
return x * exp(x, n / 2) * exp(x, n / 2); //if n is odd
number
}
int findElement(int arr[] , int start , int end , int
value){
if(start == end)//end of array, no element
found
return 0;
if(arr[start] == value)//if value equals to current
index(start) value and given value
return 1;
//else search from the next index.
return findElement(arr, start+1 , end , value);//start
incremented
}
bool pal(char arr[] , int start , int end){
if(start == end){//incase of odd size
return true;
}
else if (start > end)//both indexes crosses each other,
in case of even size
return true;
if(arr[start] != arr[end])
return false;
return pal(arr,start+1 , end-1);//end and start index
charatcers compared.
}
int main() {
//Power of x^n
double x ;
unsigned int n;//n is non-negative number.
cout << "Enter x:";
cin >> x;
cout << "Enter n:";
cin >> n;
cout << x << "^" << n<<" = "
<<exp(x,n) << endl;//recursive
function
//array element search
cout << "enter file name:";
string s;
cin >> s;
ifstream file(s.c_str());//ifstream constructor accepts a
const char* as the filename, otherwise we can use ifstream
file(s)
string line;
getline(file,line);//Input give as one line
only
stringstream ss(line);
int num=0;
int arr[100];
int i = 0;
while(ss >> num){//reading one by one number using
string stream.
arr[i] = num;
cout << arr[i] << " ";
i++;
}
cout << endl;
int test;
while(1){
cout << "Test value --> ";
cin >> test;
if(test == 0)//test value = 0, terminates
program
break;
if(findElement(arr,0 , i , test))
cout << "Present" << endl;
else
cout << "Not present" << endl;
}
//Palindrome.
string word;
cin.ignore();//to ignore new line
getline(cin,word);//will read the line from
input
// cout << word;
char characters[1000];//Only storing characters from words
ignoring . , space etc...
int j = 0;//for characters count
for(int i = 0; i < word.length(); i++){
if((word[i] >= 'A' && word[i] <= 'Z') ||(word[i]
>= 'a' && word[i] <= 'z'))
characters[j++] = tolower(word[i]);//storing only
characters and all characters converted to lower
case.
}
if(pal(characters , 0 , j-1)){
cout << "palindrome" << endl;
}
else{
cout << "Not a palindrome" << endl;
}
}
//End of the program




TITLE RECURSIVE FUNCTIONS - THREE SMALL PROGRAMS in C++ INTRODUCTION Recursion is a programming technique in...