In: Computer Science
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; }
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.