printf - How would I assign the values of 11,12, and 13 to all equal 10 without using logical operators, relational operators, or selection constructs in C? -


i'm supposed make program simple form of blackjack. program randomly draws 4 cards standard deck of 52, , card number should displayed along card value. (for example, if card number 43 has value of 4) cards 2-10 worth given value, ace worth 1, face cards (11-13, jack, queen, king) worth 10. when total exceeds 21 program should display 'b' after total. i'm having trouble last 2 parts. can't figure out how face cards equal 10, , display 'b' without using logical operators, relational operators, or selection constructs such if statement. appreciated! thanks! here code have far:

#include<stdio.h> #include<math.h> #include<stdlib.h>  int seedvalue(); int card(); int cardvalue(int); int cardvalue2(int); int cardvalue3(int); int cardvalue4(int); void printstatements(int, int, int, int, int, int, int, int);  int main() {   int seed;   int cardnum1;   int cardnum2;   int cardnum3;   int cardnum4;   int value1;   int value2;   int value3;   int value4;    seed = seedvalue();    srand(seed);    cardnum1 = card();   cardnum2 = card();   cardnum3 = card();   cardnum4 = card();    value1 = cardvalue(cardnum1);   value2 = cardvalue2(cardnum2);   value3 = cardvalue3(cardnum3);   value4 = cardvalue4(cardnum4);    printstatements(cardnum1,cardnum2,cardnum3,cardnum4,value1,value2,value3,value4);    return(0); }  int seedvalue() {   int seed;    printf("enter seed value: ");   scanf("%d",&seed);    return(seed); }  int card() {   int cardval;    cardval = (rand() % 52) + 1;    return(cardval); }  int cardvalue(int cardnum1) {   int val1;     val1 = cardnum1 % 13;    return(val1); }  int cardvalue2(int cardnum2) {   int val2;    val2 = cardnum2 % 13;    return(val2); }  int cardvalue3(int cardnum3) {   int val3;    val3 = cardnum3 % 13;    return(val3); }  int cardvalue4(int cardnum4) {   int val4;    val4 = cardnum4 % 13;    return(val4); }  void printstatements(int cardnum1, int cardnum2, int cardnum3, int cardnum4, int value1, int value2, int value3, int value4) {   printf("1. card generated: %d card value: %d\n",cardnum1,value1);   printf("2. card generated: %d card value: %d\n",cardnum2,value2);   printf("3. card generated: %d card value: %d\n",cardnum3,value3);   printf("4. card generated: %d card value: %d\n",cardnum4,value4);    printf("total points generated is: %d\n",value1 + value2 + value3 + value4); } 

as noted in comment, there nothing wrong syntax used in writing program, quite bit longer write month now. taking suggestions , making use of couple of arrays dramatically cut down on verbosity of code while still retaining information desired print.

in same manner, reducing number of functions provide same logic helps cut down on line in code (bad if getting paid line...)

to question of keeping card values equal 10 face cards, can handled in cardvalue function simple test after reduce value between 1 - 13. simple if (value >= 10) value = 10; insure face cards have value of 10.

with changes made, while still lengthy, bit shorter original. give try , let me know if have questions:

#include<stdio.h> #include<stdlib.h>  #define suits 4  int seedvalue(); int card(); int cardvalue(int); void printdeal (int *cards, int *values);  int main() {     int cardnum[suits] = {0};     int cardval[suits] = {0};     int = suits;      srand (seedvalue());      while (i--) {         cardnum[i] = card();         cardval[i] = cardvalue (cardnum[i]);     }      printdeal (cardnum, cardval);      return 0; }  int seedvalue() {     int seed;      printf ("enter seed value: ");     scanf ("%d",&seed);      return seed; }  int card () {   return (rand() % 52) + 1; }  int cardvalue (int cardnum) {     int val = cardnum % 13;      return val > 10 ? 10 : val; }  void printdeal (int *cards, int *values) {     int i, pts = 0;      putchar ('\n');  /* make nice */      (i = 0; < suits; i++) {         printf (" card[%2d] : generated: %2d  value: %2d\n",                 + 1, cards[i], values[i]);         pts += values[i];     }     printf ("\ntotal points generated : %d\n\n", pts); } 

use/output

$ ./bin/cards enter seed value: 21   card[ 1] : generated: 11  value: 10  card[ 2] : generated:  3  value:  3  card[ 3] : generated: 52  value:  0  card[ 4] : generated: 17  value:  4  total points generated : 17 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -