c - 比较 int 数组的程序不起作用

标签 c arrays

因此,作为大学工作的一部分,我必须创建一个彩票模拟器。我还在这个程序中使用了两个函数。 get_lotto_draw 创建一个 6 元素数组,其中随机数字在 1-49 之间,find_matches 应该将用户定义的 6 元素数组与此随机生成的数组进行比较,并跟踪找到了多少个匹配项。该程序模拟在用户指定的年数内每周玩一次乐透。

正文:

const int WEEKS_IN_YEAR = 52;
int lottoCounter = 0;
years = years * WEEKS_IN_YEAR;

int match1 = 0;
int match2 = 0;
int match3 = 0;
int match4 = 0;
int match5 = 0;
int match6 = 0;

for(lottoCounter = 0; lottoCounter < years; lottoCounter++)
{
      int* x = get_lotto_draw(); //Use function to generate lottery numbers
      int* y = userNums;
      int found = find_matches(x, y);

      if(found == 1)
      {
               match1++;
      }

       if(found == 2)
      {
               match2++;
      }

       if(found == 3)
      {
               match3++;
      }

       if(found == 4)
      {
               match4++;
      }

       if(found == 5)
      {
               match5++;
      }

       if(found == 6)
      {
               match6++;
      }

      if(match6 != 0)
      {
                printf("Congratulations Roger, you've won the Jackpot!");
                break;
      }   
}

printf("Matched 1 number %d times", match1);
printf("\nMatched 2 number %d times", match2);
printf("\nMatched 3 number %d times", match3);
printf("\nMatched 4 number %d times", match4);
printf("\nMatched 5 number %d times", match5);
printf("\nMatched 6 number %d times", match6);

free(arrayPointer);

get_lotto_draw函数:

int* get_lotto_draw() //Returns an array of six random lottery numbers 1-49
{
     int min = 1;
     int max = 49;
     int counter = 0;

     srand(time(NULL)); //Set seed for rand as current time

     int *arrayPointer = malloc(6 * sizeof(int)); //Clear space for arrayPointer

     for(counter = 0; counter <= 5; counter++)
     {
                 int x1 = 1;

                 while(x1)
                 {
                          int temp = rand()%(max-min)+min; //Gives random number range between 1-49 inclusive
                          int i = 0;

                          for(i = 0; i < counter; i++)
                          {
                              if( arrayPointer[i] == temp)
                              {
                                break;
                              }
                          } 

                          if(i == counter)
                          {
                             x1=0;
                             arrayPointer[counter] = temp;
                          }
                  }
     }  
     return arrayPointer;
}

find_matches 函数:

int find_matches(int * array1, int * array2)
{
    int* x = array1;
    int* y = array2;
    int found = 0;
    int i = 0;
    int j = 0;

    for(i = 0; i < 6; i++)
    {
          for(j = 0; j < 6; j++)
          {
                if(x[i] == y[j])
                {
                     found++;
                }
          }
    }
    return found;  
}

我遇到的问题是“匹配 1 个数字 %d 次”等不起作用,它们只是给我 0 值。我认为问题出在 main 的某个地方,因为我的函数之前就可以工作。感谢您抽出时间。

编辑

我将此代码添加到 main 中:

int myArray[6] = {1, 23, 42, 32, 4, 17};
int* x = userNums;
int* y = myArray;
int found = find_matches(x, y);
printf("matches: %d", found);

这工作得很好。如果我输入 myArray 中存在的 3 个数字,found 将返回 3 等等。

最佳答案

因此,我采纳了针对我的问题留下的评论中的所有建议。我将随机种子放在 main 的开头而不是放在函数中,固定每年抽奖的次数,并在返回 arrayPointer 后放置 free(arrayPointer) 。实现所有这些修复后,我的程序现在运行良好,感谢所有贡献者!

关于c - 比较 int 数组的程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22618472/

相关文章:

c - 我需要释放结构中的变量吗?

javascript - 在 Angular 6+ 中,如何用图标替换 <p-dataTable> 和 <p-column> 标记中的值?

python - 在 python 中处理字节

arrays - 遍历对象数组。如果找到正确的方法,则访问对象方法。否则在数组中创建一个新对象

c - 无法将文件内容复制到缓冲区

C 并行线程与并行进程

c++ - 获取上次激活窗口的窗口句柄

arrays - 修复char格式错误-Linux系统C程序

javascript - 如何在动态表中插入数组的某些值作为 <td> 的 CSS 样式属性?

python - OpenCV - 对于 cv2.imread,numpy 数组是 'bad argument type'?