Question

In: Computer Science

IN C++, ONE FILE Also include the main class that just runs these functions, thanks. Write...

IN C++, ONE FILE

Also include the main class that just runs these functions, thanks.

  1. 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);
  2. 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);
  3. 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);
  4. 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);
  5. 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:

  • If n is even, then you may give back exactly n/2 gummy bears.
  • If n is divisible by 3 or 4, then you may multiply the last two digits of n and give back this many gummy bears.
  • If n is divisible by 5, then you may give back exactly 42 gummy bears.

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:

  • Start with 250 gummy bears.
  • Since 250 is divisible by 5, you may return 42 of the gummy bears, leaving you with 208.
  • Since 208 is even, you may return half the gummy bears, leaving you with 104.
  • Since 104 is even, you may return half the gummy bears, leaving you with 52.
  • Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return those gummy bears. This leaves you with 42.
  • You have reached the goal!

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:

  • true for n = 250
  • true for n = 42
  • false for n = 53
  • false for n = 41

Solutions

Expert Solution

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:-->


Related Solutions

*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of...
This class should include .cpp file, .h file and driver.cpp (using the language c++)! Overview of complex Class The complex class presents the complex number X+Yi, where X and Y are real numbers and i^2 is -1. Typically, X is called a real part and Y is an imaginary part of the complex number. For instance, complex(4.0, 3.0) means 4.0+3.0i. The complex class you will design should have the following features. Constructor Only one constructor with default value for Real...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int...
How to combine these 2 main functions of c++ files in 1 main class? //SinglyLinkedList int main() { SinglyLinkedList<std::string> list; list.Add("Hello"); list.Print(); list.Add("Hi"); list.Print(); list.InsertAt("Bye",1); list.Print(); list.Add("Akash"); list.Print(); if(list.isEmpty()){ cout<<"List is Empty "<<endl; } else{ cout<<"List is not empty"<<endl; } cout<<"Size = "<<list.Size()<<endl; cout<<"Element at position 1 is "<<list.get(1)<<endl; if(list.Contains("X")){ cout<<"List contains X"<<endl; } else{ cout<<"List does not contain X"<<endl; } cout<<"Position of the word Akash is "<<list.IndexOf("Akash")<<endl; cout<<"Last Position of the word Akash is "<<list.LastOf("Akash")<<endl; list.RemoveElement("Akash"); cout<<"After removing Akash...
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
**C++ program** Please provide a header file(bubble_sort.h) and a program file(bubble_sort.cpp). Also include notes to explain...
**C++ program** Please provide a header file(bubble_sort.h) and a program file(bubble_sort.cpp). Also include notes to explain code and output screenshots. Please use Bubble Sort code provided. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include...
C++ file=.class definitions.h Define a Fraction class with num and den as its private data. Include a constructor to initialize the fraction to 0/1, a copy constructor, a destructor, and overloading functions to overload the assignment operator =, the comparison operators <, >, ==, !=, arithmetic operators +, +=, -, -=, *, *=, /, /=, as well as friend functions (non-member) to overload << and >> to output and input a fraction (see book example). Also, include a private member...
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include...
C++ Assignment 1) Write a C++ program specified below: a) Include a class called Movie. Include the following attributes with appropriate types: i. Title ii. Director iii. Release Year iv. Rating (“G”, “PG”, “PG-13”, etc) - Write code that instantiates a movie object using value semantics as text: - Write code that instantiates a movie object using reference semantics: - Write the print_movie method code: - Write Constructor code: - Write Entire Movie class declaration
In C++: Write a binary search tree and include the functions to find the number of...
In C++: Write a binary search tree and include the functions to find the number of nodes and the height of the tree. Test your code in main. Print the post-order, in-order and pre-order traversal.
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
Write a C++ program that design a class definition to be put in a header file...
Write a C++ program that design a class definition to be put in a header file called fizzjazz.h A store sells online FizzJazz which are sound tones made by famous bands. For each FizzJazz, the store wants to keep track of the following attributes: * title - the name of the sound tone * band - Famous band name that created the tone * duration - this is in seconds and may be fractional: examples: 20.0, 34.5 Each attribute will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT