2017-07-13 21 views
1

次のコードをBALLOT_SIZEの大きな入力値に適しているようにしようとしましたが、問題が発生しました。ポインタへの変更が変更されました

void Dot_Product(int a[BALLOT_SIZE][BALLOT_SIZE], int output[BALLOT_SIZE]) { 

    for (int i = 0; i < BALLOT_SIZE; i++) { 
     int total = 0; 
     for (int j = 0; j < BALLOT_SIZE; j++) { 
      total = total + a[i][j] * (BALLOT_SIZE - (j+1)); 
     } 
     output[i] = total; 
    } 
} 

void Tally_Borda(int Results[BALLOT_SIZE][BALLOT_SIZE], struct Ballot * Ballots[NUM_VOTERS]) { 
    for (int i = 0; i < NUM_VOTERS; i++) { 
     for (int j = 0; j < BALLOT_SIZE; j++) { 
      Results[j][Ballots[i]->votes[j]-1]++; 
     } 
    } 
} 

int Borda_Count(struct Ballot * Ballots[NUM_VOTERS]) { 

    printf("\nBorda Count:\n"); 
    int Results[BALLOT_SIZE][BALLOT_SIZE] ={{0}}; 
    Tally_Borda(Results, Ballots); 
    Print_First_Candidate(Results[0], BALLOT_SIZE); 

    int DotResult[BALLOT_SIZE]; 
    Dot_Product(Results, DotResult); 
    Print_Results(DotResult, BALLOT_SIZE); 

    int finalResult = Argmax(DotResult, BALLOT_SIZE); 
    printf("%d is the Borda Count winner\n", finalResult); 
    return finalResult; 
} 

投票の構造は、投票を表すintの配列への単なるポインタです。

struct Ballot { 
    int *votes; 
}; 

重要な部分は、意図したように要求された場合、私は(そのようARGMAXまたはPrint_Resultsなど)のヘルパー関数について説明したり、ポストコードでしょうが、これは、動作することです。以下は、これらの機能をどのように変更したのか、どうにかして機能を破壊する方法です。私はポインタやダブルポインタにいくつか変更し、コードが実行されますが、私は正しいと確認した最初のバージョンとは非常に異なる結果を持っています。 Tally_Bordaの問題は、結果の最初の行(候補)の結果を印刷して、一致するかどうかを確認しているようです。

void new_Dot_Product(int * a[BALLOT_SIZE], int output[BALLOT_SIZE]) { 

    for (int i = 0; i < BALLOT_SIZE; i++) { 
     int total = 0; 
     for (int j = 0; j < BALLOT_SIZE; j++) { 
      total = total + a[i][j] * (BALLOT_SIZE - (j+1)); 
     } 
     output[i] = total; 
    } 
} 

void new_Tally_Borda(int *Results[BALLOT_SIZE], struct Ballot * Ballots[NUM_VOTERS]) { 
    for (int i = 0; i < NUM_VOTERS; i++) { 
     for (int j = 0; j < BALLOT_SIZE; j++) { 
      Results[j][Ballots[i]->votes[j]-1]++; 
     } 
    } 
} 

int new_Borda_Count(struct Ballot * Ballots[NUM_VOTERS]) { 

    int ** Results = malloc(BALLOT_SIZE*sizeof(int*)); 
    for (int i = 0; i < BALLOT_SIZE; i++) { 
     Results[i] = calloc(1, BALLOT_SIZE); 
    } 

    new_Tally_Borda(Results, Ballots); 
    Print_First_Candidate(Results[0], BALLOT_SIZE); 

    int * DotResult = malloc(BALLOT_SIZE * sizeof(int)); 
    new_Dot_Product(Results, DotResult); 
    Print_Results(DotResult, BALLOT_SIZE); 

    int finalResult = Argmax(DotResult, BALLOT_SIZE); 
    printf("%d is the Borda Count winner\n", finalResult); 
    return finalResult; 
} 

何が欠けていますか?

次の例では、投票者の数が1000であり、元のボルダカウントが1000

サンプルを出力する第一候補の得票合計として正しいよう投票サイズは、15:

new_Borda Count: 
First Candidate: 
Position 1: 50 
Position 2: 56 
Position 3: 64 
Position 4: 75 
Position 5: 137 
Position 6: 142 
Position 7: 142 
Position 8: 143 
Position 9: 190 
Position 10: 201 
Position 11: 196 
Position 12: 204 
Position 13: 281 
Position 14: 267 
Position 15: 267 
Totals: 
Candidate 1: 12176 
Candidate 2: 19270 
Candidate 3: 24010 
Candidate 4: 26582 
Candidate 5: 26043 
Candidate 6: 26198 
Candidate 7: 27427 
Candidate 8: 26731 
Candidate 9: 26693 
Candidate 10: 26525 
Candidate 11: 27081 
Candidate 12: 26318 
Candidate 13: 69270 
Candidate 14: 444370 
Candidate 15: 1242793 
15 is the Borda Count winner 

Borda Count: 
Position 1: 50 
Position 2: 56 
Position 3: 64 
Position 4: 75 
Position 5: 70 
Position 6: 64 
Position 7: 73 
Position 8: 77 
Position 9: 72 
Position 10: 79 
Position 11: 63 
Position 12: 65 
Position 13: 55 
Position 14: 59 
Position 15: 78 
Totals: 
Candidate 1: 6863 
Candidate 2: 7134 
Candidate 3: 7045 
Candidate 4: 7129 
Candidate 5: 6711 
Candidate 6: 6879 
Candidate 7: 7069 
Candidate 8: 6922 
Candidate 9: 7100 
Candidate 10: 7044 
Candidate 11: 7153 
Candidate 12: 6967 
Candidate 13: 7027 
Candidate 14: 6928 
Candidate 15: 7029 
11 is the Borda Count winner 
new_Borda_Count
+0

の2D配列で簡単にどのようなあなたの意味を理解するためにあまりにも多くのコードがありますことを考えると、私にはもっと賢明なようです(さらに、出力の欠如と現在の出力との対比)。 – Evert

+0

それはまさにそれでした! –

答えて

2

この行は、私にはあまり意味がありません:calloc

 Results[i] = calloc(1, BALLOT_SIZE); 

最初の引数があります要素の数、要素のサイズの2番目の要素です。それと

 Results[i] = calloc(BALLOT_SIZE, sizeof *Results[i]); 

はあなたの最初のコード例ではResultsBALLOT_SIZE X BALLOT_SIZE

関連する問題