英文:
The count of duplicate elements is double what it should be
问题
我正在处理一份旧考试题目,题目说明一个给定的数组 (int zahlen[]={1,4,5,1,5,7,9,2,3,4}
) 中有相同的值。任务是将相同的值替换为 '-1'。每次替换后,要增加一个给定的变量 count
值。
我的问题是变量 count
的值是正常值的两倍(在这种情况下,只有3个相同的数字,但变量显示为6)。
该函数名为 array_unique
。我将感激对我的错误进行简要解释。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("chcp 1252");
int zahlen[]={1,4,5,1,5,7,9,2,3,4};
int len = sizeof(zahlen)/sizeof(int);
int erg = array_unique(zahlen,len);
printf("Es wurden %d doppelte Zahlen gelöscht: \n",erg);
printf("Das Array hat nun folgende Werte: ");
printArrayUnique(zahlen,len);
return 0;
}
void printArrayUnique(int *array, int len){
for(int i=0; i<len; i++){
if(array[i]!=-1){
printf("%d ",array[i]);
}
}
}
int array_unique(int *array, int len){
int count=0;
for(int i=0; i<len;i++){
for(int j=i+1; j<len;j++){
if(array[i]==array[j]){
array[j] = -1;
count++;
}
}
}
return count;
}
我没有找到其他解决方案来修复 count
的错误值。
英文:
I am working on an old exam and the problems states that a given array (int zahlen[]={1,4,5,1,5,7,9,2,3,4}
) has values that are the same. The task is to replace the values that are the same with '-1'. After each replacement, a given variable, count
, has to be increased by one.
My problem is that the variable count
is two-times higher than normal (In this case there are only 3 of the same numbers and the variable shows 6.)
The function is called array_unique
. I am would be grateful for a brief explanation of my mistake.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
system("chcp 1252");
int zahlen[]={1,4,5,1,5,7,9,2,3,4};
int len = sizeof(zahlen)/sizeof(int);
int erg = array_unique(zahlen,len);
printf("Es wurden %d doppelte Zahlen gelöscht: \n",erg);
printf("Das Array hat nun folgende Werte: ");
printArrayUnique(zahlen,len);
return 0;
}
void printArrayUnique(int *array, int len){
for(int i=0; i<len; i++){
if(array[i]!=-1){
printf("%d ",array[i]);
}
}
}
int array_unique(int *array, int len){
int count=0;
for(int i=0; i<len;i++){
for(int j=i+1; j<len;j++){
if(array[i]==array[j]){
array[j] = -1;
count++;
}
}
}
return count;
}
I have not figured out any other solution to fix the faulty value of count
.
答案1
得分: 1
问题是因为你多次计算了重复项;所以,当你找到重复的条目时,你会正确地将其替换为 -1,但是后来在循环中,你可能会比较两个或更多的 -1 值。
只需在递增 count
变量之前添加一个检查,检查两个值都不是 -1(以及相等测试):
int array_unique(int* array, int len)
{
int count = 0;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (array[i] == array[j] && array[j] != -1) {
array[j] = -1;
count++;
}
}
}
return count;
}
还要注意,正如评论中提到的那样,在使用函数之前,你确实需要声明它们。在 main
函数之前添加以下两行:
void printArrayUnique(int* array, int len);
int array_unique(int* array, int len);
英文:
The issue is due to the fact that your are counting duplicates more than once; so, when you have found a duplicate entry, you correctly replace that with -1 but then, later in the loops, you will be (potentially, at least) comparing two or more of those -1 values.
Just add a check that either value is not -1 (along with the test for equality) before incrementing the count
variable:
int array_unique(int* array, int len)
{
int count = 0;
for (int i = 0; i < len; i++) {
for (int j = i + 1; j < len; j++) {
if (array[i] == array[j] && array[j] != -1) {
array[j] = -1;
count++;
}
}
}
return count;
}
Note also that, as mentioned in the comments, you really do need declarations of your functions before you use them. Add the following two lines before the main
function:
void printArrayUnique(int* array, int len);
int array_unique(int* array, int len);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论