Skip to content
Laviyx Learn

Try

C++ Arrays, Vectors and Strings Practice

Practise std::vector, std::array, std::string, 2D grids and structs with common collection tasks.

Beginner7 problemsUpdated

Review first: Arrays, Vectors, Strings, 2D Arrays and Grids, Structs and Enums

  1. 1.Marks statistics

    Beginner

    Given std::vector<int> marks = {56, 91, 78, 34, 88};, print the total, the average (as a decimal) and the highest mark.

    Show hint

    One range-based for loop can add up the total and track the highest value at the same time.

    Show solution
    C++
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<int> marks = {56, 91, 78, 34, 88};
    
        int total = 0;
        int highest = marks[0];
        for (int m : marks) {
            total += m;
            if (m > highest) highest = m;
        }
    
        std::cout << "Total: " << total << '\n';
        std::cout << "Average: " << static_cast<double>(total) / marks.size() << '\n';
        std::cout << "Highest: " << highest << '\n';
    }
    Output
    Total: 347
    Average: 69.4
    Highest: 91
  2. 2.Reverse a string in place

    Beginner

    Reverse std::string word = "algorithm"; in place by swapping characters from both ends, without std::reverse.

    Show hint

    Use two indexes, one from the start and one from the end, and swap until they meet.

    Show solution
    C++
    #include <iostream>
    #include <string>
    #include <utility>
    
    int main() {
        std::string word = "algorithm";
    
        std::size_t i = 0;
        std::size_t j = word.size() - 1;
        while (i < j) {
            std::swap(word[i], word[j]);
            ++i;
            --j;
        }
        std::cout << word << '\n';
    }
    Output
    mhtirogla
  3. 3.Count the vowels

    Beginner

    Count the vowels (a, e, i, o, u, either case) in "Programming In C++ Is Fun".

    Show hint

    Convert each character with std::tolower (from <cctype>) and check it against "aeiou" with std::string::find.

    Show solution
    C++
    #include <cctype>
    #include <iostream>
    #include <string>
    
    int main() {
        std::string text = "Programming In C++ Is Fun";
        const std::string vowels = "aeiou";
    
        int count = 0;
        for (char c : text) {
            char lower = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
            if (vowels.find(lower) != std::string::npos) ++count;
        }
        std::cout << count << '\n';
    }
    Output
    6
  4. 4.Palindrome check

    Beginner

    Write bool is_palindrome(const std::string& s) and print the result for "racecar" and "rocket".

    Show hint

    Compare the string with a reversed copy, built from s.rbegin() and s.rend().

    Show solution
    C++
    #include <iostream>
    #include <string>
    
    bool is_palindrome(const std::string& s) {
        return s == std::string(s.rbegin(), s.rend());
    }
    
    int main() {
        std::cout << std::boolalpha << is_palindrome("racecar") << ' ' << is_palindrome("rocket") << '\n';
    }
    Output
    true false
  5. 5.Row and column totals

    Beginner

    For the grid {{3, 1, 4}, {1, 5, 9}, {2, 6, 5}}, print the total of each row, then the total of each column.

    Show hint

    For column totals, put the column loop on the outside.

    Show solution
    C++
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<std::vector<int>> grid = {{3, 1, 4}, {1, 5, 9}, {2, 6, 5}};
    
        for (const auto& row : grid) {
            int sum = 0;
            for (int v : row) sum += v;
            std::cout << sum << ' ';
        }
        std::cout << '\n';
    
        for (std::size_t c = 0; c < grid[0].size(); ++c) {
            int sum = 0;
            for (std::size_t r = 0; r < grid.size(); ++r) sum += grid[r][c];
            std::cout << sum << ' ';
        }
        std::cout << '\n';
    }
    Output
    8 15 13
    6 12 18
  6. 6.Remove duplicates, keep order

    Beginner

    Build a new vector from {4, 9, 4, 1, 9, 9, 7} that keeps only the first occurrence of each value, in the original order.

    Show hint

    Before adding a value, check whether it is already in the result with std::find.

    Show solution
    C++
    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    int main() {
        std::vector<int> data = {4, 9, 4, 1, 9, 9, 7};
        std::vector<int> unique;
    
        for (int x : data) {
            if (std::find(unique.begin(), unique.end(), x) == unique.end()) {
                unique.push_back(x);
            }
        }
        for (int x : unique) std::cout << x << ' ';
        std::cout << '\n';
    }
    Output
    4 9 1 7
  7. 7.Inventory value

    Beginner

    Define struct Product { std::string name; double price; int stock; };. For the products {"Pen", 25, 120}, {"Notebook", 60, 0} and {"Bag", 799, 4}, print the names of out-of-stock products and the total stock value.

    Show hint

    Loop over a std::vector<Product> by const&; the value of one product is price * stock.

    Show solution
    C++
    #include <iostream>
    #include <string>
    #include <vector>
    
    struct Product {
        std::string name;
        double price = 0;
        int stock = 0;
    };
    
    int main() {
        std::vector<Product> products = {{"Pen", 25, 120}, {"Notebook", 60, 0}, {"Bag", 799, 4}};
    
        double value = 0;
        for (const Product& p : products) {
            if (p.stock == 0) std::cout << p.name << " is out of stock\n";
            value += p.price * p.stock;
        }
        std::cout << "Total value: " << value << '\n';
    }
    Output
    Notebook is out of stock
    Total value: 6196