My Programming Lab - Ch19

Assume the availability of a function called printStars . The function receives an int argument . If the argument is positive, the function prints (to standard output ) the given number of asterisks. Thus, if the printStars(8) is called, ******** (8 aster

if (starCount > 0){
cout << "*";
printStars(starCount-1);
}

Write a function called printStars . The function receives an int parameter . If the parameter is positive, the function prints (to standard output ) the given number of asterisks; otherwise the function does nothing. The function does not return a value

void printStars(int star)
{
if(star > 0){
cout << "*";
printStars(star-1);
}
}

Write a function called fact that recursively calculates the factorial value of its single int parameter . The value returned by fact is a long

long fact(int n) {
if (n <= 1) {
return 1;
} else {
return (fact(n-1) * (long) n);
}
}

Two non-negative integers x and y are equal if either:
Both are 0, or
x-1 and y-1 are equal
Write a bool -function named equals that recursively determines whether its two int parameters are equal and returns true if they are and false otherwise.

bool equals (int x, int y)
{
if( (x<0) || (y<0) )
return false;
if( (x==0) && (y==0) )
return true;
return equals(x-1, y-1);
}

Given non-negative integers x and n, x taken to the nth power can be defined as:
x to the 0th power is 1
x to the nth power can be obtained by multiplying x to the n-1'th power with x
Write a long -valued function named power that accepts an two int param

long power(int x, int n)
{
if(n==0)
return 1;
else{
return x* power(x, n-1);
}
}

The Fibinacci series: 0, 1, 1, 2, 3, 5, 8, 13, 21, has as its first 2 values , 0 and 1; each successive value if then calculated as the sum of the previous two values .
The first element in the series is the 0'th element , thus the value 8 is element 6 of

int fib(int n)
{
int sum;
if(n==1)
return n;
else if (n==0)
return n;
else
return fib (n-1) + fib(n-2);
}

Write a recursive, bool-valued function, containsVowel, that accepts a string and returns true if the string contains a vowel.
A string contains a vowel if:
The first character of the string is a vowel, or
The rest of the string (beyond the first characte

bool containsVowel (string s) {
bool hasVowel=false;
if (s.length()==0) return false;
else if (s[0]=='a'||
s[0]=='e'||
s[0]=='u'||
s[0]=='o'||
s[0]=='i'||
s[0]=='A'||
s[0]=='E'||
s[0]=='U'||
s[0]=='O'||
s[0]=='I')
hasVowel=true;
else
hasVowel=containsVowe

A palindrome is a string that reads the same forwards or backwards; for example dad, mom, deed (i.e., reversing a palindrome produces the same string ). Write a recursive, bool-valued function, isPalindrome that accepts a string and returns whether the st

bool isPalindrome(string s)
{
if (s.length() < 2)
return true;
else if (s[0] == s[s.length() - 1])
return isPalindrome(s.substr(1,s.length()-2));
return false;
}

Write a recursive, string -valued function, reverse, that accepts a string and returns a new string consisting of the original string in reverse. For example, calling reverse with the string goodbye returns the string eybdoog.
Reversing a string involves:

string reverse(string word){
string temp = "";
if(word.length() > 1){
temp = word.substr(word.length()-1, word.length());
temp += reverse(word.substr(0, word.length()-1));
}
else
return word;
return temp;
}

The sum of the elements of an integer -valued array recursively calculated as follows:
The sum of an array of size 0 is 0;
Otherwise, the sum is the value of the first element added to the sum of the rest of the arrayset the first element of the array to

int sum( int arr[], int n ) {
if (n < 0)
return 0;
else
return arr[n] + sum(arr, n-1);
}

The maximum-valued element of an integer -valued array can be recursively calculated as follows:
If the array has a single element , that is its maximum (note that a zero-sized array has no maximum)
Otherwise, compare the first element with the maximum of

int max(const int arr[], int n)
{
if (n == 1)
return arr[0];
else
{
if (max(arr, n-1) > arr[n-1])
return max(arr, n-1);
else
return arr[n-1];
}
}

The nth harmonic number is defined non-recursively as: 1 +1/2 + 1/3 + 1/4 + ... + 1/n. Come up with a recursive definition and use it to guide you to write a function definition for a double -valued function named harmonic that accepts an int parameters n

double harmonic(int n){
if(n==0)
return 0.0;
else
return 1.0/ n + harmonic(n-1);
}

Assume the availability of a function named printStars that can be passed a non-negative integer n and print a line of asterisks. Write a function named printTriangle that receives a non-negative integer n and prints a triangle of asterisks as follows: fi

void printTriangle(int n){
if(n <= 0)
return;
printStars(n);
cout<<endl;
printTriangle(n-1);
}

Assume the availability of a function named printStars that can be passed a non-negative integer n and print a line of asterisks. Write a function named printTriangle that receives a non-negative integer n and prints a triangle of asterisks as follows: fi

void printTriangle (int n)
{
if(n==0)
return;
printTriangle(n-1);
printStars(n);
cout << endl;
}

Write the definition of a function named count that reads all the strings remaining to be read in standard input and returns their count (that is, how many there are) So if the input was:
hooligan sausage economy
ruin palatial
the function would return 5

int count()
{
string q = "";
cin >> q;
int word_count = 0;
if(q != "")
word_count += 1 + count();
else
word_count = 0;
return word_count;
}

Write the definition of a function called product . The function receives two int parameters . You may assume that neither parameter is negative. The function returns the product of the parameters . So, product (3,5) returns 15 and product (30,4) returns

int product(int x, int y)
{
return (x*y);
}