Generation

generate functionWed, 07 Dec 2022

A program to randomly split the money among 2 groups

#include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; void split (float money, int &group_one, int &group_two) { srand(time(NULL)); float left_money = money; while (left_money > 0) { if (rand()%2 == 1) { group_one++; left_money -= 0.5; } else { group_two++; left_money -= 0.5; } } } int main() { int group_one = 0; int group_two = 0; float money = 100; split(money, group_one, group_two); cout << "Group_one has: " << setprecision(2) << fixed << group_one*0.5 << endl;

Questions about programming?Chat with your personal AI assistant