- Executors
-
Learn
Learn Python Runnable examples, NumPy, pandas, and more Learn AINeural networks, ML and LLMs from scratch Learn C & C++ Pointers, memory, STL, and modern C++ Learn C#C# basics, OOP, LINQ, async, and .NET exercises Python Algorithms Search, sorting, recursion, and visualizers O(n) C & C++ Algorithms Compare algorithms implemented in C and C++
- ChallengesNew
Backpropagation applies the chain rule layer by layer, from the output back to the inputs. The network has n inputs, h hidden neurons and one output, all with sigmoid activation (σ(z) = 1 / (1 + e^(-z))):
- hidden: a_j = σ(inputs · hidden_weights[j] + hidden_biases[j])
- output: y = σ(a · output_weights + output_bias)
- loss: L = (y - target)²
Gradients:
- delta_out = 2 × (y - target) × y × (1 - y)
- Output layer: dL/doutput_weights[j] = delta_out × a_j and dL/doutput_bias = delta_out
- delta_j = delta_out × output_weights[j] × a_j × (1 - a_j)
- Hidden layer: dL/dhidden_weights[j][i] = delta_j × inputs[i] and dL/dhidden_biases[j] = delta_j
Implement backprop_gradients(inputs, hidden_weights, hidden_biases, output_weights, output_bias, target).
Output
Return a matrix with h + 1 rows:
- Row j (for each hidden neuron j, in order): the n gradients of
hidden_weights[j], then the gradient ofhidden_biases[j]. - Last row: the h gradients of
output_weights, then the gradient ofoutput_bias.
Constraints
1 ≤ n, h ≤ 20
-5 ≤ each input, weight and bias ≤ 5
0 ≤ target ≤ 1
Examples
Example 1
Input:
inputs = [1.0]
target = 1.0
output_bias = 0.0
hidden_biases = [0.0]
hidden_weights = [
[0.0]
]
output_weights = [0.0]
Expected output:
[
[0.0, 0.0],
[-0.125, -0.25]
]
Explanation
a = σ(0) = 0.5 and y = σ(0) = 0.5. delta_out = 2 × (0.5 - 1) × 0.25 = -0.25. Output row: [-0.25 × 0.5, -0.25] = [-0.125, -0.25]. delta_1 = -0.25 × 0 × 0.25 = 0, so the hidden row is [0, 0]: a zero output weight blocks the gradient.
Example 2
Input:
inputs = [1.0, 0.0]
target = 0.0
output_bias = -1.0
hidden_biases = [0.0, 0.0]
hidden_weights = [
[0.0, 1.0],
[0.0, -1.0]
]
output_weights = [1.0, 1.0]
Expected output:
[
[0.0625, 0.0, 0.0625],
[0.0625, 0.0, 0.0625],
[0.125, 0.125, 0.25]
]
Explanation
Both hidden values are σ(0) = 0.5, so y = σ(0.5 + 0.5 - 1) = 0.5 and delta_out = 2 × 0.5 × 0.25 = 0.25. Output row: [0.125, 0.125, 0.25]. Each delta_j = 0.25 × 1 × 0.25 = 0.0625, giving hidden rows [0.0625, 0, 0.0625] (the second input is 0).
References
-
Example 1 Not runInput:
inputs = [1.0] target = 1.0 output_bias = 0.0 hidden_biases = [0.0] hidden_weights = [ [0.0] ] output_weights = [0.0]Expected output:[ [0.0, 0.0], [-0.125, -0.25] ]Your output -
Example 2 Not runInput:
inputs = [1.0, 0.0] target = 0.0 output_bias = -1.0 hidden_biases = [0.0, 0.0] hidden_weights = [ [0.0, 1.0], [0.0, -1.0] ] output_weights = [1.0, 1.0]Expected output:[ [0.0625, 0.0, 0.0625], [0.0625, 0.0, 0.0625], [0.125, 0.125, 0.25] ]Your output
4 hidden tests also run when you submit.
Online C++ Compiler & Debugger
Write, compile, run, and debug C++ online. Set breakpoints, inspect variables, and step through code with GDB in Server mode - no setup required.
🚀 2,393,936 total executions (1,562 this month)
Practice C++ with coding challenges
Solve hands-on problems right in this editor: run the sample tests, submit, and get an AI code review.
Learn and practise C/C++
Follow a learning path with runnable lessons, then test yourself with coding challenges.
💻 About This C++ Online Executor
The CodeUtility C++ Executor lets you write and run C++ code directly in your browser - no installation or compiler setup needed. It’s powered by a secure sandbox that supports real C++ standards from C++11, C++14, C++17, up to the latest version.
This tool uses a real C++ compiler running in the cloud to compile and execute your code, ensuring accurate and consistent behavior across versions. You can freely experiment with core C++ concepts like variables, functions, loops, conditionals, and object-oriented programming.
Whether you’re preparing for interviews, learning modern C++ features, or verifying algorithm logic, the CodeUtility C++ Executor provides a simple yet powerful environment to run code instantly from any device.
More than a code runner
Use a multi-file editor, an isolated execution terminal, runtime-specific libraries, persistent workspaces, sharing, and real-time collaboration from the same executor.
How to use this executor?
Choose a runtime, open or create a file, write your code, and select Run. Output appears in the terminal, while signed-in users can keep files in a workspace, install libraries, and collaborate through shares.
- Select a version and edit the current file or open another workspace file.
- Run the code, provide input in the terminal when requested, and review saved run history.
- Use libraries, workspace tools, sharing, and chat when your task needs more than one snippet.
💡 C++ Basics & Examples You Can Try Above
1. Declaring Variables and Constants
C++ requires you to declare the type of each variable. Use const to define read-only values that cannot be changed after initialization.
int age = 30;
double pi = 3.14159;
char grade = 'A';
std::string name = "Alice";
bool isActive = true;
// Constants
const int MAX_USERS = 100;
const std::string COMPANY = "CodeUtility";
// Attempting to modify a const will cause a compile-time error
// MAX_USERS = 200; // ❌ error
2. Conditionals (if / switch)
Use if, else if, else for branching. Use switch for multiple values.
int x = 2;
if (x == 1) {
std::cout << "One\n";
} else if (x == 2) {
std::cout << "Two\n";
} else {
std::cout << "Other\n";
}
// Switch-case
switch (x) {
case 1:
std::cout << "One\n";
break;
case 2:
std::cout << "Two\n";
break;
default:
std::cout << "Other\n";
}
3. Loops
Use for, while, and do-while to repeat code blocks.
for (int i = 0; i < 3; i++) {
std::cout << i << "\n";
}
int n = 3;
while (n > 0) {
std::cout << n << "\n";
n--;
}
4. Arrays
Arrays store multiple elements of the same type.
int numbers[3] = {10, 20, 30};
std::cout << numbers[1] << "\n";
5. Array/Vector Manipulation
Use std::vector for dynamic arrays.
#include <vector>
std::vector nums = {1, 2, 3};
nums.push_back(4);
nums.pop_back();
for (int n : nums) {
std::cout << n << " ";
}
6. Console Input/Output
Use std::cin for input and std::cout for output.
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << "\n";
7. Functions
Functions group reusable logic. Use parameters and return types.
int add(int a, int b) {
return a + b;
}
std::cout << add(3, 4);
8. Maps
std::map stores key-value pairs like dictionaries.
#include <map>
std::map ages;
ages["Alice"] = 30;
std::cout << ages["Alice"];
9. Exception Handling
Use try and catch to handle runtime errors.
try {
throw std::runtime_error("Error occurred");
} catch (const std::exception& e) {
std::cout << e.what();
}
10. File I/O
Use fstream to read from and write to files.
#include <fstream>
std::ofstream out("file.txt");
out << "Hello File";
out.close();
std::ifstream in("file.txt");
std::string line;
getline(in, line);
std::cout << line;
in.close();
11. String Manipulation
C++ strings support methods like length(), substr(), and find().
std::string text = "Hello World";
std::cout << text.length();
std::cout << text.substr(0, 5);
std::cout << text.find("World");
12. Classes & Objects
C++ supports object-oriented programming using classes.
class Person {
public:
std::string name;
Person(std::string n) : name(n) {}
void greet() { std::cout << "Hi, I'm " << name; }
};
Person p("Alice");
p.greet();
13. Pointers
A pointer stores the memory address of another variable. Use & to get an address,
and * to dereference (access the value at the address).
int x = 10;
int* ptr = &x;
std::cout << "Value of x: " << x << "\n";
std::cout << "Address of x: " << ptr << "\n";
std::cout << "Value from pointer: " << *ptr << "\n";
// Modify x through pointer
*ptr = 20;
std::cout << "Updated x: " << x << "\n";
🐞 How to Debug %{language} Code Online
Use the online %{language} debugger to find runtime and logic errors without installing a compiler or desktop IDE. Server mode compiles with debug symbols and runs the program under GDB.
- Set one or more breakpoints beside the line numbers.
- Select Debug to compile and pause at the first matching breakpoint.
- Inspect the current line, call stack, and variables.
- Use Continue, Step Over, Step Into, or Step Out to control execution.
- Select Stop to end the debugging session.
Start with a small reproducible example and place a breakpoint just before the value or control flow becomes unexpected.