In: Computer Science
IN C++, ONE FILE
Also include the main class that just runs these functions, thanks.
Write a recursive function to produce a pattern of n lines of asterisks. The first line contains one asterisk, the next line contains two, and so on, up to the nth line, which contains n asterisks. For example, if the non-negative integer is 5, the pattern generated is:
* ** *** **** *****
Prototype:
void pattern(unsigned n);
Write a recursive function, sum_range that finds the sum of the integers in the range [first,last]. For example, sum_range(0, 5) returns 15, while sum_range(4, 5) returns 9. Prototype:
long sum_range(long first, long last);
Write a recursive function called power that computes powers of
the form baseexp, where base is any double number and
exp is an integer. So, power(3.0, 2) is 3.02 (which is
9.0), and power(4.2, 3) is 4.23 (which is 74.088). For
any non-zero value of base, the value of base0 is
defined to be 1, so for example power(9.1, 0) is 1. For a negative
exponent, -exp, the value returned is defined by
base-exp = 1/baseexp
For example, power(3.0, -2) is 1/3.02 (which is 1/9).
Taking 0exp when exp is not positive is undefined.
Prototype:
double power(double base, int exp);
Write a recursive function that reverses the order of the elements of an array of type T in the range [first, last). For example, the array { 1, 2, 3 } once reversed becomes { 3, 2, 1 }.
void reverse(T* first, T* last);
Write a recursive function to decide whether it is
possible to win the Gummy Bear Game.
Here are the rules. At any point in the game, you can give back
some of the gummy bears, but you must follow these rules where
n is the number of gummy bears that you have:
The goal of the game is end up with exactly 42 gummy bears.
For example, suppose that you start with 250 gummy bears. Then you could make the following moves:
Your function decides whether it is possible to win the Gummy Bear Game if you start with n gummy bears. You do not have to figure out the exact sequence of decisions; just determine if it is possible or not.
Prototype:
bool gummybears(unsigned n);
Examples:
NOTE;If you are stuck somewhere feel free to ask in the comments.........i will try to help you out but do not give negative rating to the question.....as it affects my answering rights.......
All the code is working perfectly...just copy paste to your online compiler and see the working
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void helperPat(int i,unsigned int n){
if(i>n) return ;
for(int j=0;j<i;j++){
cout<<"*";
}
cout<<endl;
helperPat(i+1,n);
}
void pattern(unsigned int n){
helperPat(1,n);
}
long sum_range(long first,long last){
if(first == last ) return last;
long sum=0;
sum+=first+sum_range(first+1,last);
return sum;
}
double power(double base, int exponent){
if(exponent == 0)
return 1;
else{
double r = power(base, exponent/2);
if(exponent % 2 < 0)
return r * r / base;
else if(exponent % 2 > 0)
return r * r * base;
else
return r * r;
}
}
void revereseArray(int arr[], int start, int end)
{
if (start >= end)
return;
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
revereseArray(arr, start + 1, end - 1);
}
bool gummybears(unsigned int n){
if(n == 42) return true;
if(n < 42) return false;
bool fv=false,ftr=false,ev=false;
if(n%5 == 0) fv=fv||gummybears(n-42);
if(n%3 == 0 or n%4 == 0) {
int l=n%10,s;
if(n<100) s=(n/10);
else s=((n%100)/10);
if(l != 0 and s !=0)
ftr=ftr || gummybears(n-(l*s));
}
if(n%2 == 0) ev=ev|| gummybears(n/2);
return fv||ftr||ev;
}
int main() {
cout<<"--------pattern--------"<<endl;
pattern(5);
cout<<"----------------------"<<endl;
cout<<"Sum of Range (0,5) is: "<<sum_range(0,5)<<endl;
cout<<"----------------------"<<endl;
cout<<"power(3.0,-2) gives: "<<power(3.0,-2)<<endl;
cout<<"----------------------"<<endl;
int arr[] = {1, 2, 3, 4, 5, 6};
revereseArray(arr, 0, 5);
cout<<"Array {1,2,3,4,5,6} after reversal gives;"<<endl;
for(int i:arr)
cout<<i<<" ";
cout<<endl<<"-------------------------"<<endl;
cout<<"gummybears(53) : "<<gummybears(53)<<endl;
cout<<"gummybears(250) : "<<gummybears(250)<<endl;
cout<<"gummybears(42) : "<<gummybears(42)<<endl;
cout<<"gummybears(41) : "<<gummybears(41)<<endl;
return 0;
}
Output:-->