2015-03-31 231 views
0

你好,我正在嘗試創建一個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; 
} 
+1

這是一個Q/A網站。沒有這個問題,它是(大多數情況下)不可能「回答」它 – 2015-03-31 13:55:51

+0

如果你想讓別人看看你的代碼,請使用理智和一致的縮進和支撐位置。 – Lundin 2015-03-31 14:49:11

回答

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; 
} 
+0

非常感謝你的幫助 – 2015-03-31 14:25:13

0

這裏:

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]); 

爲數組名「衰」的指針的第一個元素。

還有其他問題!

  1. 函數聲明:

    find_matches(int *lotto, int *ticket) 
    

    缺少返回類型。通過使用

    int find_matches(int *lotto, int *ticket) 
    
  2. 陣列聲明

    int arrayReturn[sizeof(lotto) + sizeof(ticket)]; 
    

    修復它是錯誤的,因爲lottoticketint*。因此,sizeof運算符返回的大小爲int*,而不是整個數組的大小。您必須硬編碼值或將大小作爲參數傳遞給函數。

  3. 在功能get_lotto_draw,我看不到任何理由爲什麼lottery有50個指數。你只需要6個。無論如何,最後(49 th)索引也不被使用。
0

你調用不正確,它會導致未定義的行爲:

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) ... 
+0

謝謝你解釋這個非常有幫助 – 2015-03-31 14:30:25

+0

@AliDavies歡迎你!如果您不想尋求這個問題的進一步幫助,請考慮通過點擊旁邊的灰色複選標記來接受答案。這可以讓其他網站訪問者知道你的問題已經解決,並在Stack Overflow上爲你贏得新徽章。 – dasblinkenlight 2015-03-31 14:34:47

相關問題