P.3 — Challenge projects (chapters 9–12)
Bigger problems that use structs, classes and more. Take your time — each one might take an evening.
Problems#
Write void sortThree(int& a, int& b, int& c) that puts the three values in order, smallest first, by changing the originals.
Use references so the function changes the caller's variables. Compare pairs and std::swap them when they're in the wrong order — three compares are enough.
#include <iostream>#include <utility> void sortThree(int& a, int& b, int& c){ if (a > b) std::swap(a, b); if (b > c) std::swap(b, c); if (a > b) std::swap(a, b);} int main(){ int x { 9 }, y { 2 }, z { 5 }; sortThree(x, y, z); std::cout << x << ' ' << y << ' ' << z << '\n'; // 2 5 9 return 0;}Make a class Rectangle with a private width and height, a constructor, area(), perimeter(), and isSquare(). All three functions should be const.
Store the sizes as double members (m_width, m_height). Set them in the constructor's member initializer list.
#include <iostream> class Rectangle{public: Rectangle(double width, double height) : m_width { width }, m_height { height } { } double area() const { return m_width * m_height; } double perimeter() const { return 2 * (m_width + m_height); } bool isSquare() const { return m_width == m_height; } private: double m_width {}; double m_height {};}; int main(){ Rectangle r { 4.0, 2.5 }; std::cout << "Area: " << r.area() << '\n'; std::cout << "Perimeter: " << r.perimeter() << '\n'; std::cout << std::boolalpha << "Square? " << r.isSquare() << '\n'; return 0;}Make a class Account with deposit, withdraw (which refuses to go below zero), balance, and printHistory, which lists every successful transaction.
Keep a std::vector<std::string> of history lines as a private member. Add a line each time deposit or a successful withdraw happens.
#include <iostream>#include <string>#include <vector> class Account{public: void deposit(double amount) { if (amount <= 0) return; m_balance += amount; m_history.push_back("Deposit +" + std::to_string(amount)); } bool withdraw(double amount) { if (amount <= 0 || amount > m_balance) return false; m_balance -= amount; m_history.push_back("Withdraw -" + std::to_string(amount)); return true; } double balance() const { return m_balance; } void printHistory() const { for (const auto& line : m_history) std::cout << line << '\n'; } private: double m_balance { 0.0 }; std::vector<std::string> m_history {};}; int main(){ Account acc {}; acc.deposit(100); acc.withdraw(30); acc.withdraw(500); // refused acc.printHistory(); std::cout << "Balance: " << acc.balance() << '\n'; return 0;}Given a 3×3 board as std::array<std::array<char, 3>, 3> filled with 'X', 'O' or ' ', write char winner(const Board& b) that returns 'X', 'O', or ' ' if nobody has won.
Check the 3 rows, the 3 columns and the 2 diagonals. A line wins if all three cells match and aren't ' '. A small helper line(a, b, c) makes this neat.
#include <array>#include <iostream> using Board = std::array<std::array<char, 3>, 3>; char line(char a, char b, char c){ return (a != ' ' && a == b && b == c) ? a : ' ';} char winner(const Board& b){ for (int i { 0 }; i < 3; ++i) { if (char w { line(b[i][0], b[i][1], b[i][2]) }; w != ' ') return w; // row if (char w { line(b[0][i], b[1][i], b[2][i]) }; w != ' ') return w; // column } if (char w { line(b[0][0], b[1][1], b[2][2]) }; w != ' ') return w; return line(b[0][2], b[1][1], b[2][0]);} int main(){ Board b { { { 'X', 'O', 'O' }, { ' ', 'X', 'O' }, { 'O', ' ', 'X' }, } }; std::cout << "Winner: " << winner(b) << '\n'; // X return 0;}using Board = ... gives a long type a short name. The if (init; condition) form makes a variable that only lives inside that if.
Tip: press ← / → to move between lessons, / to search.