online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
#include <iostream> #include <vector> #include <string> struct Box { std::string Ball1; std::string Ball2; }; int main() { // Коробки с шарами std::vector<Box> boxes; boxes.push_back({ "gold", "gold" }); boxes.push_back({ "gold", "silver" }); boxes.push_back({ "silver", "silver" }); // Тянем шары const int N = 1000000; int winCount = 0; int chosen_index; std::string chosen_ball; std::string remaining_ball; int silver_outcome = 0; for (int i = 0; i < N; i++) { // Выбор случайной коробки Box box = boxes[std::rand() % boxes.size()]; // 1) Тянем первый шар из коробки, он должен оказаться золотой (другие случаи по условию не возможны) chosen_index = std::rand() % 2; if (chosen_index == 0) { chosen_ball = box.Ball1; remaining_ball = box.Ball2; } else { chosen_ball = box.Ball2; remaining_ball = box.Ball1; } //2) Проверяем вытянутый шар if (chosen_ball == "silver") { // вытянули серебро. Невозможный случай по условию (коллапс вселенной (из общего числа попыток N нужно эту симуляцию удалить)) silver_outcome ++; } else // вытянули золотой, проверяем второй. Если он золотой, то +1 в счётчик успеха { winCount += remaining_ball == "gold"; } } std::cout << "Answer = " << (double)(winCount) / (N - silver_outcome) << "\n"; }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue