彩票计划问题

时间:2015-03-18 18:40:51

标签: c pointers

我目前正在创建我的第二个主要C程序。我刚刚开始学习C而且我遇到了一些问题,以及对该计划下一步该怎么做的困惑。

这个想法是基本上允许用户输入所需的年数,然后该程序模拟每周播放的彩票游戏,具体取决于他们输入的年数。然后在程序内部,我希望两个数组相互比较,并检查它们同时具有的任何数字。用户端的彩票保持不变,这是在阵列内设置的,当然,随机彩票号码每周都会改变。

基础已经完成,我只是遇到了一些问题,并且不知道在某些方面去哪里。

问题:
" int周=年* 52"不起作用,说初始化元素不是常数。

当我返回get_lotto_draw时,我只是得到一个褶皱的数字,它无论如何都没有分开,所以我不知道该怎么做。

#include <stdio.h> //Alows input/output operations
#include <stdlib.h> //Standard utility operations

//Declaring Variables
int year;
char name[15];
char option;
int lotteryPlayer[] = {5,11,15,33,42,43};
int i;
int randomNums[49];
int *lotteryPtr = lotteryPlayer;
int *randomPtr = randomNums;
int weeks = 0;

void print_array(int *lotteryPtr);
int* get_lotto_draw(int *randomPtr);

//Main Method
int main(int argc, char *argv[])
{
    start: //Start of program

    printf("\n---------------------------");
    printf("\nProject: Jackpot Dreams ");
    printf("\n---------------------------\n");

   printf("\nWhat is your First Name?:> "); //Asks them for their choice
   scanf("%s", &name); //Reads the input

   printf("\nHow Many Years to Sleep?:> "); //Asks them for their choice
   scanf("%d", &year); //Reads the input
   weeks = year * 52;

   printf("\nOk %s, I will play the lottery for %d years!\n",name, year);
   sleep(1500);
   printf("Sweet Dreams %s, don't let the bed bugs bite", &name);
   sleep(1500);
   printf(". ");
   sleep(1500);
   printf(". ");
   sleep(1500);
   printf(".");
   sleep(2000);

   printf("%d", get_lotto_draw);
   system("PAUSE");
}

//Returns an array of six random lottery numbers 1-49
int* get_lotto_draw(int *randomPtr) 
{
    for (i=0 ; i<weeks ; i++)
       return randomNums;  
}

//Print out the content of an array
void print_array(int *lotteryPtr)
{
     printf("Hello");
}

//Returns number of matches between two arrays
int find_matches(int * lotteryPtr, int * randomPtr)
{

}

更新

#include <stdio.h> //Alows input/output operations
#include <stdlib.h> //Standard utility operations

//Declaring Variables
int year;
char name[15];
char option;
int lotteryPlayer[] = {5,11,15,33,42,43};
int i;
int randomNums[49];
int *lotteryPtr = lotteryPlayer;
int *randomPtr = randomNums;
int weeks = 0;

void print_array(int *lotteryPtr);
int* get_lotto_draw(int *randomPtr);

//Main Method
int main(int argc, char *argv[])
{
    start: //Start of program

    printf("\n---------------------------");
    printf("\nProject: Jackpot Dreams ");
    printf("\n---------------------------\n");

   printf("\nWhat is your First Name?:> "); //Asks them for their choice
   scanf("%s", name); //Reads the input

   printf("\nHow Many Years to Sleep?:> "); //Asks them for their choice
   scanf("%d", &year); //Reads the input
   weeks = year * 52;

   printf("\nOk %s, I will play the lottery for %d years!\n",name, year);
   sleep(1500);
   printf("Sweet Dreams %s, don't let the bed bugs bite", &name);
   sleep(1500);
   printf(". ");
   sleep(1500);
   printf(". ");
   sleep(1500);
   printf(".");
   sleep(2000);

   printf("%d", get_lotto_draw(*randomPtr));
   system("PAUSE");
}

//Returns an array of six random lottery numbers 1-49
int* get_lotto_draw(int *randomPtr) 
{
    for (i=0 ; i<weeks ; i++)
       return randomNums;  
}

//Print out the content of an array
void print_array(int *lotteryPtr)
{
     printf("Hello");
}

//Returns number of matches between two arrays
int find_matches(int * lotteryPtr, int * randomPtr)
{

}

1 个答案:

答案 0 :(得分:0)

主要从评论中借用,您的代码存在一些问题。

int weeks = year * 52;导致错误,因为year尚未初始化。将其更改为int weeks = 0;,然后将weeks = year * 52放在主要功能的开头。

您在程序开头不需要start:标签,除非出于某种原因您想要使用goto返回那里,这通常被视为不良做法。

printf("%d", get_lotto_draw);get_lotto_draw的地址打印为小数(&#34;%d&#34;),您需要将其设为get_lotto_draw(args)才能获得{{1}的返回值1}}。

system(&#34; PAUSE&#34;),在shell上运行命令PAUSE。我不知道这是否会暂停当前的程序,但是如果你不想让程序退出,那么循环就会响起。

get_lotto_draw

while (1) {} 的实施并不符合您的想法。你现在正在做的只是返回get_lotto_draw,你要做的是生成1到49之间的随机数。要做到这一点,你应该首先生成一个随机数,并将其修改为48并添加一个。这将为您提供1到49之间的随机数。randomNums将生成一个随机数,srand(time(NULL)); int r = rand();将修改为48并添加一个。然后,您可以为for循环的每次迭代执行此操作,并使用值创建一个数组。您将返回的数组必须为r %= 48; r += 1;&#39; d。

malloc

您的int get_lotto_draw() { srand(time(NULL)); int* rv = malloc(sizeof(int) * 6); for (int i = 0; i < 6; i++) { rv[i] = (rand() % 48) + 1; } return rv; } 功能也未实现。只需遍历数组即可找到匹配就足够了。

find_matches

最后,对于您的int find_matches(int* a, int* b) { int matches = 0; for (int i = 0; i < 6; i++) { if (a[i] == b[i]) { matches++; } } return matches; } 功能,您只需要遍历彩票号码列表,然后打印每个号码。

print_array