D). Write a function named isPalindrome, that accepts a sentence containing blan
ID: 3842923 • Letter: D
Question
D). Write a function named isPalindrome, that accepts a sentence containing blanks and punctuation and returns true if it’s a palindrome, false otherwise. HINT: reverseStr from P3 has a role to play here.
E). Write a function named factorial() that accepts a positive integer N and returns N!
F) Write a function nChooseK(), that accepts two positive integers, N and K, and returns the number of ways to arrange K items out of a set of N. You should use factorial() in this function.
G) Write a function factorsOfN(), that accepts a positive integer N, and returns an array containing all of its factors.
H) Write a function isPrime(), that accepts a positive integer and returns true if it is a prime, false otherwise. You should use factorsOfN() in this function.
Explanation / Answer
Hi, I have written three functions in C++.
Please repost other questions in separate post.
D). Write a function named isPalindrome, that accepts a sentence containing blanks and punctuation and returns true if it’s a palindrome, false otherwise. HINT: reverseStr from P3 has a role to play here.
bool isPalindrome(string str) {
string reverse = reverseStr(str);
if(str == reverse)
return true;
else
return false;
}
E). Write a function named factorial() that accepts a positive integer N and returns N!
int factorial(int n) {
int fact = 1;
for(int i=2; i<=n; i++)
fact = fact*i;
return fact;
}
F) Write a function nChooseK(), that accepts two positive integers, N and K, and returns the number of ways to arrange K items out of a set of N. You should use factorial() in this function.
int nChooseK(int n, int k) {
int nFact = factorial(n);
int kFact = factorial(k);
int diffFact = factorial(n-k);
return nFact/(kFact*diffFact);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.