I have an error with my code where I am trying to get the duplicates and remove them.
however when I try this it doesn't remove anything and still gives me the same values in the output as the original.
Down below I displayed the two methods.
void print_array just prints out the array.
void removeDups I want is to get the duplicates and remove them and print the new array.
Let me know where the error occurs.
Also pointer notation would be recommended. Thank you!
void removeDups(int *array, int *length)
{
*length = 10;
int i, j;
for(i = 0; i < *length; i++)
{
for(j = 0; j < *length; j++)
{
if(array[i] == array[j])
{
array[j] = array[*length-2];
length++;
}
}
}
printf("Number of new elements: %d,", *length);
printf(" new elements: ");
for(i = 0; i < *length; i++)
{
printf("%d " , array[i]);
}
}
void print_array(int *array, int length)
{
printf("Number of elements: %d,", length);
int i;
printf(" Orginal elements: ");
for(i = 0; i < length; i++)
{
printf("%d " , array[i]);
}
}
int main()
{
int array [10];
int i, number;
int size = 10;
int *length;
length = &size;
for(i = 0; i < size; i++)
{
number = rand() % 10 + 1;
array[i] = number;
}
print_array(array, size);
printf("\n");
removeDups(array, length);
return 0;
}
output:
Number of elements: 10, Orginal elements: 4 7 8 6 4 6 7 3 10 2
Number of new elements: 3, new elements: 10 6 8
void removeDups(int *array, int *length)where you uselengthwithin as if it isintrather than theint *it is declared to be. Whatever tutorial or book you're using should have ample instruction on using pointers.