CSC 101 Lab Notes Week 8
Arrays
The following are the tasks for this lab:
Hardcopy listings for the card-play example files were part of Lecture Notes
Week 7, Part 2. Hardcopy listings for the last two examples are attached here.
Example file deck-as-const-string-array.cpp
////
//
// This program declares a card deck as a constant string array. It uses the
// DumpDeck function defined in ./card-play.cpp. Hence, this program needs to
// be compiled with that file, as follows:
//
// CC +a1 deck-as-const-string-array.cpp card-play.cpp -o deck-as-const
//
// Note the "+a1" argument to CC. This is necessary with the polylog compiler
// when a program contains any aggregate array initialization. The topic of
// aggregate array initialization is covered on pages 601-602 and 671-672 of
// the book.
//
////
#include <iostream.h>
#include "card-play.h"
int main() {
char deck[52][4] = { // Deck of 52 cards as strings. Note padding
" AC", // of blank in all but 10's. This allows the
" AD", // suit to be accessed consistently as
" AH", // deck[i][2], for any card i.
" AS",
" 2C",
" 2D",
" 2H",
" 2S",
" 3C",
" 3D",
" 3H",
" 3S",
" 4C",
" 4D",
" 4H",
" 4S",
" 5C",
" 5D",
" 5H",
" 5S",
" 6C",
" 6D",
" 6H",
" 6S",
" 7C",
" 7D",
" 7H",
" 7S",
" 8C",
" 8D",
" 8H",
" 8S",
" 9C",
" 9D",
" 9H",
" 9S",
"10C",
"10D",
"10H",
"10S",
" JC",
" JD",
" JH",
" JS",
" QC",
" QD",
" QH",
" QS",
" KC",
" KD",
" KH",
" KS"
};
DumpDeck(deck);
return 0;
}
////
//
// This program does some simple experimentation with the rand function, with
// an eye towards a deck shuffling function. This comment is purposely vague,
// since the intention of this example is to stimulate your thinking.
//
// Here's a UNIX trick to help test this program. Use the UNIX sort function
// as follows:
//
// rand-tests | sort -n
//
// The '|' symbol is called the UNIX "pipe" operator. What it does here is
// "pipe", i.e., send, the output of this program through the UNIX sort
// command. The "-n" argument to sort tells it to sort numerically. You can
// read more about UNIX pipes and sorting in the Hahn book.
//
////
#include "Boolean.h" // Textbook definition of Boolean type
#include <iostream.h> // The regularly used lib for cin and cout
#include <stdlib.h> // Lib containing the rand and srand functions
#include <time.h> // Lib containing the time function
int main() {
int rands[52]; // Array of 52 random numbers, between 0 and 51
// inclusive
Boolean already_used[52]; // Array indicating if a random number between
// 0 and 51 has already been chosen
int i; // Loop and array index
int r; // Next generated random number
int dups = 0; // Counter for number of thrown away duplicates
//
// Seed the random sequence with the value of the system time function.
//
srand(time(0));
//
// Init all already_used elements to false to indicate that each is
// initially unused.
//
for (i = 0; i < 52; i++) {
already_used[i] = false;
}
//
// For each elem of rands, store an unused random number in it.
//
for (i = 0; i < 52; i++) {
//
// Generate the next unused random number in the 0-51 sequence by
// finding an unused slot in already_used.
//
for (r = rand() % 52; already_used[r]; r = rand() % 52) {
dups++;
}
//
// Put the unused number in the next successive slot of rands and mark
// the already used slot for that number as used.
//
rands[i] = r;
already_used[r] = true;
}
//
// Dump out the rands array.
//
for (i = 0; i < 52; i++) {
cout << rands[i] << endl;
}
cout << endl;
//
// For information purposes, dump out the number of thrown away
// duplicates. This number isn't really important, it's just helps clarify
// what this algorithm is doing.
//
cout << dups << " dups" << endl;
return 0;
}