如何初步化卡片组的3D阵列

时间:2014-09-03 21:36:30

标签: c++ arrays initialization

// deck of cards
#include <iostream>
#include <string>
using namespace std;

int main()
{
    int i, j, k;
    char arr[4][13][14] =
    {
        {
            { heart one, heart two, heart three, heart four, heart five, heart six, heart seven, heart eight, heart nine, heart ten, heart jack, heart queen, heart king, heart ace }
        },
        {
            { diamond one, diamond two, diamond three, diamond four, diamond five, diamond six, diamond seven, diamond eight, diamond nine, diamond ten, diamond jack, diamond queen, diamond king, diamond ace }
        },
        {
            { club one, club two, club three, club four, club five, club six, club seven, club eight, club nine, club ten, club jack, club queen, club king, club ace }
        },
        { 
            { spade one,spade two, spade three, spade four, spade five, spade six, spade seven, spade,eight, spade nine, spade ten, spade jack, spade queen, spade king, spade ace }
        },
    };

    clrscr();
    printf(":::3D Array:::\n\n");
    for(i=0; i<4;i++)
    {
        for(j=0;j<13;j++)
        {
            for(k=0;k<14;k++)
            {
                printf("%d\t",arr[i][j][k]);
            }
            printf("\n");
        }
        printf("\n");
    }

    return 0;
}

我收到一个错误,其中没有指定心脏,钻石,铁锹,俱乐部。但是我设置了类型char,有人可以给我一些关于如何解决这个问题的指示吗?我想要一副3D阵列用于我的卡片组,4行(套装),13个柱子(两个,三个,......,ace)和14个数据位置(例如,最长的是8个钻石,占13个元素)。请帮忙!

2 个答案:

答案 0 :(得分:2)

您可以从下往上开始构建声明以帮助您 更好地理解声明语法。

你如何声明一个14 char的数组?

char card[14] = "heart two";

现在,您将如何创建其中13个数组呢?

char suite[13][14] = {"heart two", "heart three", "heart four" ...};

现在,您将如何创建其中4个数组呢?

char deck[4][13][14] =
{
   {"heart two", "heart three", "heart four" ...},
   {"diamond two", "diamond three", "diamond four" ...},
   {"club two", "club three", "club four" ...},
   {"spade two", "spade three", "spade four" ...}
};

答案 1 :(得分:0)

我猜你想要这样的东西。此外,一副牌中没有1。

const char *arr[4][13]=
{
    {"heart two", "heart three",...},
    {"diamond two", "diamond three",...},
    {"club two", "club three",...},
    {"spade two", "spade three",...}
};