2017-01-12 51 views
0
int main(void) { 
    int no, flimit, i, pos_of_fval, freq_of_fval, n, j; 
    long long int max; 
    scanf("%d %d", &n, &flimit); 
    char list[n][21]; 
    long long int fval[n]; 
    char disp[flimit][21]; 
    for (i = 0; i < n; i++) 
     scanf("%s %lld", list[i], &fval[i]); 
    for (i = 0; i < flimit; i++) { 
     max = 0; 
     for (j = 0; j < n; j++) 
      if (fval[j] > max) { 
       max = fval[j]; 
       pos_of_fval = j; 
      } 
     freq_of_fval = freq(fval, max, n); 
     if (freq_of_fval == 1) { 
      strcpy(disp[i], list[pos_of_fval]); 
      fval[pos_of_fval] = 0; 
     } else 
      lexical(list, disp, fval, i, n, freq_of_fval, max); 
    } 
    for (i = 0; i < flimit; i++) 
     printf("%s\n", disp[i]); 

    return 0; 
} 

int freq(long long fval[], long long max, int n) { 
    int count = 0, i; 
    for (i = 0; i < n; i++) 
     if (fval[i] == max) 
    count++; 
    return count; 
} 

void lexical(char list[][21], char disp[][21], long long fval[], 
      intn dispos ,intn, int freq_of_fval, long long max) { 

    int a[freq_of_fval], i, j, apos = 0, temp; 

    for (j = 0; j < freq_of_fval; j++) { 
     for (i = 0; i < n; i++) 
      if (fval[i] == max) 
       a[apos++] = i; 
    } 

    for (i = 0; i < freq_of_fval - 1; i++) { 
     for (j = 0; j < freq_of_fval - 1 - i; j++) { 
      if (strcmp((list[a[j]]), list[a[j + 1]]) > 1) { 
       temp = a[j]; 
       a[j] = a[j + 1]; 
       a[j + 1] = temp; 
      } 
     } 
    } 
    strcpy(disp[dispos], list[a[0]]); 
} 

錯誤:如何通過任何類型的二維數組

warning: passing argument 1 of 'lexical' from incompatible pointer type 
lexical(list,disp,fval,i,n,freq_of_fval,max); 
      note: expected 'char \*' but argument is of type 'char (\*)[21]' 
    void lexical(char list[],char disp[][21],long long fval[], int dispos, int 
+1

首先歡迎您的StackOverflow!第二,你應該提供[mcve] ...第三隻是拋出代碼和錯誤不被認爲是一個好問題:) – geisterfurz007

+0

* void lexical(char list [] [21],char disp [] [21] ,long long fval [],*'intn' * dispos,*'intn' *,int freq_of_fval,long long max)* - 真的嗎? –

回答

0

您好,歡迎到C編碼的精彩世界 - 在這裏你真的需要保留一個嚴密的統治上的數組的長度。

該代碼有幾個問題- 首先,您聲明的數組大小未知。例如在 -

char list[n][21]; 

這樣的行只能在C99中工作 - 否則你不能聲明一個可變大小的數組。 但即使在這種情況下,你也可以這樣做 - 在你的情況下,n在你使用它之前從未設置過。

一般來說,在C - 你必須選擇: 1.你事先知道你的數組的大小,然後您使用的同時在分配和函數簽名

舉例來說,如果你有20x21矩陣,除了在主代碼:

char list[20][21] 

,然後在功能

void lexical(char list[20][21],...) 

void lexical(charlist[][],...) 
  • ,如果你不事先知道大小,那麼你可以使用malloc
  • 在主代碼分配內存:

    //N is how many rows you have 
    char **list = malloc(sizeof (char *) * N); 
    if (list ) 
    { 
        for (i = 0; i < N; i++) 
        { 
        a[i] = malloc(sizeof(char) * 21); 
        } 
    } 
    

    然後在功能可按: 無效詞彙(字符**列表,...)

    -1
    #include <stdio.h> 
    #include <string.h> 
    
    void lexical(char list[][21], char disp[][21], long long fval[], 
          int dispos ,int n, int freq_of_fval, long long max); 
    int freq(long long fval[], long long max, int n) ; 
    
    int main(void) { 
    
        int no, flimit, i, pos_of_fval, freq_of_fval, n, j; 
        long long int max; 
    
        printf("請輸入n 和 flimit\n"); 
        scanf("%d %d", &n, &flimit); 
    
        char list[n][21]; 
        long long int fval[n]; 
        char disp[flimit][21]; 
        for (i = 0; i < n; i++) { 
    
         printf("字符串 和 long long數據\n"); 
         scanf("%s %lld", list[i], &fval[i]); 
        } 
    
        for (i = 0; i < flimit; i++) { 
    
         max = 0; 
         for (j = 0; j < n; j++) 
         { 
          if (fval[j] > max) { 
           max = fval[j]; 
           pos_of_fval = j; 
          } 
         } 
    
         freq_of_fval = freq(fval, max, n); 
    
         if (freq_of_fval == 1) { 
          strcpy(disp[i], list[pos_of_fval]); 
          fval[pos_of_fval] = 0; 
         } else { 
          lexical(list, disp, fval, i, n, freq_of_fval, max); 
         } 
        } 
    
        for (i = 0; i < flimit; i++) 
         printf("%s\n", disp[i]); 
    
        return 0; 
    } 
    
    // 計算數組中最大值有多少個(相同的最大值) 
    int freq(long long fval[], long long max, int n) { 
    
        int count = 0, i; 
        for (i = 0; i < n; i++) 
         if (fval[i] == max) 
          count++; 
        return count; 
    } 
    
    void lexical(char list[][21], char disp[][21], long long fval[], 
          int dispos ,int n, int freq_of_fval, long long max) { 
    
        int a[freq_of_fval], i, j, apos = 0, temp; 
    
        for (j = 0; j < freq_of_fval; j++) { 
         for (i = 0; i < n; i++) 
          if (fval[i] == max) 
           a[apos++] = i; 
        } 
    
        for (i = 0; i < freq_of_fval - 1; i++) { 
         for (j = 0; j < freq_of_fval - 1 - i; j++) { 
          if (strcmp((list[a[j]]), list[a[j + 1]]) > 1) { 
           temp = a[j]; 
           a[j] = a[j + 1]; 
           a[j + 1] = temp; 
          } 
         } 
        } 
        strcpy(disp[dispos], list[a[0]]); 
    }