Question

In: Computer Science

I have this C program that has two functions to solve this Blackjack Solution. But i...

I have this C program that has two functions to solve this Blackjack Solution. But i need to use only function (not including main of course).

Is it possible to get to just one function outside of Main? #include<stdio.h> int isvalid(char card1, char card2); int sum(char card1, char card2); int sum(char card1, char card2) { int result=0; if(card1=='A' && card2=='A') return 12; if(card1<='9' && card1>='2') result+=card1-'0'; if(card1=='T' || card1 == 'J' || card1=='Q' || card1=='K') result+=10; if(card1=='A') result+=11; if(card2<='9' && card2>='2') result+=card2-'0'; if(card2=='T' || card2 == 'J' || card2=='Q' || card2=='K') result+=10; if(card2=='A') result+=11; return result; } int isvalid(char card1, char card2) { int i,cnt=0; char a[]="23456789ATJQK"; for(i=0;a[i];i++) { if(card1==a[i]) cnt++; if(card2==a[i]) cnt++; } if(cnt==2) return 1; return 0; } int main() { char c1, c2; scanf("%c %c",&c1,&c2); if(isvalid(c1,c2)) printf("%d\n",sum(c1,c2)); else printf("Invalid input"); return 0; }

Solutions

Expert Solution

Yes, it can be solved using one function.

The only function I have used is the 'invalid' function.

I have not modified your function but instead I have used a variable to trace the 'sum' function.

My isvalid function is:

int isvalid(char card1, char card2)
{
   int i,cnt = 0, result = 0;
   char a[]="23456789ATJQK";
   for(i=0;a[i];i++)
   {
       if(card1==a[i])
           cnt++;
       if(card2==a[i])
           cnt++;
   }
   if(cnt==2)
   {
       if(card1=='A' && card2=='A')
           return 12;
       if(card1<='9' && card1>='2')
           result+=card1-'0';
       if(card1=='T' || card1 == 'J' || card1=='Q' || card1=='K')
           result+=10;
       if(card1=='A')
           result+=11;
       if(card2<='9' && card2>='2')
           result+=card2-'0';
       if(card2=='T' || card2 == 'J' || card2=='Q' || card2=='K')
           result+=10;
       if(card2=='A')
           result+=11;
       if(result == 0) // if result is 0 but count is not zero, valid case but result 0,
           return -1; // return value is made different than invalid case, in invalid
                   // case the function returns 0 but when there is valid case but the
                   // result is 0 then that value has to be different than invalid case
                   // so -1 is returned in this case
       return result;
   }
   return cnt; // return value of invalid case is 0
}

The overall program is:

#include<stdio.h>

int isvalid(char card1, char card2)
{
   int i,cnt = 0, result = 0;
   char a[]="23456789ATJQK";
   for(i=0;a[i];i++)
   {
       if(card1==a[i])
           cnt++;
       if(card2==a[i])
           cnt++;
   }
   if(cnt==2)
   {
       if(card1=='A' && card2=='A')
           return 12;
       if(card1<='9' && card1>='2')
           result+=card1-'0';
       if(card1=='T' || card1 == 'J' || card1=='Q' || card1=='K')
           result+=10;
       if(card1=='A')
           result+=11;
       if(card2<='9' && card2>='2')
           result+=card2-'0';
       if(card2=='T' || card2 == 'J' || card2=='Q' || card2=='K')
           result+=10;
       if(card2=='A')
           result+=11;
       if(result == 0) // if result is 0 but count is not zero, valid case but result 0,
           return -1; // return value is made different than invalid case, in invalid
                   // case the function returns 0 but when there is valid case but the
                   // result is 0 then that value has to be different than invalid case
                   // so -1 is returned in this case
       return result;
   }
   return cnt; // return value of invalid case is 0
}
int main()
{
   char c1, c2;
   scanf("%c %c",&c1,&c2);
   int a = isvalid(c1,c2);
   if(a)
   {
       if(a != -1)
           printf("%d\n",a);
       else
           printf("0\n");
   }
   else
       printf("Invalid input");
   return 0;
}

The sample of the program is:

The sample output of the code is:

The program can be shortened further but I have tried to keep it simple for your better understanding.


Related Solutions

Write a C++ program that scores a blackjack hand. In blackjack, a player receives from two...
Write a C++ program that scores a blackjack hand. In blackjack, a player receives from two to five cards. The cards 2 through 10 are scored as 2 through 10 points each. The face cards --- jack, queen, and king ---- are scored as 10 points. The goal is to come as close to a score of 21 as possible without going over 21. Hence, any score over 21 is called “busted”. The ace can count as either 1 or...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
Need the correct line of code for c program to solve  this equation . i is a...
Need the correct line of code for c program to solve  this equation . i is a value that the program all ready calculates from user imput. t i = (x*1000)+(y*500)+(z*250
create a C++ program where you have 2 functions with two parameters. Limit the first function...
create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).
hey I have this program written in c++ and I have a problem with if statement...
hey I have this program written in c++ and I have a problem with if statement {} brackets and I was wondering if someone can fix it. //Name: Group1_QueueImplementation.cpp //made by ibrahem alrefai //programming project one part 4 //group members: Mohammad Bayat, Seungmin Baek,Ibrahem Alrefai #include <iostream> #include<stdlib.h> using namespace std; struct node { string data; struct node* next; }; struct node* front = NULL; struct node* rear = NULL; struct node* temp; void Insert() {     string val;    ...
I am trying to solve a c++ problem over c strings where I have to input...
I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code. Assignment Instructions Write a...
C program simple version of blackjack following this design. 1. The basic rules of game A...
C program simple version of blackjack following this design. 1. The basic rules of game A deck of poker cards are used. For simplicity, we have unlimited number of cards, so we can generate a random card without considering which cards have already dealt. The game here is to play as a player against the computer (the dealer). The aim of the game is to accumulate a higher total of points than the dealer’s, but without going over 21. The...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to...
Write an Html Page that uses JavaScript Program to make a Blackjack Game. I need to write an html file (P5.html) that uses JavaScript program to create a Blackjack game. 1. Blackjack Games Rules: a. The object of the game is to "beat the dealer", which can be done in a number of ways: • Get 21 points on your first two cards (called a blackjack), without a dealer blackjack; • Reach a final score higher than the dealer without...
I have listed below two functions. Please add these two functions to StudentMath class. Test it...
I have listed below two functions. Please add these two functions to StudentMath class. Test it from the Test class public int divideByZero(int gradeA, int gradeB, int gradeC) { int numberOfTests = 0; int gradeAverage = 0; try { gradeAverage = (gradeA + gradeB + gradeC)/numberOfTests; } catch(ArithmeticException ex) { System.out.println("Divide by zero exception has occured" + ex.getMessage()); } return gradeAverage; } public int ArrayOutOfBoundEception() { int i = 0; try { // array of size 4. int[] arr =...
Hello, I very stuck on this c++ blackjack project and was wondering if anyone can offer...
Hello, I very stuck on this c++ blackjack project and was wondering if anyone can offer help and guidance on this subject can someone help he with part 4 i have no idea how to approach the problem and how to do it I have compeleted a few parts to this project they are listed below Part 1 – Displaying Cards Write a function to display (print) a card. sample program output void displayCard(int card) Prints the name of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT