User Tools

Site Tools


Sidebar

projects

ntr0 (due 20210825)
pct1 (bonus; due 20210819)
wcp1 (due 20210818)
uom0 (due 20210901)
pct2 (due 20210825)
wcp2 (due 20210825)
pnf0 (due 20210901)
pct3 (bonus; due 20210901)
wcp3 (due 20210901)
pnf1 (due 20210908)
pct4 (due 20210908)
wcp4 (due 20210908)
pnf2 (due 20210915)
gfo0 (due 20210915)
pct5 (bonus; due 20210915)
wcp5 (due 20210915)
pnf3 (due 20210922)
pct6 (due 20210922)
wcp6 (due 20210922)
saf0 (due 20210929)
pct7 (bonus; due 20210929)
wcp7 (due 20210929)
saf1 (due 20211006)
pct8 (due 20211006)
wcp8 (due 20211006)
saf2 (due 20211020)
pct9 (bonus; due 20211020)
bwp1 (bonus; due 20211020)
wcp9 (due 20211020)
fom0 (due 20211027)
gfo1 (due 20211027)
pctA (due 20211027)
wcpA (due 20211027)
pctB (bonus; due 20211103)
wcpB (due 20211103)
mpg0 (due 20211110)
pctC (due 20211110)
wcpC (due 20211110)
gfo2 (due 20211117)
pctD (bonus; due 20211117)
pctE (bonus; due 20211201)
bwp2 (bonus; due 20211201)
EoCE (due 20211209)
haas:fall2021:discrete:projects:yol0

Corning Community College

CSCS2330 Discrete Structures

Project: YOUR OPTIMIZED LOGIC (yol0)

Objective

I've seen far too many tendencies to “plug and chug”, as if programming is like reading recipes from a book. This project will focus on your ability to creatively solve programs and craft algorithms, according to various constraints.

Algorithms will rely on both mathematical and logical operations.

Decks of Cards

The standard deck of playing cards (for our purposes) possesses 52 distinct cards, spread out among 4 different suits.

The suits are (in this order):

  • clubs
  • diamonds
  • hearts
  • spades

The face values on the cards are (in this order):

  • Ace
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • Jack
  • Queen
  • King

13 face values, 4 suits, 13 x 4 = 52.

For the purposes of any programs we write, we are going to display the cards as 2- or 3-symbol strings, as such:

  • 6 of Hearts: “6H”
  • Jack of Spades: “JS”
  • Ace of Diamonds: “AD”
  • 10 of Clubs: “10C”

With the differing values used by the suits and face values (ie, by name it isn't immediately linear), you are going to need to “flatten” the data so that it can be represented by a single numeric value (ranging from 1-52).

Program

Your task is to write a program that processes cards.

It should have a menu allowing the selection of the following functions, and to prompt the user for the appropriate input, which gets passed into the selected function, displaying the resulting output.

It is to have the following functions:

  • char *num2name_if(unsigned char);
  • char *num2name_tern(unsigned char);
  • char *num2name_algo(unsigned char);
  • unsigned char name2num_if(char *);
  • unsigned char name2num_tern(char *);
  • unsigned char name2num_algo(char *);

And yes, you must implement all six functions, according to their implementation requirements. Any arrays created must be compatible for use by all functions you implement (no custom array per function).

There are 2 actual functions, the num2name() and its reverse, name2num(); however, to aid in your explorations, I'd like for you to create 3 different implementations of each, restricting yourself to the following:

  • _if functions make use of your standard selection statements (likely how you'd approach it by default)
  • _tern functions will only make use of ternary operators (no if()/switch() statements)
  • _algo functions will only make use of math/logic operations (no if()/switch() statements, no ternary operators)

You can (and should) make use of arrays.

I'd recommend 2 arrays:

  • char suit[], containing the 'C', 'D', 'H', and 'S' values
    • feel free to use #define's, perhaps:
      • #define CLUB 0
      • #define DIAMOND 1
      • #define HEART 2
      • #define SPADE 4
  • char face[][] (array of strings), containing the “A”, “2”, “3”, …, “8”, “9”, “10”, “J”, “Q”, “K” values (in that order)

You can use static arrays (the brackets) or dynamically allocated arrays; I'd caution against just using what you are comfortable with… you'll want to use what is best for the algorithm/program.

You should strive for optimized code, but if you can make something work by “wasting” or “skipping” an occasional space, by all means.

Example: Using num2name()

If we had the number 17 (out of 1-52), and we passed it to num2name(), with the specified suit ordering of Clubs, Diamonds, Hearts, and Spades, we should get: 4 of Diamonds

The actual return value from num2name() would be the string: “4D”

Another example, the number 34; we should get: “8H”

Or: 1; we should get: “AC” (Ace of Clubs)

Example: using name2num()

If we had “10C”, we should get: 10

If we had “QH”, we should get: 38

If we had “6D”, we should get: 19

Helpful hints

Remember that linearity can be of great value (especially if you cannot divert program flow with some conditional statement).

Finding ways to conveniently arrange or pack your data can make for a much, much easier implementation. But that requires planning, crafting, and maybe even a bit of creative brainstorming.

Do remember your third grade math.

Do consider making use of bitwise logic; by the way, your operators are:

  • & bitwise AND
  • | bitwise OR
  • ^ bitwise XOR
  • » logical right shift
  • « logical left shift
  • ~ bitwise NOT

Remember what a string is (a set of characters), what a character is (merely a number), and how a number can be expressed/manipulated.

Note the required parameter/return data types. Make sure you implement with these in mind. Type cast if necessary, but all input to (parameters) and output from (return value) your functions must be of those types.

Submission

Project Submission

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

lab46:~/src/discrete/yol0$ submit discrete yol0 yol0.c
Submitting discrete project "yol0":
    -> yol0.c(OK) 

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

haas/fall2021/discrete/projects/yol0.txt · Last modified: 2019/10/06 15:17 by 127.0.0.1