C++ STL Algorithms and Fundamental Programming Practice

Exercise 1: Modern C++ Programming Basics

Task 1: STL Algorithm Deomnstration - reverse, reverse_copy, rotate

Source code:

#include <bits/stdc++.h>
using namespace std;

// Template function declaration
template<typename T>
void printContainer(const T& container);

// Regular function declarations
void demoReverseOperations();
void demoVectorReversal();
void demoRotationOperations();

int main() {
    cout << "Test 1: \n";
    demoReverseOperations();
    
    cout << "Test 2: \n";
    demoVectorReversal();
    
    cout << "Test 3: \n";
    demoRotationOperations();
    
    return 0;
}

// Print elements in container
template <typename T>
void printContainer(const T& container) {
    for (const auto& item : container)
        cout << item << " ";
    cout << endl;
}

void demoReverseOperations() {
    string original{"0123456789"};
    cout << "original = " << original << endl;
    
    string reversed{original};
    reverse(reversed.begin(), reversed.end());
    cout << "reversed = " << reversed << endl;
    
    string copied{original};
    reverse_copy(original.begin(), original.end(), copied.begin());
    cout << "copied = " << copied << endl;
}

void demoVectorReversal() {
    vector<int> numbers{2, 0, 4, 9};
    cout << "numbers: ";
    printContainer(numbers);
    
    vector<int> reversedCopy{numbers};
    reverse(reversedCopy.begin(), reversedCopy.end());
    cout << "reversedCopy: ";
    printContainer(reversedCopy);
    
    vector<int> dest{numbers};
    reverse_copy(numbers.begin(), numbers.end(), dest.begin());
    cout << "dest: ";
    printContainer(dest);
}

void demoRotationOperations() {
    vector<int> source{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    cout << "source: ";
    printContainer(source);
    
    vector<int> rotate1{source};
    rotate(rotate1.begin(), rotate1.begin() + 1, rotate1.end());
    cout << "rotate1: ";
    printContainer(rotate1);
    
    vector<int> rotate2{source};
    rotate(rotate2.begin(), rotate2.begin() + 2, rotate2.end());
    cout << "rotate2: ";
    printContainer(rotate2);
    
    vector<int> rotate3{source};
    rotate(rotate3.begin(), rotate3.end() - 1, rotate3.end());
    cout << "rotate3: ";
    printContainer(rotate3);
    
    vector<int> rotate4{source};
    rotate(rotate4.begin(), rotate4.end() - 2, rotate4.end());
    cout << "rotate4: ";
    printContainer(rotate4);
}

Task 2: STL Algorithm Demonstration - sort, generate, min/max

Source code:

#include <bits/stdc++.h>
using namespace std;

template <typename T>
void printContainer(const T& container);

int generateRandomNumber();
void demoSortingOperations();
void demoMinMaxOperations();

int main() {
    cout << "Test 1: \n";
    demoSortingOperations();
    
    cout << "Test 2: \n";
    demoMinMaxOperations();
    
    return 0;
}

template <typename T>
void printContainer(const T& container) {
    for (const auto& item : container)
        cout << item << " ";
    cout << endl;
}

int generateRandomNumber() {
    return rand() % 101;
}

void demoSortingOperations() {
    vector<int> data(10);
    generate(data.begin(), data.end(), generateRandomNumber);
    cout << "data: ";
    printContainer(data);
    
    vector<int> sortedFull{data};
    sort(sortedFull.begin(), sortedFull.end());
    cout << "sortedFull: ";
    printContainer(sortedFull);
    
    vector<int> sortedPartial{data};
    sort(sortedPartial.begin() + 1, sortedPartial.end() - 1);
    cout << "sortedPartial: ";
    printContainer(sortedPartial);
}

void demoMinMaxOperations() {
    vector<int> data(10);
    generate(data.begin(), data.end(), generateRandomNumber);
    cout << "data: ";
    printContainer(data);
    
    auto minIter = min_element(data.begin(), data.end());
    cout << "Minimum: " << *minIter << endl;
    
    auto maxIter = max_element(data.begin(), data.end());
    cout << "Maximum: " << *maxIter << endl;
    
    auto result = minmax_element(data.begin(), data.end());
    cout << "Minimum: " << *(result.first) << endl;
    cout << "Maximum: " << *(result.second) << endl;
    
    double average = accumulate(data.begin(), data.end(), 0) / static_cast<double>(data.size());
    cout << "Average: " << fixed << setprecision(2) << average << endl;
    
    cout << endl;
    vector<int> sorted{data};
    cout << "Original data: ";
    printContainer(data);
    sort(sorted.begin(), sorted.end());
    double trimmedAverage = accumulate(sorted.begin() + 1, sorted.end() - 1, 0) / 
                           static_cast<double>(sorted.size() - 2);
    cout << "Average (excluding min/max): " << trimmedAverage << endl;
}

Task 3: Palindrome Detection

Source code:

#include <iostream>
#include <string>
#include <algorithm>

bool checkPalindrome(std::string str);

int main() {
    using namespace std;
    string input;

    while (cin >> input)
        cout << boolalpha << checkPalindrome(input) << endl;
    
    return 0;
}

bool checkPalindrome(std::string str) {
    int length = str.size();
    int flag = 0;
    for (int i = 0; i < length; i++) {
        if (str[i] != str[length - 1 - i])
            flag = 1;
    }
    return (flag == 0);
}

Task 4: Decimal to N-base Conversion

Source code:

#include <iostream>
#include <string>
#include <algorithm>

std::string convertBase(int value, int base = 2);

int main() {
    using namespace std;

    int num;
    while (cin >> num) {
        cout << "Decimal: " << num << endl;
        cout << "Binary: " << convertBase(num) << endl;
        cout << "Octal: " << convertBase(num, 8) << endl;
        cout << "Hexadecimal: " << convertBase(num, 16) << endl << endl;
    }
    
    return 0;
}

std::string convertBase(int value, int base) {
    int remainder;
    std::string result;
    
    do {
        remainder = value % base;
        if (remainder >= 0 && remainder <= 9)
            result += (remainder + '0');
        else
            result += (remainder + 'A' - 10);
        value /= base;
    } while (value > 0);
    
    reverse(result.begin(), result.end());
    return result;
}

Task 5: Caesar Cipher Implementation

Source code:

#include <bits/stdc++.h>
using namespace std;

int main() {
    string lowercase = "abcdefghijklmnopqrstuvwxyz";
    string uppercase = "ABCDEFGHIJKLNMOPQRSTUVWXYZ";
    
    cout << "  ";
    for (int i = 0; i <= 26; i++)
        cout << setw(2) << lowercase[i];
    cout << endl;
    
    for (int shift = 1; shift <= 26; shift++) {
        cout << setw(2) << shift;
        for (int j = 0; j < 26; j++)
            cout << setw(2) << uppercase[(j + shift) % 26];
        cout << endl;
    }
    
    cout << endl;
    return 0;
}

Task 6: Arithmetic Quiz Game

Source code:

#include <bits/stdc++.h>
using namespace std;

int main() {
    srand(static_cast<unsigned>(time(nullptr)));
    
    int correct;
    int operand1, operand2;
    double score = 0;
    char operatorChar;
    
    for (int question = 1; question <= 10; question++) {
        correct = 0;
        operatorChar = "-+*/"[rand() % 4];
        
        if (operatorChar == '*') {
            operand1 = rand() % 10 + 1;
            operand2 = rand() % 10 + 1;
            correct = operand1 * operand2;
        }
        else if (operatorChar == '+') {
            operand1 = rand() % 10 + 1;
            operand2 = rand() % 10 + 1;
            correct = operand1 + operand2;
        }
        else if (operatorChar == '-') {
            operand1 = rand() % 10 + 1;
            operand2 = rand() % 10 + 1;
            while (operand2 > operand1)
                operand2 = rand() % 10 + 1;
            correct = operand1 - operand2;
        }
        else {
            operand1 = rand() % 10 + 1;
            operand2 = rand() % 10 + 1;
            while (operand1 % operand2 != 0)
                operand2 = rand() % 10 + 1;
            correct = operand1 / operand2;
        }
        
        cout << operand1 << " " << operatorChar << " " << operand2 << " = ";
        int userAnswer;
        cin >> userAnswer;
        
        if (userAnswer == correct)
            score++;
    }
    
    printf("%.2lf%%\n", score * 10);
    return 0;
}

Summary of Key STL Functions

Reversal Operations:

  • reverse(first, last) - reverses elements in the range [first, last)
  • reverse_copy(srcFirst, srcLast, destFirst) - copies and reverses elements
  • rotate(first, middle, last) - rotates elements so that middle becomes the new first element

Random Number Generation:

  • srand(static_cast<unsigned>(time(nullptr))) - seed random number generator
  • rand() % N + M - generate random number in range [M, M+N-1]

Tags: C++ STL algorithms Programming Exercise reverse

Posted on Sun, 04 Oct 2026 16:49:06 +0000 by dabaR