Thread: Multidimensional arrays using chars

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    1

    Multidimensional arrays using chars

    Code:
    So i am creating two arrays a card and a corresponding suit i want to make it into an array that shows: {{A,S}{2,S},{3,S}...{A,C},{2,C}...}. The following is what I have so far. Any help would be great!
    
    #include <stdio.h> #include <stdlib.h> #include <time.h> #include <memory.h> int five(int player,int money){ int x,y,i,j; char deck_n[13][200],deck[13]; if ((player > 4)||(player < 2)){ printf("Players must be between 2-4. Please enter the right number"); } if (money <= 0){ printf("You must start with more then 0 dollars"); } srand(time( NULL )); x = rand()%13; y = rand()%4+1; const char *card[] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; const char *suit[] = {"S","C","D","H"}; //printf("%d,%d \n",x,y); //printf("%c%c \n",*card[x],*suit[y]); for(j = 0; j < 4;j++){ for(i = 0;i < 13;i++){ deck_n[i][j] = *card[i],*suit[j]; printf("%c \n",deck_n[i][j]); } } } main(){ five(2,100); }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you want 52 short strings of the form "AS" "2S" etc, then you need to start with
    char deck_n[52][3];

    > deck_n[i][j] = *card[i],*suit[j];
    The comma is not some kind of string concatenation operator.

    int ordinal = j * 13 + i;
    deck_n[ordinal][0] = *card[i];
    deck_n[ordinal][1] = *suit[j];
    deck_n[ordinal][2] = '\0';
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help in Multidimensional arrays
    By Satya in forum C Programming
    Replies: 5
    Last Post: 04-21-2015, 12:23 AM
  2. Multidimensional Arrays
    By BIGDENIRO in forum C Programming
    Replies: 6
    Last Post: 10-29-2013, 09:00 PM
  3. pointers of chars and arrays of chars Question
    By shiroaisu in forum C++ Programming
    Replies: 9
    Last Post: 08-09-2010, 10:42 AM
  4. Help with multidimensional arrays
    By Moose112 in forum C++ Programming
    Replies: 5
    Last Post: 11-21-2006, 04:28 AM
  5. Multidimensional arrays
    By cjoe77 in forum C++ Programming
    Replies: 4
    Last Post: 03-17-2003, 07:27 PM

Tags for this Thread