Question: Code in C++ please C++'s algorithms work on iterators and/or iterator ranges. There are many algorithms in and in C++. Calculating the score of cards

Code in C++ please

C++'s algorithms work on iterators and/or iterator ranges. There are many algorithms in and in C++. Calculating the score of cards in a container amounts to iterating from the beginning to the "end" of the container summing up their score. (NOTE: In this assignment that each card contributes to the score only on its own.) While this can be manually done with a for loop, there is an algorithm, std::accumulate(), capable of "adding" up elements in an iterator range [first,last).

The score of a container cards, i.e., a "hand", is to be calculated as follows:

  • aces are worth 1 or 11,
  • jacks, queens, and kings are worth 10,
  • card::invalid is worth 0 (obviously and such should never be in a deck or hand),
  • otherwise the card is worth its face value (i.e., 2 to 10).

(Some might recognize that these are the values of cards in the game of Blackjack.)

Since aces can be worth 1 or 11, in this assignment you are to compute the score:

  • by computing the lowest possible score,
  • by computing the highest possible score, and,
  • returning both from this function.

Thus, to calculate the score, you want to return two values from a function. You are not permitted to do this indirectly by passing arguments by reference to the function. Instead you will return the results in a single object from the function. This means your calc_score() function could be prototyped a one of:

auto calc_score(cards const& cs) -> std::pair;

or as:

auto calc_score(cards const& hand) -> cards;

(These prototypes are using C++11's function suffix notation.)

Returning cards in a type cards value would work as there can be 0 or more elements in the container --the caller can check how many results there are. However, in this assignment you will return a std::pair object for these reasons:

  • all of C++'s associative "map" containers store their elements as std::pair where Key and Value are the types of those objects (so it is good to know how to use std::pair),
  • using std::pair demonstrates how to store two values in a single object, and,
  • using std::pair allows one to learn how to access the elements in std::pair.

(ASIDE: std::pair is legacy (but will remain in the language) and has been made to be essentially compatible with std::tuple, i.e., a tuple containing two elements of types A and B. Tuples are more general however supporting 0 to N elements of distinct types.)

Write the code in calc_score() as follows:

  1. size_t low = accumulate(hand. begin(), hand.end(), size_t{}, LOW_SUM_OP);
  2. size_t high = accumulate(hand.begin(), hand.end(), size_t{}, HIGH_SUM_OP);
  3. return { low, high };

where LOW_SUM_OP and HIGH_SUM_OP are functions, lambda functions, or function objects (of your choosing) to calculate the low and high score in a hand respectively. (Try using lambda functions here!) The signature of LOW_SUM_OP and HIGH_SUM_OP are what std::accumulate requires when a binary operator is passed in. The first argument is the current sum, and, the second argument is the next element in the container to add to the current sum. Thus, the return value of both LOW_SUM_OP and HIGH_SUM_OP is the first argument + whatever score the current card is. This also means the first argument will be std::size_t const& and the second argument is card const&.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!