Question: Need help for these C++ codes. 1. Make all tests pass #include #include #include using namespace std; class Cat { private: string name; public: Cat(string
Need help for these C++ codes.
1. Make all tests pass
#include
class Cat { private: string name; public: Cat(string inputName); ~Cat(); string getName(); void setName(string newName); };
//Your Code Starts Here void Cat :: Cat(string inputName){ name = inputName; return; }
void Cat :: setName(string newName){ Name = newName; }
void Cat :: string getName(){ return Name; } //Your Code Ends Here
int main() { Cat my_pet("fluffy"); assert(my_pet.getName() == "fluffy"); my_pet.setName("Ginger"); assert(my_pet.getName() == "Ginger"); cout << "All tests passed" << endl; }
2. Make all test pass
#include
//BEGIN YOUR CODE HERE
//END YOUR CODE HERE
int main() { long* chunk; set_values(chunk);
assert( *(chunk) == 0); assert( *(chunk + 2) == 4); assert( *(chunk + 3) == -9); assert( *(chunk + 5) == -25); assert( *(chunk + 6) == 36); assert( *(chunk + 7) == -49); free(chunk); cout << "All tests pass" << endl; return 0; }
3. Find the bugs and fix
#include
//edit this all you want int next_fibonacci(int* fibs_so_far, int last_index){ if (last_index = 0){ return 1; } else { return (fibs_so_far[last_index-1] + fibs_so_far[last_index-2]); }; };
int main(){ //don't edit this line: int first_10_fibs[10] = {1,1,1,1,1,1,1,1,1,1}; //have at it here for(int i = 0; i < 10; i++){ first_10_fibs[i] = next_fibonacci(first_10_fibs, i-1); cout < first_10_fibs[i] < endl; }; //Don't edit the tests assert(first_10_fibs[2] == 2); assert(first_10_fibs[5] == 8); assert(first_10_fibs[9] == 55); return 0; }
4. Make all test pass
#include
// Return the altered bits of "input" // where the first "numBits" of "newBits" (unspecified bits are 0) // replace "input" bits starting at "position"
unsigned char set_bits_to( unsigned char input, unsigned char newBits, short position, short numBits){ //Your Code Here
//Stop Code Here };
int main() { assert(set_bits_to(0b1111, 0b0, 0, 1) == 0b1110); assert(set_bits_to(0b1111, 0b0, 1, 1) == 0b1101); assert(set_bits_to(0b1111, 0b0, 0, 3) == 0b1000);
assert(set_bits_to(0b11001100, 0b101, 2, 3) == 0b11010100); assert(set_bits_to(0b11001100, 0b101, 2, 5) == 0b10010100); assert(set_bits_to(0b11001100, 0b101, 3, 3) == 0b11101100); assert(set_bits_to(0b11001100, 0b101, 5, 3) == 0b10101100); assert(set_bits_to(0b1111, 0b101010, 0, 6) == 0b101010); assert(set_bits_to(0b1111, 0b101010, 1, 6) == 0b1010101); assert(set_bits_to(0b1111, 0b101010, 0, 3) == 0b1010); cout << "All tests passed" << endl; return 0; }
5. Make all test pass
#include
struct Fruit { string type; int ripeness; Fruit(string inputType, int startingRipeness){ ripeness = startingRipeness; type = inputType; } };
//Your code here
//stop here
int main() { Fruit freshBanana("Banana", 4); ripen(freshBanana, 2); ripen(freshBanana, 2); throwOut(freshBanana); Fruit orange("Orange", 7); ripen(orange, 1); ripen(orange, 8); compost(orange); assert(freshBanana.ripeness == 8); assert(freshBanana.type == "Garbage"); assert(orange.ripeness == 16); assert(orange.type == "Soil"); cout << "All tests pass" << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
