My code is complete minus one little flaw. It searches the array and prints out which values are unique, however it always counts the first entry as unique even if it is followed by the same value. Can anyone look at my code and tell me which part is messing this up because it is driving me crazy.
#include <stdio.h>
#define size 7
int main(void)
{
int array1[size], target, answer, found, x, k, prev, count =1, i;
printf("Please input %d integers: ", size);
scanf("%d", &target);
for(x = 0; x < size; x++)
{
scanf("%d", &array1[x]);
}
prev = array1[0];
for (i = 1; i < size; i++)
{
if (array1[i] == prev)
{
count++;
}
else
{
if (count < 2)
printf("%d=%d\n", prev, count);
prev = array1[i];
count = 1;
}
}
if (count < 2)
{
printf("%d=%d\n", prev, count);
}
return 0;
}
I’m confused by your code though, but the error is obvious. For example, if the input array is
9 8 7 9 8 7 9, theprevshould be 9 at first iteration and wheni == 1,prev == array1[i]is false. And we come to the else clause, of course thecountis only1now. I suggest you rewrite the whole things… Either build a array to store the frequency of each entry, or sort the array.