提取数组中的唯一元素(来自 K 和 R C ex1-14)

Extracting unique elements in an array (from K and R C ex1-14)

这里又回到了C newb。我正在尝试 K 和 R C 的练习,在尝试练习 1-14 的路上,我真的很困惑。 我的解决方案有效但并不总是正确的,我正在寻求帮助来完善我所写的内容,或者是否有更好的(更容易理解!)方式! 我的脚本:

#include <stdio.h>

/* How many times a character appears in an array */
main()
{
    int c;
    int count = 0;
    int uniquecount = 0;
    char array[20];
array[0] = '[=10=]';

while((c = getchar()) != EOF)
{
    array[++count] = c;
}

/* for each element in array,
 * check if array[each] in newarray.
 * if array[each] in newarray
 * break and start checking again.
 * if array[each] not in newarray
 * add array[each] to end of newarray*/

printf("count = %d\n", count);
array[count] = '[=10=]';
char newarray[count];
newarray[0] = '[=10=]';

for(int a = 0; a < count; ++a)
{
    for(int b = 0; b <= a; ++b)
    {
        if(newarray[b] == array[a])
            break;
        if(newarray[b] != array[b])
        {
            newarray[b] = array[b];
            ++uniquecount;
        }

    }
}

printf("uniquecount = %d\n", uniquecount);    
newarray[uniquecount + 1] = '[=10=]';

printf("array => ");
for(int i = 0; i < count; ++i)
    printf("\'%c\'", array[i]);
printf("\n");
printf("newarray => ");
for(int i = 0; i < uniquecount + 1; ++i)
{
    if(newarray[i] != '[=10=]')
        printf("\'%c\'", newarray[i]);
}
printf("\n");

}

当我尝试一些简单的字符串时,它起作用了,有时却不起作用:

./times_in_array 
this is 
count = 9
uniquecount = 5
array => '''t''h''i''s'' ''i''s'' '
newarray => 't''h''i''s'' '
 ./times_in_array 
something comes
count = 16
uniquecount = 11
array => '''s''o''m''e''t''h''i''n''g'' ''c''o''m''e''s'
newarray => 's''o''m''e''t''h''i''n''g'' ''c'
 ./times_in_array 
another goes
count = 13
uniquecount = 12
array => '''a''n''o''t''h''e''r'' ''g''o''e''s'
newarray => 'a''n''o''t''h''e''r'' ''g''o''e''s'

有人可以指导我错在哪里吗?非常感谢!

获取数组的唯一元素

a) sort it
b) mark element one as unique
v) iterate over it from i = 1 to i = N-1 and mark an element as unique if array[i-1] != array[i]

假设您需要每个重复元素的一个副本。要消除重复元素,请修改算法以查找同样不等于其上方元素的元素。

您的代码存在不正确的字符串比较问题。

我们最常写int main(void)而不是main()

此外,了解当您像这样访问新数组时非常重要:

newarray[b]

您实际上正在访问未初始化内存。然而,由于随机字符匹配 array[a] 的概率太小,你似乎逃脱了。

出于这个原因,我建议您像这样初始化新数组:

for(int i = 0; i < count; ++i)
    newarray[i] = '[=11=]';

现在,我不提供我的解决方案,而是坚持让你明白你做错了什么;这就是你教育自己的方式。在内部循环开始时打印数据真的很有帮助,例如:

printf("b = %d, a = %d, newarray[b] = %c, array[a] = %c, array[b] = %c\n", b, a, newarray[b], array[a], array[b]);

并在增加唯一计数器时打印一条消息,如下所示:

printf("UNIQUE, %d\n", uniquecount);

当你执行你的程序时,你会看到:

...
b = 9, a = 12, newarray[b] = g, array[a] = s, array[b] = g
b = 10, a = 12, newarray[b] = , array[a] = s, array[b] = o
UNIEUQ, 10
b = 11, a = 12, newarray[b] = , array[a] = s, array[b] = e
UNIEUQ, 11
b = 12, a = 12, newarray[b] = , array[a] = s, array[b] = s
UNIEUQ, 12
uniquecount = 12
array => '''a''n''o''t''h''e''r'' ''g''o''e''s'
newarray => 'a''n''o''t''h''e''r'' ''g''o''e''s'

强烈提示您哪里出了问题。在 's'(goes 的最后一个字母)第一次被发现是唯一的(这很好)之后,我们不会中断循环,因此您的代码将继续检查新数组并被愚弄这个 's'又是独一无二的。

所以当你找到一个独特的元素时添加一个中断,你现在应该没问题:

++uniquecount;
break;