fork download
  1. #include <stdio.h>
  2.  
  3. //**************************************************************
  4. // Function: blackJackValue
  5. //
  6. // Purpose: Calculate the total points associated with the black jack hand
  7. //
  8. // Parameters:
  9. //
  10. // card1 - the first card in the hand
  11. // card2 - the second card in the hand
  12. //
  13. // Returns: value of the hand (in points)
  14. //
  15. //**************************************************************
  16.  
  17. int blackJackValue (char card1, char card2){
  18.  
  19. char handArray[2][1] = {card1, card2}; //creating an array which holds the two cards associated with a specific hand. Each card is represented by a single character
  20. int cardValues[2]= {0,0}; //initializing an array that will hold the points associated with the two cards in the hand
  21.  
  22. for (int i = 0; i < 2; i++) { //looping through the cards in the hand
  23. switch (handArray[i][0]) { //checking the card in the hand
  24. case 'T':
  25. case 'K':
  26. case 'Q':
  27. case 'J':
  28. cardValues[i]=10; //if the card is 'T','K','Q' or 'J', then it is associated with a value of 10 points
  29. break;
  30. case 'A':
  31. if (i==1 && cardValues[0]==11){
  32. cardValues[1]=1; //if the second card in the hand is an ACE and the first card was also an ACE, then the points associated with the second card is 1
  33. }
  34. else{
  35. cardValues[i]=11; //if the card is an ACE but either (1) the first card in the hand, or (2) the second card in the hand, but first card was not ACE then points=11
  36. }
  37. break;
  38. case '2':
  39. case '3':
  40. case '4':
  41. case '5':
  42. case '6':
  43. case '7':
  44. case '8':
  45. case '9':
  46. int cardValue2=handArray[i][0]-'0'; //converting the card value from a character digit to an integer. To do this subtracting the ASCII value for '0' from the ASCII value from the character digit (e.g., '3') to return the ASCII value for the numeric digit (e.g. 3)
  47. cardValues[i]=cardValue2; //after character digit has been converted to an integer, setting the appropriate value in the cardValues array equal to it
  48. break;
  49. };
  50. };
  51.  
  52. int totalPoints=cardValues[0]+cardValues[1]; //calculating total points
  53.  
  54. if (cardValues[0]==0 | cardValues[1]==0){
  55. printf("There was an invalid card in the hand. Caution should be applied when interpreting the total points calculation.");
  56. };
  57.  
  58. return totalPoints;
  59. };
  60.  
  61. int main() {
  62. int totalPoints= blackJackValue('Z','4');
  63. printf("Total Points Associated with Hand: %i\n", totalPoints);
  64. return 0;
  65. };
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
There was an invalid card in the hand. Caution should be applied when interpreting the total points calculation.Total Points Associated with Hand: 4