2016-09-25 4 views
0

newb beginner here。私は2つの配列(単語とユニーク)をループする方法を理解しようとしているので、ユニーク配列の各要素が単語配列に何回出現するかを調べることができます。 ここに私の動作しないコードがあります、誰かが私がループを台無しにしているのを見て助けることができますか?ある配列の要素が他の配列に現れる回数を数える方法

ありがとうございます!

#define MAXWORDS 100 
#define ALLWORDS 1000 

int main() 
{ 

int c, state, nowords; 
state = OUT; 

int array[MAXWORDS] = {0}; 
char words[ALLWORDS] = {'0'}; 


for (int i = 0; i < MAXWORDS; ++i) 
    /* printf("%i", array[i]) */; 
/* printf("\n"); */ 

/* Filling an array correctly!!! */ 
int count; 
count = 0; 
int countchars; 
countchars = 0; 
while((c = getchar())!= EOF) 
{ 
    words[countchars++] = c; 
    count++; 

    switch(c) { 
     case ' ': 
     case '\n': 
     case '\t': 
      if(state == IN) 
      { 
       state = OUT; 
       ++nowords; 
      } 
      count = 0; 
      break; 
     default: 
      state = IN; 
      if(count > 0) 
       array[nowords] = count; 
      break; 
    } 
} 

words[countchars + 1] = '\0'; 

printf("number of chars in each word in the sentence: "); 
for (int i = 0; array[i] != 0; i++) 
    printf("%i,", array[i]); 
printf("\n"); 

printf("What was typed and stored into the words array: "); 
for(int k = 0; words[k] != '\0'; k++) 
    printf("%c", words[k]); 
printf("\n"); 

printf("Finding unique chars: "); 
int a, b; 
char uniques[ALLWORDS] = {'0'}; 

for(a = 0; a < countchars; a++) 
{ 
    for(b = 0; b < a; b++) 
    { 
     if(words[a] == words[b]) 
     { 
      break; 
     } 
    } 
    if(a == b) 
    { 
     uniques[a] = words[a]; 
    } 
} 

uniques[a + 1] = '\0'; 

for(int d = 0; d != ALLWORDS; d++) 
    printf("%c", uniques[d]); 
printf("\n"); 

int counting = 0; 
for(int j = 0; j < countchars; j++) 
{ 
    counting = 0; 
    for(int h = 0; h < a; h++) 
    { 
     if(words[h] == uniques[j]) 
      ++counting; 
    } 
    printf("\"%c\": %i ", uniques[j], counting); 
    printf("\n"); 
} 

return 0; 
} 

私は一種の奇妙である。このような出力を取得しています。これを行うための

./homework 
a big fat herd of kittens 
number of chars in each word in the sentence: 1,3,3,4,2,7, 
What was typed and stored into the words array: a big fat herd of kittens 

Finding unique chars: a bigftherdokns 

"a": 2 
" ": 5 
"b": 1 
"i": 2 
"g": 1 
"": 0 
"f": 2 
"": 0 
"t": 3 
"": 0 
"h": 1 
"e": 2 
"r": 1 
"d": 1 
"": 0 
"o": 1 
"": 0 
"": 0 
"k": 1 
"": 0 
"": 0 
"": 0 
"": 0 
"n": 1 
"s": 1 
" 
": 1 
+0

とは何ですか? –

+0

'a'はどのように初期化されましたか?うまくいけば、 'strlen(words);'ですか? –

+0

申し訳ありません。 countcharsとaは配列の長さを数えるために使われる変数です。単語はユーザの入力から収集され、ユニークは単語のユニークな要素を数えることから得られる。ミックスアップには申し訳ありません。 – Sina

答えて

0

効率的な方法は以下の通りです: 3番目の配列を使用:文字カウントを[5](必須ユニーク配列と同じサイズ) counts [0]は「words」配列に文字「a」が現れる回数を保持します。counts [1]は「b」が「words」に現れる回数を保持します"配列など...

したがって、あなたは歌が必要ですle forループを実行します。 words [i]がユニーク配列にあるかどうかを確認します(基本的にwords [i]が< = 'e'かどうかを確認します)。 words [i]が本当に< = 'e'の場合は、counts [words [i] - 'a']をインクリメントします。

ここで、「words [i] - 'a'」が実際にどのように機能するかを見てみましょう。 words [i]が 'a'の場合、words [i] - 'a' = 'a' - 'a' = 0(counts配列の最初の位置) words [i]が 'b'ならばあなたは単語[i] - 'a' = 'b' - 'a' = 1(counts配列の2番目の位置)を持ちます。

2 forループで非効率的なアプローチを使用します。

また、特にこのような小さなコードでバグを見つける良い方法は、実際に紙とペンを取り、小さな入力で作業をコーディングする方法に従うことです。もう1つ(より効率的な方法)は、段階的にデバッグし、実際に各変数が保持する値を確認することです。

私は十分にはっきりしていました。幸運:D

+0

あなたはあまり明確ではありません。 – Sina

関連する問題