diff --git a/src/Ch02/02_13/CodeDemo.cpp b/src/Ch02/02_13/CodeDemo.cpp index a306936..052d4a7 100644 --- a/src/Ch02/02_13/CodeDemo.cpp +++ b/src/Ch02/02_13/CodeDemo.cpp @@ -8,7 +8,7 @@ int add_int(float a, double b, long double c){ int result = 0; - // Write your code here + result = (int) a + (int) b + (int) c; return result; } diff --git a/src/Ch03/03_08/CodeDemo.cpp b/src/Ch03/03_08/CodeDemo.cpp index f4e63e4..7f4f822 100644 --- a/src/Ch03/03_08/CodeDemo.cpp +++ b/src/Ch03/03_08/CodeDemo.cpp @@ -8,7 +8,7 @@ double my_average(int a, int8_t b, uint32_t c){ double result = 0.0; - // Write your code here + result = (a + b + c) / 3.0; return result; } diff --git a/src/Ch04/04_11/CodeDemo.cpp b/src/Ch04/04_11/CodeDemo.cpp index f33c360..6a8eb57 100644 --- a/src/Ch04/04_11/CodeDemo.cpp +++ b/src/Ch04/04_11/CodeDemo.cpp @@ -9,7 +9,9 @@ std::vector SelectKeyPoints(std::vector distances, int checkpointIndex){ std::vector result; - // Write your code here + result.push_back(*distances.begin()); // Add the first element + result.push_back(distances[checkpointIndex]); // Add the element at checkpointIndex + result.push_back(distances.back()); // Add the last element return result; } diff --git a/src/Ch05/05_09/CodeDemo.cpp b/src/Ch05/05_09/CodeDemo.cpp index 3e5e398..bfa2fc6 100644 --- a/src/Ch05/05_09/CodeDemo.cpp +++ b/src/Ch05/05_09/CodeDemo.cpp @@ -15,7 +15,18 @@ struct Resource{ double CalculateTotalCost(std::vector resources){ double result = 0.0; - // Write your code here + for (const auto& resource : resources){ + double costWithTax = resource.baseCost; + + if (resource.type == 'B') // Basic resource: 5% tax + costWithTax += resource.baseCost * 0.05; + else if (resource.type == 'L') // Luxury resource: 15% tax + costWithTax += resource.baseCost * 0.15; + + // Essential resource 'E' has no tax, so no change is needed + + result += costWithTax; + } return result; } diff --git a/src/Ch06/06_07/CodeDemo.cpp b/src/Ch06/06_07/CodeDemo.cpp index 1fe7e49..ad2561e 100644 --- a/src/Ch06/06_07/CodeDemo.cpp +++ b/src/Ch06/06_07/CodeDemo.cpp @@ -5,25 +5,30 @@ #include #include #include +#include // Function to calculate score based on milliseconds difference // You still need to make the bonus multiplier optional with a default value -double CalculateScore(int millisecondsDiff, double bonusMultiplier){ - double score = 0.0; +double CalculateScore(int millisecondsDiff, double bonusMultiplier = 1.0){ + double score; - // Write your code here - - return score; + if (std::abs(millisecondsDiff) <= 50) + score = 100; // Perfect + else if (std::abs(millisecondsDiff) <= 100) + score = 70; // Good + else if (std::abs(millisecondsDiff) <= 200) + score = 50; // Okay + else + score = 0; // Miss + + return score * bonusMultiplier; // Apply the bonus multiplier } // Overloaded function to calculate score based on seconds difference // You still need to make the bonus multiplier optional with a default value -double CalculateScore(double secondsDiff, double bonusMultiplier){ - double score = 0.0; - - // Write your code here - - return score; +double CalculateScore(double secondsDiff, double bonusMultiplier = 1.5){ + int millisecondsDiff = static_cast(secondsDiff * 1000); // Convert seconds to milliseconds + return CalculateScore(millisecondsDiff, bonusMultiplier); // Return the integer version with multiplier } // Function to call both overloaded functions and return a vector of results diff --git a/src/Ch07/07_05b/CodeDemo.cpp b/src/Ch07/07_05b/CodeDemo.cpp index 91b488d..35f63c1 100644 --- a/src/Ch07/07_05b/CodeDemo.cpp +++ b/src/Ch07/07_05b/CodeDemo.cpp @@ -74,17 +74,26 @@ int main(){ Inventory myInventory(5); // Add 5 items + myInventory.addItem("Health Potion"); + myInventory.addItem("Mana Potion"); + myInventory.addItem("Sword"); + myInventory.addItem("Shield"); + myInventory.addItem("Bow"); // Display current inventory myInventory.displayInventory(); // Try to add another item when inventory is full + myInventory.addItem("Arrow"); // Remove an item + myInventory.removeItem("Mana Potion"); // Display the item count + std::cout << "The inventory now contains: " << myInventory.getItemCount() << " items." << std::endl; // Access item by index + std::cout << "Item at index 2: " << myInventory.getItem(2) << std::endl; // Display final state of inventory myInventory.displayInventory(); diff --git a/src/Ch07/07_10/CodeDemo.cpp b/src/Ch07/07_10/CodeDemo.cpp index 4fbfbde..43f0f8c 100644 --- a/src/Ch07/07_10/CodeDemo.cpp +++ b/src/Ch07/07_10/CodeDemo.cpp @@ -9,7 +9,53 @@ #include class Person{ - // Write your code here +private: + std::string name; + float energy; + float happiness; + float health; + +public: + // Constructor + Person(const std::string& name, float energy, float happiness, float health) + : name(name), energy(energy), happiness(happiness), health(health) {} + + // Member function to eat + void Eat(float calories){ + energy += (calories * 7.0) / 200.0; + if (energy > 100) energy = 100; // Cap energy to 100 + } + + // Member function to play + void Play(float minutes){ + happiness += minutes / 2.0; // Playing increases happiness by half the minutes + energy -= minutes / 3.0; // Playing decreases energy proportionally + if (happiness > 100) happiness = 100; // Cap happiness to 100 + if (energy < 0) energy = 0; // Ensure energy doesn't go below 0 + } + + // Member function to sleep + void Sleep(float hours){ + energy += hours * 3.75; // Sleeping increases energy + health += hours * 2.5; // Sleeping increases health + if (energy > 100) energy = 100; // Cap energy to 100 + if (health > 100) health = 100; // Cap health to 100 + } + + // Getter for energy + float GetEnergy() const{ + return energy; + } + + // Getter for happiness + float GetHappiness() const{ + return happiness; + } + + // Getter for health + float GetHealth() const{ + return health; + } }; int main(){ diff --git a/src/Ch08/08_10/CodeDemo.cpp b/src/Ch08/08_10/CodeDemo.cpp index 9aa5cb7..6c3c083 100644 --- a/src/Ch08/08_10/CodeDemo.cpp +++ b/src/Ch08/08_10/CodeDemo.cpp @@ -24,9 +24,26 @@ enum class Operation{ }; deque> ManagePetSchedule(const deque>& initialActivities, const vector>>& operations){ - deque> schedule; + deque> schedule = initialActivities; // Initialize the schedule - // Write your code here. + for (const auto& op : operations){ + switch (op.first) { + case Operation::ADD_FRONT: + schedule.push_front(op.second); + break; + case Operation::ADD_BACK: + schedule.push_back(op.second); + break; + case Operation::REMOVE_FRONT: + if (!schedule.empty()) + schedule.pop_front(); + break; + case Operation::REMOVE_BACK: + if (!schedule.empty()) + schedule.pop_back(); + break; + } + } return schedule; }