Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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 - Fatal编程技术网

C 比较两个数组并打印匹配元素的数量

C 比较两个数组并打印匹配元素的数量,c,C,你好,我正在尝试创建一个由6个随机数组成的数组作为我的彩票号码,并将它们与我的彩票进行比较,看看我有多少场比赛。 我正在努力将指针作为函数参数传递 #include <stdio.h> #include <stdlib.h> #include <time.h> int *get_lotto_draw(int n) { int i; static int lotto[6]; int lottery[5

你好,我正在尝试创建一个由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;
}
#包括
#包括
#包括
int*get\U lotto\U抽签(int n)
{
int i;
静态整数乐透[6];
国际彩票[50];
国际单位,j,温度;
对于(i=0;i此处:

您传递的是
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)];
    
    find_matches(int *lotto, int *ticket) ...
    
    是错误的,因为
    lotto
    ticket
    int*
    。因此
    sizeof
    运算符返回
    int*
    的大小,而不是整个数组的大小。您必须硬编码值或将大小作为参数传递给函数

  • 在函数
    get\u lotto\u draw
    中,我看不出为什么
    lotking
    有50个索引。您只需要其中的6个。无论如何,最后一个(第49个)索引也没有使用

  • 您的调用不正确,并导致未定义的行为:

    int count = find_matches(&lotto[6], &ticket[6]);
    
    这会将指针传递到两个数组的另一端,因此
    find\u matches
    将执行无效的解引用

    调用函数的正确方法如下:

    int count = find_matches(lotto, ticket);
    
    或者,如果设置为使用
    &
    运算符

    int count = find_matches(&lotto[0], &ticket[0]);
    
    还请注意,本声明

    int arrayReturn[sizeof(lotto) + sizeof(ticket)];
    
    find_matches(int *lotto, int *ticket) ...
    
    是ANSI之前的,因为函数的返回类型被省略(在这种情况下,C编译器假定为
    int
    )。您应该更改声明以显式提供返回类型:

    int find_matches(int *lotto, int *ticket) ...
    

    下面是进行比较的代码:

    #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;
    }
    
    #包括
    #包括
    #包括
    int*get\U lotto\U抽签(int n)
    {
    int i;
    静态整数乐透[6];
    国际彩票[50];
    int j,温度;
    
    对于(i=0;这是一个问答网站。没有这个问题,(在大多数情况下)不可能“回答”如果你想让别人看你的代码,请使用合理一致的缩进和大括号。非常感谢这非常有用谢谢你解释这非常有用helpful@AliDavies不客气!如果你不想进一步帮助这个问题,请考虑通过点击灰色复选标记NEX接受答案。这会让其他网站访问者知道你的问题已经解决,并在堆栈溢出时为你赢得一个新的徽章。