Practice Problems

Practice Problems#

Let’s look at some practice problems on std::vector, structs, and references.

  1. Let’s modify our struct Planet example to add the period as a member and fill the period automatically via Kepler’s law in a loop.

    solution
    Listing 43 period.cpp#
    #include <iostream>
    #include <vector>
    #include <cmath>
    #include <format>
    #include <string>
    
    struct Planet {
        std::string name;
        double a{};            // semi-major axis
        double e{};            // eccentricity
        double P{};
    };
    
    int main() {
    
        std::vector<Planet> planets{{.name="Mercury", .a=0.3871, .e=0.2056},
                                    {.name="Venus",   .a=0.7233, .e=0.0068},
                                    {.name="Earth",   .a=1.0000, .e=0.0167},
                                    {.name="Mars",    .a=1.5237, .e=0.0934},
                                    {.name="Jupiter", .a=5.2029, .e=0.0484},
                                    {.name="Saturn",  .a=9.5370, .e=0.0539},
                                    {.name="Uranus",  .a=19.189, .e=0.0473},
                                    {.name="Neptune", .a=30.070, .e=0.0086}};
    
        for (auto &p : planets) {
            p.P = std::sqrt(std::pow(p.a, 3.0));
        }
    
        for (auto p : planets) {
            std::cout << std::format("planet {} has a period of {:.2f} years.\n",
                                     p.name, p.P);
        }
    }
    
  2. Create a vector with this list of names:

    luke
    leia
    han
    chewy
    obi-wan
    yoda
    vader
    lando
    

    Now loop over the names and use the std::toupper() function from cctype to capitalize the first letter in each name (updating the vector.

    Finally loop over the vector again and output the names.

    solution
    Listing 44 names.cpp#
    #include <iostream>
    #include <string>
    #include <vector>
    #include <cctype>
    
    int main() {
    
        std::vector<std::string> names{"luke",
                                       "leia",
                                       "han",
                                       "chewy",
                                       "obi-wan",
                                       "yoda",
                                       "vader",
                                       "lando"};
    
        for (auto &nm : names) {
            nm[0] = std::toupper(nm[0]);
        }
    
    
        for (const auto &nm : names) {
            std::cout << nm << std::endl;
        }
    
    }
    
  3. Create a vector containing the numbers 0 through 9.

    Now loop over the vector, and modify each element to be the sum of itself and the all the previous elements.

    There are several ways to do this. For this example, we will create a variable called previous that will be updated in the loop body to always hold the previous sum.

    Upon completion, output the new values to the screen.

    solution
    Listing 45 sum.cpp#
    #include <iostream>
    #include <vector>
    #include <format>
    
    int main() {
    
        std::vector<int> nums{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    
        int previous{};
        for (auto &n : nums) {
            n += previous;
            previous = n;
        }
    
        int i{};
        for (auto n : nums) {
            std::cout << std::format("{:2} : {:3}\n", i, n);
            i++;
        }
    
    }