<mosaic.cnfolio.com>
PROGRAM
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <time.h>
4. #include <stdlib.h>
5. #include <math.h>
6.
7.
8. /*
9. START
10.
11.    //37 is treated as 00 in this simulation
12.
13.    initialize arrays of bets and amounts from -1
14.
15.    do this 4 times
16.    {
17.       read bet numbers and amount from user
18.    }
19.
20.    generate random number between 0 and 37 (inclusive)
21.    output user choices
22.    output winning number
23.    update sum accordingly
24.
25.    print money earned
26.
27. END
28.
29. */
30. int main()
31. {
32.    //setting time NULL to generate random number later on
33.    srand(time(NULL));
34.
35.    printf("Welcome to American Roulette, please place your bets.\n");
36.    printf("In this version, you will place 4 bets on one roll.\n");
37.    printf("Note: to place a bet on 00, enter 37 instead.\n");
38.    printf("__________________________________________\n");
39.
40.    int bets; //the numbers on which the bets are placed are in this array
41.    float amounts; //the bet amounts are in this array
42.
43.
44.    /*
45.    the purpose of this loop is to initialize the arrays with -1.
46.    */
47.    for (int i = 0; i < 4; i++)
48.    {
49.       bets[i] = -1;
50.       amounts[i] = -1;
51.    }
52.
53.
54.    /*
55.    the purpose of this loop is to take input from the user.
56.    */
57.    for (int i = 0; i < 4; i++)
58.    {
59.       int input = 0;
60.
61.       printf("\n\nBET#%d:\n", i + 1); //output the bet number from "BET#1" to "BET#4".
62.
63.       printf("Please enter bet number, followed by bet amount:" );
64.       scanf("%d%f", &bets[i], &amounts[i]); //reading two input values.
65.    }
66.
67.    printf("\n__________________________________________\n");
68.
69.    float sum = 0; //the sum that the user earns
70.
71.    int winningNum = rand() % 38; //37 will be treated as "00"
72.
73.    //printing the winning number
74.    if (winningNum != 37)// since 00 cannot be differentiated from 0, 37 is treated as 00
75.    {
76.       printf("The winning number is %d.\n", winningNum);
77.    }
78.    else
79.    {
80.       printf("The winning number is 00.\n");
81.    }
82.
83.
84.    for (int i = 0; i < 4; i++) //running the simulation
85.    {
86.
87.       printf("\nBet # %d: \n", i + 1);
88.
89.
90.       if (bets[i] > 37 || bets[i] < 0) //invalid bet number
91.       {
92.          printf("The bet number for this bet is invalid");
93.       }
94.       else if (amounts[i] < 0) //invalid bet amount
95.       {
96.          printf("The amount for this bet is invalid");
97.       }
98.       else // ie the input is correct
99.       {
100.          //printing the user bet number
101.          if (bets[i] != 37) // since 00 cannot be differentiated from 0, 37 is treated as 00
102.          {
103.             printf("Your bet number is %d.\n", bets[i]);
104.          }
105.          else
106.          {
107.             printf("Your bet number is 00.\n");
108.          }
109.
110.          //print the user bet amount
111.          printf("Your bet amount is %.2f.\n", amounts[i]); //printing amount to 2 decimal places
112.
113.          //checking if the user won anything
114.          if (winningNum == bets[i])
115.          {
116.             //the payout for getting one number right is 1:35, so if you bet \$10, you will get \$350
117.             printf("You won %.2f dollars!\n", amounts[i] * 35);
118.             sum += amounts[i] * 35; //adding the profits to the grand total
119.          }
120.          else
121.          {
122.             printf("You lost %.2f dollars!\n", amounts[i]);
123.             sum -= amounts[i]; //deducting the losses from the grand total
124.          }
125.       }
126.
127.    }
128.
129.    printf("\n__________________________________________\n");
130.    if (sum < 0) //if the user lost money
131.    {
132.       printf("You lost %.2f dollars!", sum*-1);
133.    }
134.    else if (sum > 0) //if the user won money
135.    {
136.       printf("You won %.2f dollars!", sum);
137.    }
138.    else //if the user didnt have any winnings or loss
139.    {
140.       printf("You broke even.");
141.    }
142.
143.    return 0;
144. }