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 values of the two cards in the hand
  21.  
  22. for (int i = 0; i < 2; i++) {
  23. switch (handArray[i][0]) {
  24. case 'T':
  25. case 'K':
  26. case 'Q':
  27. case 'J':
  28. cardValues[i]=10;
  29. break;
  30. case 'A':
  31. if (i==1 && cardValues[0]==11){
  32. cardValues[1]=1;
  33. }
  34. else{
  35. cardValues[i]=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. if (i==0){
  47. int cardValue1=card1-'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)
  48. cardValues[i]=cardValue1;
  49. }
  50. else if (i==1){
  51. int cardValue2=card2-'0';
  52. cardValues[i]=cardValue2;
  53. }
  54. break;
  55. // default:
  56. // printf("Not a valid card");
  57. // break;
  58. };
  59. };
  60.  
  61. int totalPoints=cardValues[0]+cardValues[1]; //calculating total points
  62.  
  63. if (cardValues[0]==0 | cardValues[1]==0){
  64. printf("There was an invalid card in the hand. Caution should be applied when interpreting the total points calculation.");
  65. };
  66.  
  67. return totalPoints;
  68. };
  69.  
  70. int main() {
  71. int totalPoints= blackJackValue('A','#');
  72. printf("Total Points Associated with Hand: %i\n", totalPoints);
  73. return 0;
  74. };
Success #stdin #stdout 0.01s 5308KB
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: 11