gpt4 book ai didi

c - 在循环之间移动数组

转载 作者:行者123 更新时间:2023-11-30 17:15:39 24 4
gpt4 key购买 nike

好吧,这是我项目的一部分,我遇到了麻烦。我需要能够选择一个类别,然后让它指向一个单词列表,然后需要这些单词用于各种用途。

到目前为止,我已将我的单词分为不同的类别,并且它们能够嵌入到我的二维数组中。但是,我现在需要根据用户输入检查这些单词,看看它们是否匹配。

我已经创建了一个函数来检查 2 个数组是否匹配,但是在将单词输入到函数中时遇到问题。

这是我的话的排序方式。

        printf("\n\n------------------------------");
printf("\n1. Animals");
printf("\n2. Colors");
printf("\n3. Planets");
printf("\n4. Weekdays");
printf("\n------------------------------");


int valid = 0;
while(valid == 0)
{
printf("\n\nYour selection: ");
int catagory;
scanf("%d", &catagory);

if(catagory == 1)
{
char word[] = "dog";
char word2[] = "cat";
char word3[] = "chicken";
char word4[] = "mouse";
char word5[] = "lion";
char word6[] = "rabbit";
char word7[] = "pig";
horizontal(myArray, word);
horizontal(myArray, word2);
diagonal(myArray, word3);
diagonal(myArray, word4);
vertical(myArray, word5);
vertical(myArray, word6);
horizontal(myArray, word7);
valid++;


}
else if(catagory == 2)
{
char word[] = "red";
char word2[] = "blue";
char word3[] = "yellow";
char word4[] = "brown";
char word5[] = "pink";
char word6[] = "green";
char word7[] = "black";
horizontal(myArray, word);
horizontal(myArray, word2);
diagonal(myArray, word3);
diagonal(myArray, word4);
vertical(myArray, word5);
vertical(myArray, word6);
horizontal(myArray, word7);
valid++;

}
else if(catagory == 3)
{
char word[] = "earth";
char word2[] = "neptune";
char word3[] = "venus";
char word4[] = "mars";
char word5[] = "jupiter";
char word6[] = "saturn";
char word7[] = "pluto";
horizontal(myArray, word);
horizontal(myArray, word2);
diagonal(myArray, word3);
diagonal(myArray, word4);
vertical(myArray, word5);
vertical(myArray, word6);
horizontal(myArray, word7);
valid++;

}
else if(catagory == 4)
{
char word[] = "monday";
char word2[] = "tuesday";
char word3[] = "wednesday";
char word4[] = "thursday";
char word5[] = "friday";
char word6[] = "saturday";
char word7[] = "saturn";
horizontal(myArray, word);
horizontal(myArray, word2);
diagonal(myArray, word3);
diagonal(myArray, word4);
vertical(myArray, word5);
vertical(myArray, word6);
horizontal(myArray, word7);
valid++;

}
else if(catagory > 4)
{
printf("\n\nInvalid input. Please select again.\n");
}
else if(catagory < 1)
{
printf("\n\nInvalid input. Please select again.\n");
}
}

下面我现在再次需要这些词:

        clock_t start = clock(), diff;
int match = 0;
int correct = 0;
print_array(myArray);
printf("\nPlease type any words that you can find");
while (correct != 7)
{
printf("\n: ");
char word2[256];
fgets(word2,256,stdin);

match = checker("word from category",word2);
if(match == 1)
{
correct++;
printf("\nWeldone you found a match!");
}

}


diff = clock() - start;

int msec = diff * 1000 / CLOCKS_PER_SEC;
printf("Time taken %d seconds %d milliseconds\n", msec/1000, msec%1000);

如您所见,我的数组超出了范围。谁能给我一个解决方案吗?

最佳答案

也许定义一个全局三维数组可能会有所帮助:

char words[4][8][];

words[0] = {"dog", "cat", "chicken", "mouse", "lion", "rabbit", "pig"};
//etc...

(you can of course define it in one line)

然后您可以使用以下方式访问它:

words[catagory][<WORD>]

(最后一个维度是单词本身)

或者我误解了你的问题?然后我需要知道你的水平函数和对角函数以及整个代码正在做什么。

关于c - 在循环之间移动数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29925574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com