The following program is supposed to remove duplicates from a sequence of numbers. For example, running 7, 7, 7, 2, 1 thru the program should return 7, 2, 1 (actually 7, 2, 1, 0, 0).
However, anytime I comment out "int unique_list = 0;" in the main, the program misbehaves, printing out random numbers.
Can someone please advise?
Thank you very much,
#include <stdio.h>
#define MAXNUMS 5
void RemoveDuplicates(int list[]);
int main()
{
int idx, numbers[MAXNUMS - 1];
int unique_list = 0;
printf("Enter %d numbers.\n", MAXNUMS);
for(idx = 0; idx < MAXNUMS; idx++) {
printf("input number %d: ", idx + 1);
scanf("%d", &numbers[idx]);
}
printf("pass");
RemoveDuplicates(&numbers);
printf("\npass 1");
printf("\nThe new list is:\n");
for (idx = 0; idx < MAXNUMS; idx++)
printf("%d\n", numbers[idx]);
}
/* The list contains MAXNUMS integers */
/* Also, all duplicate elements are converted to 0 */
void RemoveDuplicates(int list[])
{
int i;
int j;
int found;
int unique_list = 0;
for (i = 0; i < MAXNUMS; i++) {
found = 0;
for (j = 0; j < unique_list; j++) {
if (list[j] == list[i])
found = 1;
break;
}
if (!found) {
list[unique_list] = list[i];
unique_list++;
}
}
/* clean up the remainder of the list */
for (j = unique_list; j < MAXNUMS; j++)
list[j] = 0;
return;
}