Directions: Fill in answers on the pages below. Don't spend
too much time on any one problem.
Points for each problem: 1-15, 2-20, 3-25, 4-15, 5-25.
#include <stdio.h>
int main() {
int p[] = {2, 3, 5, 7, 11, 13, 17};
/* Use a loop to add the square of each number in this array. */
/* Then print the sum. */
int sum = 0;
int i;
(for i = 0; i < 7; i++)
sum = sum + p[i]*p[i];
printf("Sum: %i\n", sum);
}
#include <stdio.h>
/* give a prototype of the function printp on the next line */
void printp(int p[]);
int main() {
int p[] = {2, 3, 5, 7, 11, 13, 17};
/* call the function printp on the next line */
printp(p);
}
/* give the definition of the function printp below */
void printp(int p[]) {
int i;
for (i = 0; i < 7; i++) {
printf("%i", p[i];
if (i != 6) printf(",");
else printf("\n");
}
}
You are to write a program that simulates betting repeatedly on a single number. You start with $1000 (and we'll drop the $-sign). Just initialize a variable such as amount to 1000. You bet 20 at a time, inside a loop. If you lose, your amount of money drops by 20. If you win, your amount of money goes up by 20*36 = 720. I made a mistake in the problem statement here: This program is keeping track of the gains and loses, so in case of a win the amount of money goes up by 20*35 = 700, and NOT by 720. The loop continues until one of two things happens: either your money drops below 20 (actually to 0, since everything here occurs in increments of 20), so you can't bet any more, or your money goes up to 2000 or above, so that you have doubled your stake, and you quit happy.
It's easy to simulate something randomly happening with probability 1/38: just produce a random double uniformly distributed between 0 and 1. If this random double is less than 1/38, you win, and otherwise you lose.
Write your program into the framework below. I have inserted the random number generator that we have often used, along with its initialization.
You should count the number of bets needed until you either at least double your money or lose all your money, and print this number. Because of the way this is set up, when the program ends, you will either have 0 amount, or if you double your money, you may have considerably more than 2000 when you quit. Thus you should also print the final value of amount.
#include <stdio.h> /* for input/output */
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() in srand */
double rand_double();
int main() {
int amount = 1000;
int bets = 0;
srand((long)time(NULL));
while (amount >= 20 && amount < 2000) {
bets++;
if(rand_double() < 1.0/38.0)
amount = amount + 700;
else
amount = amount - 20;
}
printf("# bets: %i, final amount: %i\n", bets, amount);
}
double rand_double() {
return rand()/(double)RAND_MAX;
}
Results of 42 runs: # bets: 50, final amount: 0 # bets: 165, final amount: 2020 # bets: 158, final amount: 0 # bets: 302, final amount: 0 # bets: 50, final amount: 0 # bets: 90, final amount: 2080 # bets: 50, final amount: 0 # bets: 86, final amount: 0 # bets: 177, final amount: 2500 # bets: 86, final amount: 0 # bets: 40, final amount: 2360 # bets: 265, final amount: 2180 # bets: 374, final amount: 0 # bets: 223, final amount: 2300 # bets: 122, final amount: 0 # bets: 50, final amount: 0 # bets: 86, final amount: 0 # bets: 194, final amount: 0 # bets: 50, final amount: 0 # bets: 50, final amount: 0 # bets: 86, final amount: 0 | # bets: 194, final amount: 0 # bets: 265, final amount: 2180 # bets: 50, final amount: 0 # bets: 12, final amount: 2200 # bets: 11, final amount: 2220 # bets: 12, final amount: 2200 # bets: 147, final amount: 2380 # bets: 71, final amount: 2460 # bets: 48, final amount: 2200 # bets: 50, final amount: 0 # bets: 86, final amount: 0 # bets: 50, final amount: 0 # bets: 18, final amount: 2080 # bets: 89, final amount: 2100 # bets: 50, final amount: 0 # bets: 266, final amount: 0 # bets: 45, final amount: 2260 # bets: 40, final amount: 2360 # bets: 50, final amount: 0 # bets: 22, final amount: 2000 # bets: 57, final amount: 2020 |
Other versions of the loop in this problem:
Instead of:
while (amount >= 20 && amount < 2000) {
. . .
}
You could use some of these variations:
for (count = 0; amount >= 20 && amount < 2000; count++) {
. . .
}
while(1) {
. . .
if (!(amount >= 20 && amount < 2000)) break;
}
while(1) {
. . .
if (!(amount >= 20) || !(amount < 2000)) break;
}
while(1) {
. . .
if (amount < 20) || amount >= 2000) break;
}
while(1) {
. . .
if (amount < 20) break;
if (amount >= 2000) break;
}
Can also use:
for( ; ; ) in place of while(1)
Next I expanded the program to the following,
so that I could do 100000000 experiments at a time:
Further Discussion: The above experiment show that betting
in this fashion, for every $1000 we bet, we win about
$906 on the average. This means the house is getting about
9.4% of our money.
Similarly, if we play in Monte Carlo,
$1000 get us $952 back, so the house there is only getting about
4.8% of our money.
For each single bet, the house percentage is easy to compute.
In Las Vegas, if we bet $1 on a number, then with probability
1/38 we win $36 and the rest of the time we win nothing.
Thus our expected value after betting $1 is to end
up with 36/38 of a dollar, or $0.9473684.
So in Las Vegas the house percentage on this game is 5.26316%.
Our game above didn't achieve this percentage, since it was not
optimal play.
If you bet on an even number or on a color in Las Vegas,
you double your money with probability 18/38 and you lose
with probability 20/38, so here your expected value if you
bet $1 is 2*(18/38) = 36/38, which is the same answer as before.
In Monte Carlo, the expected value when you bet $1 is
36/37 = 0.97297297, so the house percentage is
2.7027%.
#include <stdio.h> /* for input/output */
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() in srand */
#define N 100000000
double rand_double();
int main() {
int amount = 1000;
int bets = 0;
int i;
int wins = 0;
int loses = 0;
double ave_win = 0.0;
rand((long)time(NULL));
for(i = 0; i < N; i++) {
amount = 1000;
bets = 0;
while (amount >= 20 && amount < 2000) {
bets++;
if(rand_double() < 1.0/38.0)
amount = amount + 700;
else
amount = amount - 20;
}
if (amount == 0) loses++;
else wins++;
ave_win += (double)amount/N;
}
printf("wins: %i, loses: %i, ave win: %0.5f\n", wins, loses, ave_win);
}
double rand_double() {
return rand()/(double)RAND_MAX;
}
Results of a run (100000000 experiments):
wins: 40836946, loses: 59163054, ave win: 906.42298
Results of runs in Monte Carlo (100000000 experiments, changing 1.0/38.0 to 1.0/37.0 above):
wins: 42864608, loses: 57135392, ave win: 951.83830
#include <stdio.h>
#define MAX(x,y) (x > y) ? (x) : (y)
int main() {
int a = 6;
int b = 4;
int max = MAX(a+6, b+7);
printf("The max of (a+6) and (b+7) is: %i\n", max);
}
#include <stdio.h>
int search(int x, int val[4][6], int *a, int *b);
int main() {
int val[4][6] = {{1, 8, 27, 64, 125, 216},
{1, 7, 19, 37, 61, 91},
{1, 6, 12, 18, 24, 30},
{1, 5, 6, 6, 6, 6}};
int x = 24;
int found;
int a, b;
found = search(x, val, &a, &b);
if (found == 1) {
printf("%i found\n", x);
printf("In main: the location of %i is (%i, %i)\n",
x, a, b);
}
else printf("%i NOT found\n", x);
}
int search(int x, int val[4][6], int *a, int *b) {
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 6; j++)
if (x == val[i][j]) {
*a = i; *b = j;
printf("In search: the location of %i is (%i, %i)\n",
x, i, j);
return 1;
}
return 0;
}
Results of a run:
In search: the location of 24 is (2, 4)
24 found
In main: the location of 24 is (2, 4)