比较两个阵列并打印匹配元素的数量
你好,我正在尝试创建一个6个随机数字的数组作为我的彩票号码,并将它们与我的票据进行比较,以查看我有多少匹配。 我正在努力将传递指针作为函数参数。比较两个阵列并打印匹配元素的数量
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *get_lotto_draw(int n)
{
int i;
static int lotto[6];
int lottery[50];
int u,j,temp;
for (i =0; i<49; i++)
lottery[i] = i+1;
for (i =0; i<49; i++)
{
j = (rand()%49)+1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i =0; i<6; i++)
{
lotto[i] = lottery[i];
}
return lotto;
}
find_matches(int *lotto, int *ticket)
{
int arrayReturn[sizeof(lotto) + sizeof(ticket)];
int count = 0;
int i;
int j;
for(i = 0; i < 6; i++)
{
for(j = 0; j < 6; j++)
{
if(lotto[i]==lotto[j])
{
count = count + 1;
}
}
}
return count;
}
int main(int argc, char *argv[])
{
int i, n = 6;
int *lotto;
int ticket[6] = {5, 11, 15, 33, 42, 43};
srand(time(NULL));
lotto = get_lotto_draw(n);
int count = find_matches(&lotto[6], &ticket[6]);
printf("%d\n\n", count);
printf("Here is the array: ");
for(i = 0 ; i < n ; i++) {
printf("%d ", lotto[i]);
}
printf("\n\n");
return 0;
}
这里是代码做比较:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *get_lotto_draw(int n)
{
int i;
static int lotto[6];
int lottery[50];
int j, temp;
for (i = 0; i<49; i++)
lottery[i] = i + 1;
for (i = 0; i<49; i++){
j = (rand() % 49) + 1;
temp = lottery[i];
lottery[i] = lottery[j];
lottery[j] = temp;
}
for (i = 0; i<6; i++){
lotto[i] = lottery[i];
}
return lotto;
}
int find_matches(int *lotto, int *ticket)
{
int count = 0;
int i;
int j;
for (i = 0; i < 6; i++){
for (j = 0; j < 6; j++){
if (lotto[i] == ticket[j]){
count++;
}
}
}
return count;
}
int main(int argc, char *argv[])
{
int i, n = 6;
int *lotto;
int ticket[6] = { 5, 11, 15, 33, 42, 43 };
int count = 0;
srand(time(NULL));
lotto = get_lotto_draw(n);
printf("\nHere is the lotto array: ");
for (i = 0; i < n; i++) {
printf("%d ", lotto[i]);
}
printf("\nHere is the ticket array: ");
for (i = 0; i < n; i++) {
printf("%d ", ticket[i]);
}
count = find_matches(lotto, ticket);
printf("\ncount of matches: %d", count);
getchar();
return 0;
}
非常感谢你的帮助 – 2015-03-31 14:25:13
这里:
int count = find_matches(&lotto[6], &ticket[6]);
传递的lotto[6]
和ticket[6]
它是一个无效的数组元素的地址。实际上,你想写
int count = find_matches(lotto, ticket);
这是一样的
int count = find_matches(&lotto[0], &ticket[0]);
为数组名“衰”的指针的第一个元素。
还有其他问题!
-
函数声明:
find_matches(int *lotto, int *ticket)
缺少返回类型。通过使用
int find_matches(int *lotto, int *ticket)
-
阵列声明
int arrayReturn[sizeof(lotto) + sizeof(ticket)];
修复它是错误的,因为
lotto
和ticket
是int*
。因此,sizeof
运算符返回的大小为int*
,而不是整个数组的大小。您必须硬编码值或将大小作为参数传递给函数。 - 在功能
get_lotto_draw
,我看不到任何理由为什么lottery
有50个指数。你只需要6个。无论如何,最后(49 th)索引也不被使用。
你调用不正确,它会导致未定义的行为:
int count = find_matches(&lotto[6], &ticket[6]);
这个指针传递到两个阵列之一,过去的高端,所以find_matches
将执行无效解引用。
正确的方法来调用你的函数如下:
int count = find_matches(lotto, ticket);
,或者如果您在使用&
操作,设置
int count = find_matches(&lotto[0], &ticket[0]);
另外请注意,此声明
find_matches(int *lotto, int *ticket) ...
是ANSI之前的,因为函数的返回类型被省略(在这种情况下,C编译er假设为int
)。你应该改变的声明明确提供的返回类型:
int find_matches(int *lotto, int *ticket) ...
谢谢你解释这个非常有帮助 – 2015-03-31 14:30:25
@AliDavies欢迎你!如果您不想寻求这个问题的进一步帮助,请考虑通过点击旁边的灰色复选标记来接受答案。这可以让其他网站访问者知道你的问题已经解决,并在Stack Overflow上为你赢得新徽章。 – dasblinkenlight 2015-03-31 14:34:47
这是一个Q/A网站。没有这个问题,它是(大多数情况下)不可能“回答”它 – 2015-03-31 13:55:51
如果你想让别人看看你的代码,请使用理智和一致的缩进和支撑位置。 – Lundin 2015-03-31 14:49:11