I am learning C++, and came across an idea about how to remove duplicates from a randomly generated integer array with values from 0 to 100. I coded it like this, but I see that some numbers are repeating. I really can't figure out the bug. Please help.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int item[100];
int z;
int count = 0;
for (z = 0; z < 100; z++) {
item[z] = rand() % (z + 1);
count++;
cout << item[z] << endl;
}
cout << "Total = ";
cout << count << endl;
int i, j;
int NewLength = 1;
for (i = 1; i < 100; i++) {
for (j = 0; j < NewLength; j++) {
if (item[i] == item[j])
break;
}
if (j == NewLength)
item[NewLength++] = item[i];
}
for (int x = 0; x < 100; x++) {
cout << item[x] << endl;
}
}