3. Provide a non-recursive implementation of the following power function double
ID: 3586656 • Letter: 3
Question
3. Provide a non-recursive implementation of the following power function double power (double x, unsigned n) {if(n== 0 ) return 1.0; else return x* power (x, n-1); J 4. Implement the Palindrome checker algorithm below and run the following tests on your main program using assert: 1234321 racecar Zavala YOURNAME true true false false ALGORITHM If input-word length 1 return true Else compare first and last characters D D if they are not equal, return false If they are equal, apply algorithm recursively to input_word -first and last charactersExplanation / Answer
#include<iostream>
using namespace std;
double power(double x, unsigned n)
{
// resultant
double ans=1;
// looping for n times
for(n;n!=0;n--)
{
// each time multiplying it by x
ans = ans*x;
}
// returning answer
return ans;
}
main()
{
// sample runs
cout << power(3, 4) << endl;
cout << power(2, 5) << endl;
}
/*SAMPLE OUTPUT
81
32
*/
#include<iostream>
using namespace std;
bool ispal(string a, int i, int j)
{
//Base case: string is of odd lenght. Hence center character is one.
if(i==j)
return true;
//As long as i and j aren't equal and i<j
if(i!=j && i<j){
// if the first and last characters are matching
if(a[i]==a[j])
{
// returning recursive function
return ispal(a,i+1,j-1);
}
else
{
// not matched
return false;
}
}
// looped through all and didn't find any mismatch.
return true;
}
main()
{
// sample runs;
string b = "12345321";
cout << boolalpha << ispal(b, 0, b.length()-1) << endl;
b = "racecar";
cout << ispal(b, 0, b.length()-1) << endl;
b = "Zavala";
cout << ispal(b, 0, b.length()-1) << endl;
b = "Chegg";
cout << ispal(b, 0, b.length()-1) << endl;
}
/*
false
true
false
false
*/
#include<iostream>
using namespace std;
double power(double x, unsigned n)
{
// resultant
double ans=1;
// looping for n times
for(n;n!=0;n--)
{
// each time multiplying it by x
ans = ans*x;
}
// returning answer
return ans;
}
main()
{
// sample runs
cout << power(3, 4) << endl;
cout << power(2, 5) << endl;
}
/*SAMPLE OUTPUT
81
32
*/
#include<iostream>
using namespace std;
bool ispal(string a, int i, int j)
{
//Base case: string is of odd lenght. Hence center character is one.
if(i==j)
return true;
//As long as i and j aren't equal and i<j
if(i!=j && i<j){
// if the first and last characters are matching
if(a[i]==a[j])
{
// returning recursive function
return ispal(a,i+1,j-1);
}
else
{
// not matched
return false;
}
}
// looped through all and didn't find any mismatch.
return true;
}
main()
{
// sample runs;
string b = "12345321";
cout << boolalpha << ispal(b, 0, b.length()-1) << endl;
b = "racecar";
cout << ispal(b, 0, b.length()-1) << endl;
b = "Zavala";
cout << ispal(b, 0, b.length()-1) << endl;
b = "Chegg";
cout << ispal(b, 0, b.length()-1) << endl;
}
/*
false
true
false
false
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.