Lawrence Tran Lawrence Tran - 16 days ago 5
C++ Question

Display duplicate numbers in an array of numbers

I'm making a program that reads in an array of numbers and then only displays the duplicate numbers. However, my code behaves strangely and prints out incorrect outputs (outputs attached at the end). This is my code so far:

#include <iostream>
using namespace std;
const int MAX_NUMBER_ELEMENTS = 20;

void fillArray (int a[], int size, int& numberUsed);
void deleteRepeats (const int a[], int numberUsed, int n);

int main()
{
int array [MAX_NUMBER_ELEMENTS], numberUsed;

cout << "This program reads in an array and scans for duplicate elements. " << endl;
cout << "Enter the array: \n";

fillArray(array, MAX_NUMBER_ELEMENTS, numberUsed);
deleteRepeats(array, numberUsed, 20);
}

void deleteRepeats (const int array[], int numberUsed, int n)
{
int i, j;

for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] == array[j])
cout << "The duplicate " << array[i] << " was found. ";
}
}
}

void fillArray (int a[], int size, int& numberUsed)
{
cout << "Enter up to " << size << " nonnegative whole numbers.\n"
<< "Mark the end of thae list with a negative number.\n";
int next, index = 0;
cin >> next;
while ((next >= 0) && (index < size))
{
a[index] = next;
index++;
cin >> next;
}

numberUsed = index;
}


My input:

This program reads in an array and scans for duplicate elements.
Enter the array:
Enter up to 20 nonnegative whole numbers.
Mark the end of the list with a negative number.
1 1 3 5 -1


My output:

The duplicate 1 was found. The duplicate 1 was found. The duplicate 1 was found. The duplicate 1 was found. The duplicate 1 was found. The duplicate 1 was found. The duplicate 1 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 1 was found. The duplicate 1 was found. The duplicate 1 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found. The duplicate 0 was found.

As you can see, the output is not displaying duplicate numbers. I think it has something to do with the loop, although I don't know how to debug it. Can anyone enlighten me why isn't the code displaying the duplicate elements in the read-in array of numbers? Any help is appreciated. Thanks!

Answer

your loop runs till the maximal end iof the array (so to 20), it seems that your array is initialized with zeros, so your input translates to

1 1 3 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

every 0 reports all zeros behind it -> thats the output you see, your loop has to go to numberUsed not to MAX_NUMBER_ELEMENTS...


here a possible correction (untested, but the idea should be clear):

void deleteRepeats (const int array[], int numberUsed, int n)
{
    int i, j;

    for (i = 0; i < std::min(numberUsed, n); i++)
    {
        for (j = i + 1; j < std::min(numberUsed, n); j++)
        {
            if (array[i] == array[j])
                cout << "The duplicate " << array[i] << " was found. ";
        }
    }
}

hth,

Kai


Addendum: just use (i,j) < numberUsed is dangerous, because you could get out of bounds to uninitialized memory