OutputTracer

Example Programs

Browse sample C++ programs by topic. Pick one to load it straight into the predictor.

Variables

Basic Arithmetic

#include <iostream>
using namespace std;

int main() {
    int a = 10;
    int b = 20;
    cout << a + b;

    return 0;
}

Expected: 30

Conditions

If / Else

#include <iostream>
using namespace std;

int main() {
    int x = 10;

    if (x > 5)
        cout << "Yes";
    else
        cout << "No";

    return 0;
}

Expected: Yes

Loops

Loop

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 3; i++)
        cout << i;

    return 0;
}

Expected: 123

Nested Loop

#include <iostream>
using namespace std;

int main() {
    for (int i = 1; i <= 2; i++) {
        for (int j = 1; j <= 2; j++) {
            cout << i << j << " ";
        }
    }

    return 0;
}

Expected: 11 12 21 22

Arrays

Array Sum

#include <iostream>
using namespace std;

int main() {
    int arr[4] = {2, 4, 6, 8};
    int sum = 0;

    for (int i = 0; i < 4; i++) {
        sum += arr[i];
    }

    cout << sum;

    return 0;
}

Expected: 20

Strings

String Concatenation

#include <iostream>
using namespace std;

int main() {
    string first = "Output";
    string second = "Tracer";

    cout << first + second;

    return 0;
}

Expected: OutputTracer

Functions

Function Call

#include <iostream>
using namespace std;

int square(int n) {
    return n * n;
}

int main() {
    cout << square(6);

    return 0;
}

Expected: 36

Pointers

Pointer Basics

#include <iostream>
using namespace std;

int main() {
    int x = 15;
    int* p = &x;
    *p = *p + 5;

    cout << x;

    return 0;
}

Expected: 20

Recursion

Recursive Factorial

#include <iostream>
using namespace std;

int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    cout << factorial(4);

    return 0;
}

Expected: 24

OOP

Class & Object

#include <iostream>
using namespace std;

class Rectangle {
public:
    int width, height;
    int area() {
        return width * height;
    }
};

int main() {
    Rectangle r;
    r.width = 5;
    r.height = 4;

    cout << r.area();

    return 0;
}

Expected: 20