Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 确定阵列中元素的频率_C - Fatal编程技术网

C 确定阵列中元素的频率

C 确定阵列中元素的频率,c,C,假设我有一个颜色数组,数组中的数字代表不同的颜色,我试图计算每个颜色在该数组中出现的次数 这是我的密码: 我已经声明了此函数所需的变量: 我理解这一部分,只是创建数组 printf("Enter the color combination , separated by a space:\n"); for(i = 0; i<slots; i++){ scanf("%d", &correct[i]); for(i=0;i我将尝试为您解释您的代码。:) 有两个大小相等

假设我有一个颜色数组,数组中的数字代表不同的颜色,我试图计算每个颜色在该数组中出现的次数 这是我的密码: 我已经声明了此函数所需的变量:

我理解这一部分,只是创建数组

printf("Enter the color combination , separated by a space:\n");
    for(i = 0; i<slots; i++){
    scanf("%d", &correct[i]);


for(i=0;i我将尝试为您解释您的代码。:)

有两个大小相等的数组

例如,一个数组具有颜色值

int correct[] = {3,3,4,2,4,1,3};
另一个数组称为
freq

int freq[sizeof( correct ) / sizeof( *correct )]; 
这相当于

int freq[7];
最后一个数组的所有元素最初设置为-1(
freq[i]=-1;

所以它也可以定义为

int freq[7] = { -1, -1, -1, -1, -1, -1, -1 };
然后在这些循环中

for(i=0; i<slots; i++){
count = 1;
    for(j=i+1; j<slots; j++){
        if(correct[i]==correct[j]){
            count++;
            freq[j]=0;
        }
    }

    if(freq[i]!= 0){
        freq[i] = count;
    }
}
执行i等于0的内部循环后,您将

correct[] = {  3,  3,  4,  2,  4,  1,  3 };
freq[7]   = { -1,  0, -1, -1, -1, -1,  0 };
                   ^                   ^
correct[] = {  3,  3,  4,  2,  4,  1,  3 };
freq[7]   = {  3,  0,  2,  1,  0,  1,  0 };
遇到了两次正确的值[0]。因此,变量计数将等于3。它写入数组的第一个元素
freq

int freq[sizeof( correct ) / sizeof( *correct )]; 
你会得到

correct[] = {  3,  3,  4,  2,  4,  1,  3 };
freq[7]   = {  3,  0, -1, -1, -1, -1,  0 };
The color 3 appears 3 time(s).
The color 4 appears 2 time(s).
The color 2 appears 1 time(s).
The color 1 appears 1 time(s).
然后,对于i等于1,将跳过内部循环(我在重写时考虑修改后的代码段)

因为i等于2,在执行内部循环之后

correct[] = {  3,  3,  4,  2,  4,  1,  3 };
freq[7]   = {  3,  0, -1, -1,  0, -1,  0 };
                               ^
count等于2,其值将写入数组freq的第三个元素中

correct[] = {  3,  3,  4,  2,  4,  1,  3 };
freq[7]   = {  3,  0,  2, -1,  0, -1,  0 };
执行外部循环后,您将有

correct[] = {  3,  3,  4,  2,  4,  1,  3 };
freq[7]   = { -1,  0, -1, -1, -1, -1,  0 };
                   ^                   ^
correct[] = {  3,  3,  4,  2,  4,  1,  3 };
freq[7]   = {  3,  0,  2,  1,  0,  1,  0 };
现在使用这个循环

printf("Correct combination:\n");
for(i=0;i<slots;i++){
    if(freq[i]!=0)
        printf("The color %d appears %d time(s).\n", correct[i], freq[i]);
}

调试器将为您提供一步一步的行为……数据比代码更重要,并且缺少其声明。这使得跟踪无意义的单字母数组索引发生的情况变得很尴尬。我建议,既然你掌握了所有的信息,你应该自己用调试器一步一步地检查代码,记录每个阶段发生的事情,正如@OliverCharlesworth所建议的那样。“这是我的代码……这部分做什么?”。你不知道“你自己的”代码吗?@Morgan Ariel Henry你能解释一下为什么输出“颜色1显示1次”。“颜色4显示2次”。?请发布一篇文章。
The color 3 appears 3 time(s).
The color 4 appears 2 time(s).
The color 2 appears 1 time(s).
The color 1 appears 1 time(s).