Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 简单二维阵列程序中的非期望输出_C_Arrays_Output - Fatal编程技术网

C 简单二维阵列程序中的非期望输出

C 简单二维阵列程序中的非期望输出,c,arrays,output,C,Arrays,Output,作为一个家庭作业问题,我必须创建一个简单的程序,从标准的52张卡片组中输出5张不同的卡片 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<time.h> void deal(char input_array[5][4]); char deck[52][4]={"AcS","02S","03S","04S","05S","06S","07S","08S","09S"

作为一个家庭作业问题,我必须创建一个简单的程序,从标准的52张卡片组中输出5张不同的卡片

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

void deal(char input_array[5][4]);

char deck[52][4]={"AcS","02S","03S","04S","05S","06S","07S","08S","09S","10S","JaS","QuS","KiS",
"AcH","02H","03H","04H","05H","06H","07H","08H","09H","10H","JaH","QuH","KiH","AcD","02D",
"03D","04D","05D","06D","07D","08D","09D","10D","JaD","QuD","KiD","AcC","02C","03C","04C",
"05C","06C","07C","08C","09C","10C","JaC","QuC","KiC"};

main()
{
    int index;
    char hand[5][4];
    deal(hand);

    for(index = 0; index < 5; index++){
        printf("%s\n", hand[index]);
    }
}

void deal(char input_array[5][4])
{
    int i, j, randInt, count = 0;
    //srand((unsigned int)time(NULL));
    srand(time(NULL));

    /* outer for loop used to assign 5 cards to array */
    for (i = 0; i < 5; i++){

        /* random int generated between 0 and 51, so a random card can be picked from the deck array */
        randInt = (rand() % 52);

        /* inner for loop checks each time whether the card already exists in the input_array. if it exists, count is incremented by 1 */
        for(j = 0; j < 5; j++){

            if(strcmp(input_array[j], deck[randInt]) == 0){
                ++count;
            }

        }

        /* after exiting inner for loop, if count is still 0, the card chosen from the deck isn't already in input_array. so it's added to input_array */
        if(count == 0){
            strcpy(input_array[i], deck[randInt]);
        }

    }
}
#包括
#包括
#包括
#包括
无效交易(字符输入_数组[5][4]);
字符组[52][4]={“AcS”、“02S”、“03S”、“04S”、“05S”、“06S”、“07S”、“08S”、“09S”、“10S”、“JaS”、“QuS”、“KiS”,
“AcH”,“02H”,“03H”,“04H”,“05H”,“06H”,“07H”,“08H”,“09H”,“10H”,“JaH”,“QuH”,“KiH”,“AcD”,“02D”,
“03D”,“04D”,“05D”,“06D”,“07D”,“08D”,“09D”,“10D”,“JaD”,“QuD”,“KiD”,“AcC”,“02C”,“03C”,“04C”,
“05C”、“06C”、“07C”、“08C”、“09C”、“10C”、“JaC”、“QuC”、“KiC”};
main()
{
整数指数;
炭手[5][4];
成交(手);
对于(索引=0;索引<5;索引++){
printf(“%s\n”,hand[index]);
}
}
无效交易(字符输入_数组[5][4])
{
int i,j,randInt,count=0;
//srand((无符号整数)时间(NULL));
srand(时间(空));
/*用于将5张卡分配给阵列的外部for循环*/
对于(i=0;i<5;i++){
/*随机整数在0和51之间生成,因此可以从牌组数组中拾取一张随机牌*/
randInt=(rand()%52);
/*内部for循环每次检查输入_数组中是否已存在该卡。如果存在,则计数递增1*/
对于(j=0;j<5;j++){
if(strcmp(输入数组[j],数据组[randInt])==0){
++计数;
}
}
/*退出内部for循环后,如果计数仍然为0,则从牌组中选择的牌不在input_数组中。因此,它被添加到input_数组中*/
如果(计数=0){
strcpy(输入数组[i],数据组[randInt]);
}
}
}
但一旦我反复运行了足够多的时间,最终我会得到奇怪的/意外的/随机的值(见下面的输出图像)


我检查了每一行代码,并仔细检查了所有内容。我不知道这里出了什么问题。感谢您的帮助

您的代码在此处显示未定义的行为:

if(strcmp(input_array[j], deck [randInt]) == 0){
因为
input\u数组
未初始化且包含“垃圾”值

要解决它,就要改变

for(j = 0; j < 5; j++){
同时添加附加一个
else
部分:

if(count == 0){
         strcpy(input_array[i], deck [randInt]);
     }
else{
         input_array[i][0]='\0';
     }

input_数组[i][0]='\0'用于NUL terminate
input_array[i]
以使其初始化,并且
main
中的
printf
不会打印wierd内容。

请发布源代码,图像不可读。此外,循环似乎像复制粘贴问题一样错误,您确定两个循环都指向
i<5
j<5
?而您的
main()
签名是错误的,它是
int main()
,这意味着您没有使用编译器警告,因此可能会对您隐藏一些细微的错误。@iharob为什么循环是错误的?外部for循环运行5次,确保将5张卡保存到输入_阵列。内部for循环检查输入数组中的每个元素以避免重复。最大的问题是,如果代码检测到重复,它仍会前进到下一张卡(增量
i
)。
i
的值只有在选择了非重复项并且执行了
strcpy
之后才应该增加。@010010000101001不要发布链接,发布输出只需复制并粘贴它即可。
if(count == 0){
         strcpy(input_array[i], deck [randInt]);
     }
else{
         input_array[i][0]='\0';
     }