0

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;
  }
}
mlhazan
  • 29
  • 5
  • Title should be remove duplicate numbers from randomly generated array. You aren't removing `duplicate arrays` because you don't have multi-dimensional array. – Brandon May 08 '14 at 03:48
  • What exactly are you trying to do? – ooga May 08 '14 at 03:50
  • OP is trying to generate a random array of unique digits.. I think. – Brandon May 08 '14 at 03:51
  • 2
    If you don't want duplicates, it's generally best to generate the data without duplicates in the first place, rather than generate with dupes, then remove the extras you didn't want in the first place. – Jerry Coffin May 08 '14 at 03:53
  • do you think this is not working? for (z = 0; z < 100; z++) { item[z] = rand() % (z + 1); cout << item[z] << endl; } – mlhazan May 08 '14 at 04:36
  • Whether that loop is working or not depends upon what you intend it to do. So, what do you intend it to do? – Benjamin Lindley May 08 '14 at 04:50
  • possible duplicate of [Creating random numbers with no duplicates](http://stackoverflow.com/questions/4040001/creating-random-numbers-with-no-duplicates) – Mooing Duck May 08 '14 at 20:08

4 Answers4

0

You start with an array of 100 integers. Assuming some of the integers are duplicates, there are less than 100 unique integers. So, since there are less than 100 unique integers in your array, unless your algorithm generates new integer values which were not already in the original sequence, you can be certain you will see duplicates if you print out all 100 values of the array, as you do at the end or your program.

If you only print up to NewLength, you shouldn't see any duplicates (assuming your algorithm is correct, I didn't check it rigorously).

for (int x = 0; x < NewLength; x++) {
    cout << item[x] << endl;
}
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
0

If you can use C++11 and do NOT mind doing so, what you can do is store numbers 0 to 100 in an array.

Next you can shuffle them randomly and voila.. Array of unique numbers from 0 to 100 in a random order. Otherwise if you can't, ignore this solution.

#include <random>
#include <numeric>
#include <algorithm>
#include <iostream>


int main()
{
    int arr[100] = {0};

    std::iota(&arr[0], &arr[100], 0);

    std::random_device rd;
    std::mt19937 mt(rd());

    std::shuffle(&arr[0], &arr[100], mt);

    for (int i = 0; i < 100; ++i)
        std::cout<<arr[i]<<" ";
}

Another option is to generate an array of 0 to 100 and swap indexes at random (assuming you cannot use C++11 and random_shuffle).

Brandon
  • 22,723
  • 11
  • 93
  • 186
  • This does not answer the OP's question. – R Sahu May 08 '14 at 04:36
  • Exactly how does it NOT answer the question? It generates a random array of integers from 0 to 100 without duplicates. OP is attempting the exact same thing. It will NEVER work the way they have it now. Frankly, generating random numbers then removing the duplicates is a bad idea. The only reasonable solution is to shuffle the array. I also stated that if they cannot use C++11, then they can randomly swap indices in the array which will solve their problem. Compare the code length of either solution (swap or shuffle vs. OP's code and other snippets). – Brandon May 08 '14 at 06:18
0
#include <ctime>

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;

      //Assume if duplicate found assign -1 in the place of duplicate

      for(int i = 0;i<100;++i)
      {
          if(item[i] == -1) //If this number already checked & it is a duplicate
              continue;

          for(int j=i+1;j<99;++j)
          {
              if(item[i] == item[j])
              {
                  //Duplicate found.
                  item[j] = -1; //Assign -1 in the place of duplicate
              }
          }
      }

         for (z = 0; z < 100; z++) {
             if(item[z] != -1)
                cout << item[z] << endl;
      }


        char c;
        cin >> c;
    return 0;
}
user3607571
  • 126
  • 2
0

Here is yet another "remove duplicate from a randomly generated integer array".

This line of code below reverses one iteration if a duplicate random number is encountered.

for(int j=0;j<i;j++) if (random_once[j]==random_once[i]) i--;

Entire code:

#include <ctime>
#include <iostream>
using namespace std;


int main()
{

      int size=100 ;
      int random_once[100 ];

      srand(time(0));

      for (int i=0;i<size;i++)  
      {
          random_once[i]=rand() % 100;

          // generate unique random number only once
          for(int j=0;j<i;j++) if (random_once[j]==random_once[i]) i--;  

      }
      cout<<" "<< i <<"\n\n\n ";
      for ( i=0;i<size;i++) cout<<" "<<random_once[i]<<"\t";

  return 0;
}

Output:

  60     61      96      77      89      94      31      87      19      24
 27      99      64      7       8       88      62      75      37      97
 28      55      92      48      91      47      50      13      36      85
 21      34      82      23      40      4       79      90      46      10
 18      0       2       74      56      33      70      84      49      73
 69      57      32      41      83      67      76      43      35      44
 80      14      25      93      11      15      63      9       26      16
 5       98      17      38      51      30      53      58      3       65
 66      52      95      86      78      29      42      12      45      59
 81      71      39      6       1       68      20      72      54      22

Press any key to continue
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
  • I did not get it.in the first loop i = 0 and j= 0 but j – mlhazan May 09 '14 at 04:28
  • the variable i is the randome_one[] buffer array, j is the check for duplicates, j goes through the random_once[] buffer from 0 to i at every iteration scanning for duplicates. – Software_Designer May 09 '14 at 07:02