Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Ch02/02_13/CodeDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Ch03/03_08/CodeDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Ch04/04_11/CodeDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
std::vector<int> SelectKeyPoints(std::vector<int> distances, int checkpointIndex){
std::vector<int> 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;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Ch05/05_09/CodeDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,18 @@ struct Resource{
double CalculateTotalCost(std::vector<Resource> 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;
}
Expand Down
27 changes: 16 additions & 11 deletions src/Ch06/06_07/CodeDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@
#include <iostream>
#include <cstdint>
#include <vector>
#include <cmath>

// 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<int>(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
Expand Down
9 changes: 9 additions & 0 deletions src/Ch07/07_05b/CodeDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
48 changes: 47 additions & 1 deletion src/Ch07/07_10/CodeDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,53 @@
#include <utility>

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(){
Expand Down
21 changes: 19 additions & 2 deletions src/Ch08/08_10/CodeDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,26 @@ enum class Operation{
};

deque<pair<string, int>> ManagePetSchedule(const deque<pair<string, int>>& initialActivities, const vector<pair<Operation, pair<string, int>>>& operations){
deque<pair<string, int>> schedule;
deque<pair<string, int>> 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;
}
Expand Down