Problems

Problem

Assume we are interested in describing the distribution of a set of test scores: s1, ..., sn. The center of the distribution is given by the mean:

mean = (s1 + ... + sn)/n

The spread of the scores is given by the standard deviation:

sd = sqrt(variance);

The variance is the average variation:

variance = (v1 + ... + vn)/n

The variation of the ith score is the square of its difference from the mean:

vi = (si – mean)2

Write a program that prompts the user for a file of test scores, then computes and prints the mean, minimum score, maximum score, and standard variation.

Problem

Write a program that prompts the user for the rows of a 3 X 3 matrix, then returns the determinant. (Check your math books for the definition of determinant.)

Problem

Assume

sizeof(int) = sizeof(int*) = sizeof(int**) = 4 bytes

Assume the following declarations are made:

int nums[3] = {100, 200, 300}, *p = &nums[1], **q = &p;

Assume:

p = 5000 and q = 6000

Compute the values produced by the following expressions:

*p =
*p + 1 =
*(p + 1) =
**q =
*q =
&nums[0] =
p[1] =

Problem: Example: Blackjack

Write a program that plays Blackjack with the user. After each game the program displays the number of draws, user wins, and computer wins, then asks the user if the user wants another game.

A game of Blackjack consists of dealing a hand of two random cards to the user , then printing the value of the hand. The value is the sum of the card values, where:

ACE = 1 or 11
TWO = 2
etc.
NINE = 9
TEN = JACK = QUEEN = KING = 10

The user is repeatedly asked if he wants another card until he "holds" or until the value of the hand exceeds 21, in which case the game is over and the user looses.

If the user holds, then the computer deals two random cards to itself and displays the sum. The computer continues to deal itself a card until the sum of the cards is greater than or equal to 17. If the sum is greater than 21 or less than the sum of the user's hand, then the computer looses, if the sums are the same, then the game ends in a draw. Otherwise, the computer wins.

Use top down design. Start by defining a short main()function, then move on to define coherent, loosely coupled supporting functions.

Representing cards and decks provides an opportunity to introduce some new C++ features:

Decks as Arrays

A deck is simply any array of 52 cards:

typedef Card Deck[52];

Here's a tricky way to initialize a deck. Note the conversion operators used to change integers into suits and values:

void init(Deck d)
{
   for(int i = 0; i < 4; i++)
      for(int j = 0; j < 13; j++)
      {
         int n = 13 * i + j; // 0 <= n < 52
         d[n].suit = Suit(i);
         d[n].value = Value(j);
         d[n].dealt = false;
      }
}

Shuffling a deck is just a matter of clearing the dealt flags:

void shuffle(Deck d)
{
   for(int i = 0; i < 52; i++)
      d[i].dealt = false;
}

Here's a little utility for determining the number of undealt cards in a deck:

int undealtCards(Deck d)
{
   int result = 0;
   for(int i = 0; i < 52; i++)
      if (!d[i].dealt) result++;
   return result;
}

Random Numbers

To pick a card, generate a random number between 0 and 51. If the card at that position is dealt, then increment the number modulo 52 until an undealt card is found:

Card pickCard(Deck d)
{
   int i = rand() % 52;
   int j = i;
   while (d[j].dealt)
   {
      j = (j + 1) % 52;
      if (i == j) error("No more cards");
   }
   d[j].dealt = true;
   return d[j];
}

You'll need to seed the random number generator at the beginning of main:

#include <time.h> // not standard
// etc.
int main()
{
   srand(time(0));
   // etc.
   return 0;
}

Problem

Using standard generic algorithms, write a program that perpetually prompts the user for a string, then determines if the string is a palindrome or not.

Problem

Using standard generic algorithms, write a program that manages a dynamic array of numbers and responds to the following commands:

put N // puts a number into the array
rem N // removes first occurance of N from array
rem* N // remove all occurances of N from array
count N // # of occurances of N in array
sum  // sum of all nums in array
prod  // product of all nums in array
clear // clear array
get i // print ith entry of the array
find N // prints index of first occurance of N in array