c - Program to compare int arrays not working -


so, part of uni work had create lottery simulator. have used 2 functions in program. get_lotto_draw creates 6 element array random nums between 1-49, find_matches should compare user defined 6 element array random generated array, , keep track of how many matches found. program simulates playing lotto once weeks on user specified number of years.

main body:

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 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 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 function:

int* get_lotto_draw() //returns array of 6 random lottery numbers 1-49 {      int min = 1;      int max = 49;      int counter = 0;       srand(time(null)); //set seed rand current time       int *arraypointer = malloc(6 * sizeof(int)); //clear space 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 = 0;                            for(i = 0; < counter; i++)                           {                               if( arraypointer[i] == temp)                               {                                 break;                               }                           }                             if(i == counter)                           {                              x1=0;                              arraypointer[counter] = temp;                           }                   }      }        return arraypointer; } 

find_matches function:

int find_matches(int * array1, int * array2) {     int* x = array1;     int* y = array2;     int found = 0;     int = 0;     int j = 0;      for(i = 0; < 6; i++)     {           for(j = 0; j < 6; j++)           {                 if(x[i] == y[j])                 {                      found++;                 }           }     }     return found;   } 

the problem im having " matched 1 number %d time" , on not working, give me 0 values. think problem somewhere in main because functions working earlier. time.

edit

i added code 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); 

and works fine. if enter 3 number present in myarray, found returns 3 , on.

so took advice comments left on question. placed random seed @ start of main instead of in function, fixed amount of times lottery drawn each year, , placed free(arraypointer) after returning arraypointer. after implementing of these fixes, program working fine, contributed!


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -