c - 如何计算一个数组中的元素在另一个数组中出现的次数

标签 c arrays loops

这里是新手。我试图了解如何在两个数组(单词和 uniques)之间循环,以便我可以找出 uniques 数组中的每个元素在单词数组中出现的次数。 这是我的非工作代码,请有人帮我看看我在哪里弄乱了循环?

谢谢!

#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 

最佳答案

执行此操作的有效方法如下: 使用第三个数组: char counts[5] (必须与您的 uniques 数组具有相同的大小) counts[0] 将保存字母“a”在“words”数组中出现的次数,counts[1] 将保存字母“b”在“words”数组中出现的次数,依此类推...

因此,您只需要一个 for 循环即可执行以下操作: 检查words[i]是否在uniques数组中(基本上你检查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',那么你将拥有words[i] - 'a' = 'b' - 'a' = 1(计数数组中的第二个位置)。

您使用了 2 个 for 循环的低效方法。

此外,发现错误的一个好方法,特别是在像这样的小代码中,就是实际拿一张纸和一支笔,并尝试遵循如何在小输入上进行编码。另一种(也是更有效的方法)是逐步调试并实际查看每个变量的值。

希望我说得足够清楚。祝你好运:D

关于c - 如何计算一个数组中的元素在另一个数组中出现的次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39687499/

相关文章:

c - 在 ncurses 上重绘

c - 一个范围内具有完美数字的完美平方

c - 在 C 中,数组中的变量是否有一种模式,它被分配了 0 值?

javascript - 将 mongo db 查询减少为纯数组

c - 将用户输入获取到动态数组 C 时出现问题

c - 在 C 中以 block 的形式将内存写入套接字

php - 从数据库中以数组形式检索数据

loops - 如何在UML序列图中表示中断?

javascript - nodejs 单独的数组

java - 尝试使用 if 语句在 Java for 循环中创建 10 列