The program we looked at in lecture today

 

Pointers and C-strings

•      C-strings are arrays of characters, terminated by a NULL

•      So pointer notation can be used with the characters in strings.

•      We will look at some code where we define two strings,

–    one using array notation,

–    one using string notation.

•      The main difference is that

–    with array notation the array name is a pointer constant,

–     with string notation it is a pointer variable

 

Example of C-strings as pointers

•      int main( )
   {
   char str1[ ] = "Defined as an array";
   char* str2 = "Defined as a pointer";

   cout << str1 << endl;   
// display both strings
   cout << str2 << endl;

// str1++;                  // can't do this; str1 is a constant
   str2++;                 
// this is OK, str2 is a pointer

   cout << str2 << endl;    
// now str2 starts "efined...“
   return 0;
   }

Arrays of pointers

•      If c-strings are put into arrays, you really have an array of pointers,

–    This is since every C-string is an array of characters

–    Each entry in an array of strings is actually a pointer to the first character of a string

–    Example:
char * suit[4] = {“Spades”, “Hearts”,“Diamonds”, “Clubs”};

–    The suit[4] indicates an array of 4 elements

–    The char* indicates that each element is an array of characters, using pointer notation.

 

You lab using arrays of c-strings

•      In lab tomorrow, you will be dealing with a deck of cards

•      The information about the deck is kept in three arrays

–    Two are arrays of c-strings, the other is a 2-D array.

•      We will look at some syntax and constructs you may not have seen yet in C++

–    This includes:

•    2-D arrays

•    Random number generation

•    Formatting numbers with the iosflags

 

Double-subscripted arrays

•      2-D arrays are used
to store tables

•      Every element is
identified by row and col

•      2-D arrays are processed using a nested for loop
  int A[3][4] = {{ 3,4,5,6},{9,8,7,6},{2,9}};
  for (row =0; row < 3; row++)
  {    for col = 0; col < 4; col ++)
             cout << A[row][col] << ‘ ‘;
       cout<<endl;
  }

•      When the array is initialized, if there are not enough initializers for a given row, it is filled with zeros  

 

Random number generation

•      The standard library function rand returns an unsigned integer between 0 some max which is system dependent.

–    Max is at least 32767, so it is always a large number

•      Most of the time, you will want a random number between zero and some smaller number

•      You use the mod function to accomplish this.

•      rand( ) % 10 will return random numbers between 0 and 9 (including endpoints)

–    This is called scaling. The number ten is the scale factor

 

Seeding the rand function

•       rand( ) really produces pseudo random numbers.

•      You will get the same sequences of “random numbers” every time it is called if it doesn’t start with a different “seed”

•      The srand(seed_int) function provides the seed.

•      In order to get a different seed each time it is called, time is often used as the argument.

•       srand(time(0)) causes the computer to read its clock to get the seed value;

–    The time function with the zero argument returns the current “calendar time” in seconds

–    It only needs to be called once in a program

•      You must #include <ctime> to use srand

 

Formatting numbers with the ios flags 

•      To justify data in a field, you set the iosflags

•      The column width should be set first

•      Example

•       cout << setw( 5 ) << setiosflags( ios::right )
           << wFace[ column ] << " of “
           << setw( 8 ) << setiosflags( ios::left )

           
<< wSuit[ row ]
           << ( card % 2 == 0 ? '\n' : '\t' );

 

•      You must #include <iomanip> to format this way

•      The last line of code could be written this way
 
if (card % 2 = = 0)  cout << ‘\n’;  //(newline)
  else  cout  << ‘\t’                        //(tab)
  

•        

The program

•      This program gets the job done, but is inefficient.

•      The two string arrays are used output the data

•      The 2-D array actually holds the cards in the deck

–    The position in the array determines what card it holds

•    The rows are the suit of the card

•    The columns are the numeric value of the card

–   Of course, one is an ace, eleven is a jack, etc.

–    The contents of the array determine the card’s position in the deck.

•    the contents of the deck is initialized to zero

•    The shuffle function determines which card goes where using a random number generator

–   It is quite inefficient. Changing this to be more efficient is one part of your lab.                          
     

What to do in the lab

•      Modify the shuffle and deal functions to be more efficient.

–    A strategy will be given in the notes for the lab.

•      Modify the program so that the card dealing function deals a five-card poker hand. 

•      Then write functions to accomplish each of the following: 

–    Determine if the hand contains a pair

–    Determine if the hand contains two pair

–    Determine if the hand contains three of a kind

–    Determine if the hand contains four of a kind

–    Determine if the hand contains a flush (all five cards one suit)

–    Determine if the hand contains a straight (five cards of consecutive face values)

•      Modify main to deal a hand, then output the cards in the hand, and tell if what it contains (i.e. a pair, etc.)

 

Modifying the shuffle functions

•      As it now stands, the shuffle function can lead to indefinite postponement.

–    It may take almost forever to find the empty spot to put the 52nd card.

•      A better way

–    Initialize the deck so that deck[0][0] contains 1, deck[0][1] contains 2, etc.  (see overhead)

–    Modify the shuffle function to loop through every element in the array, swapping with a randomly selected element of the array

–    Print the resulting array to be sure your shuffle function is working well

–    You may want to shuffle several times before dealing.

 

Modifying the deal function

•      After the dealing function locates and deals a card, it continues searching though the remainder of the deck

•      Modify the function so that once a card is dealt, no further attempts are made to match that card number, and the function immediately proceeds with dealing the next card.