Thread: Question about duplicate in array algorithm (that seems to work sometimes, maybe not)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question Question about duplicate in array algorithm (that seems to work sometimes, maybe not)

    Hi! I'm having a problem with this algorithm that is supposed to remove duplicates from the array. The problem is that when chaging the number after the modulus ( % ) sign, the console just stands and blinks and when I press enter it does not close.

    While some numbers seem to be fine, but maybe isn't? I think it is sorting the array if I have implemented the algorithm correctly, but I think there still are problems with my code, right?

    So why does the console just stop? And seem to wait for something (it maybe isn't waiting for something but it looks like that for me).

    Some of the comments are for the original values that were set in the program.


    Edit:

    I noticed that changing the constant named numberOfValues to the same number as the number
    behind the modulus ( % ) sign fixed the problem, so that the console will output the array. What
    I don't understand however is how numberOfValues can be related somehow to the number behind
    modulus? Or what is really happening?
    Code:
    #include<iostream>
    #include<cstdlib>
    #include<ctime>
    
    int main()
    {
        std::srand(std::time(nullptr)); // Set the random seed.
        const int numberOfValues = 10;
        int valueArray[numberOfValues] = {0};
    
        int counter = numberOfValues;
        for(int i = 0; i < numberOfValues; ++i)
        {
            // Here is the problem when changing the number after
            // modulus to another number, sometimes it doesn't work.
            valueArray[i] = std::rand() % 5 + 1; //Give a random value between [1, 2000].
            //Add checks to make sure that the value is unique
    
            for (int j = 0; j < i; j++) {
                if (valueArray[i] == valueArray[j]) {
                    counter--;
                    for (int k = i; k < numberOfValues; k++) {
                        valueArray[k] = valueArray[k + 1];
                    }
                    i--;
                }
            }
        }
    
        //Present all values    
        /* Divide the size of all numbers stored in the array by the size of one number in the array
         * An int is 4 bytes which gives me 4 * n, where n is the number of characters in the array.
         * Then divide (4 * n) by 4 and we get n. */
        int n = sizeof(valueArray) / sizeof(valueArray[0]);
        for (int i = 0; i < n; i++) {
            std::cout << valueArray[i] << std::endl;
        }
    
        getchar();
    }
    
    Last edited by DecoratorFawn82; 09-07-2018 at 05:25 PM.

  2. #2
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    67
    The problem is that the modulus has to produce a range of values greater than the size of the array. Think about it. If the array size is 10 and the modulus is 5 then the only number you can produce is a value between 1 and 5. But you need 10 unique values! In other words the loop never completes because the array isn't ever completely filled.
    Last edited by Sir Galahad; 09-07-2018 at 05:56 PM.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Thank you! That is a very clear answer. I understand why that is happening now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-11-2017, 06:19 AM
  2. Please help with Printing duplicate array elements
    By TheSprawl in forum C Programming
    Replies: 9
    Last Post: 11-23-2011, 11:22 PM
  3. algorithm for duplicate file checking help
    By geekoftheweek in forum C Programming
    Replies: 1
    Last Post: 04-04-2009, 01:46 PM
  4. duplicate detection algorithm
    By Gustaff in forum C Programming
    Replies: 4
    Last Post: 01-28-2003, 12:26 PM
  5. Duplicate values in Array
    By TONYMX3 in forum C++ Programming
    Replies: 2
    Last Post: 01-30-2002, 03:57 PM

Tags for this Thread