So this code I'm trying to calculate the number of distinct values entered into an array. If i enter the followings
"3,4,5,6,7,7,6,e (anything that's not a number)"
i get a total of 7 but in reality it should be a 5. can anyone help out?
Code:
#include <stdio.h>
//---------function to find the distinct values----
//------ need help here----------
int find_distinct(int list[], int size)
{
int i, j,size2, distinct = 0;
for(i = 0; i < size; i++)
{
for(j=i+1;j<size;j++)
{
if(list[i] == list[j])
{
continue;
}
else
{
distinct++;
}
}
return distinct;
}
}
int main()
{
//-------variables------------------
int list[1000]; // can hold 1000 items
int i, element, distinct;
int ret;
printf("Enter in as much number as you want and it'll calculate:\nthe number of distinct values. \n");
printf("Type in a non integer when you're done\n");
//------ user puts in array 1000 times or until they press a non integar
for (i = 0; i < 1000; ++i)
{
ret = scanf("%i", &list[i]);
if(ret == 0)
{
element = i++;
distinct = find_distinct(list, element);
printf("\nThe distinct value: %i", distinct);
getch();
}
}
}