User Tools

Site Tools


Sidebar

projects

pct0 (bonus; due 20230823)
wcp1 (due 20230823)
abc0 (due 20230830)
btt0 (due 20230830)
pct1 (bonus; due 20230830)
pct2 (due 20230830)
wcp2 (due 20230830)
mpg0 (due 20230906)
pct3 (bonus; due 20230906)
wcp3 (due 20230906)
pct4 (due 20230913)
ttb0 (due 20230913)
wcp4 (due 20230913)
pct5 (bonus; due 20230920)
ttb1 (due 20230920)
wcp5 (due 20230920)
dap0 (due 20230927)
gfo0 (due 20230927)
pct6 (due 20230927)
wcp6 (due 20230927)
cgf0 (due 20231004)
pct7 (bonus; due 20231004)
wcp7 (due 20231004)
bwp1 (bonus; due 20231018)
pct8 (due 20231018)
wcp8 (due 20231018)
yol0 (due 20231018)
bjm0 (due 20231025)
pct9 (bonus; due 20231025)
wcp9 (due 20231025)
bjm1 (due 20231101)
gfo1 (due 20231101)
pctA (due 20231101)
wcpA (due 20231101)
pctB (bonus; due 20231108)
set0 (due 20231108)
wcpB (due 20231108)
mor0 (due 20231115)
pctC (due 20231115)
wcpC (due 20231115)
bwp2 (bonus; due 20231129)
pctD (bonus; due 20231129)
wcpD (bonus; due 20231129)
gfo2 (due 20231206)
pctE (bonus; due 20231206)
wcpE (bonus; due 20231206)
EoCE (due 20231214)
haas:fall2023:discrete:projects:yol0old

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/fall2023/discrete/projects/yol0old.txt · Last modified: 2019/10/06 15:17 by 127.0.0.1