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. */
}
#include <stdio.h>
/* give a prototype of the function printp on the next line */
int main() {
int p[] = {2, 3, 5, 7, 11, 13, 17};
/* call the function printp on the next line */
}
/* give the definition of the function printp below */
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. 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() {
srand((long)time(NULL));
}
double rand_double() {
return rand()/(double)RAND_MAX;
}
#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 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;
found = search(x, val );
if (found == 1) printf("%i found\n", x);
else printf("%i NOT found\n", x);
}
int search(int x, int val[4][6] ) {
int i, j;
for (i = 0; i < 4; i++)
for (j = 0; j < 6; j++)
if (x == val[i][j]) {
printf("The location of %i is (%i, %i)\n",
x, i, j);
return 1;
}
return 0;
}
Results of a run:
The location of 24 is (2, 4)
24 found