Hello all,

This is my first time posting in this forum. I searched and found a similar post from years ago, but was unable to find an answer. I'm hoping some of you may be able to help me.

I've been assigned a problem in which 20 integers must be read and only non-duplicate entries must be inserted into an array. The exact phrasing of the problem is as follows:

Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array only if it isn’t a duplicate of a number already read. After reading all the values, display only the unique values that the user entered. Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve this problem.

I'm experiencing difficulty in checking for previous entries as well as inputting them into the smallest array possible. You'll find comments in my code in the parts that I know are incorrect:

#include "stdafx.h"
#include <iostream>
using namespace std;

double desiredEntry;

void duplicateCheckAndInput( double noDupesArray[] )
{
    for ( int counter = 1; counter <= 20; counter++ )
    {
        cout << "Please enter a value for array element #" << counter << endl;
        cin >> desiredEntry;

        // Will only check against current counter value, not all previous values
        if( desiredEntry != noDupesArray[counter] )
                noDupesArray[counter] = desiredEntry;
    } // end for
} // end function

int main()
{
    double noDupesArray[]; // Must have a size or value(s)

    // Call method;
    duplicateCheckAndInput(noDupesArray);


    // Output the results: I'm using an upper limit of 20, but the actual array may not be that large
    for(int outputCounter = 0; outputCounter < 20; outputCounter++)
        cout << noDupesArray[outputCounter] << endl;


    system("pause");
    return 0;
}

I'm aware that my current code doesn't account for integers between 10 and 100, inclusive, but I'd rather get the basic validation and entry solved and then implement that part.

Any help is greatly appreciated. Thank you.

put the condition on line 15 in a loop that checks if the current input has already been used by a previous element, set the index of the array to the counter of that loop

Another thing you could do is to have a boolean function of checking if there are duplicates in the array. Inside of that function, a for which goes from 0 to array size. So it would take as parameters an array and an int containing its size, and the element to be inserted. Inside of that for, if the element is to be found it will return false, otherwise it will return true. After you done the validation, if the function returns true, you'll add the element at the end of the array, and increase its size by 1, or if it returns false you output the error.

Thanks for getting back to me, guys. I appreciate it.

The array size is unknown because only non-duplicate entries will dictate it's size. For example, if only the integer 5 was entered 20 times, then the array size would only be 1. If it were 5 and 6, then the array size would be 2, and so on.

Also, I understand the idea behind a looped boolean function that will return a true/false statement for whether or not an entry is a duplicate, but I have absolutely no idea how to implement the validation.

For example:

boolean dupeCheck (currentEntry)
{
    if( currentEntry != previousEntries)
        return true;
    else
        return false;
}

Is about all I can come up with. Naturally, this won't do anything for me. I need to do something that will store all the previous entries and compare the current entry to the previous ones, but I'm lost as to how to do that.

I look forward to hearing more responses. Thank you again for the help, guys.

How about using a pointer for an array? For example an integer array would be

int *data;

You would use realloc to create additional storage space to add the new unique number to the array after you have searched thru the array to determine that the new number is in fact unique.

Here's an example of what I was talking about:

Data model{
    array=record (an array of type Elem);
    size=int (size of the array);
    elem=an element of type Elem;
}

subalg noDup(array, size, elem)
    if size=0 then //if size is 0, then the array is empty, so no need for a validation
        noDup<-true
    end_if
    for i=0 to i<size do 
        if elem=array[i] then //if the element is to be found, it will return false
            noDup<-false
        end_if
    end_for
    noDup<-true //if the functions gets troughtout the for, and comes to this line, that means
                //that there are no duplicates, so the return value should be true.
end_Subalg

If you do have other questions, perhaps more pseudo-code function will be at hand, but only if we see you've tried and didn't succeed.

Edited 4 Years Ago by Lucaci Andrew

Thanks for getting back to me. I worked on it quite a bit last night and was able to come up with something rather similar to that.

For now, I decided that I should just write a program that can detect duplicates and then I can write the remainder of the assignment around that program.

Here's what I've got:

#include "stdafx.h"
#include <iostream>
using namespace std;


int previousEntries[20];
int currentEntryCounter;

bool isItADupe( int currentEntry )
{
    for( int counter = 0; counter = 19; counter++)
    {
        if (currentEntry == previousEntries[counter])
        {
            return true;
            break;
        } // end if
        else
            if( counter == 20 ) // if the loop finishes without finding a duplicate
                return false;
    } // end for
} // end function

int main()
{
    cout << "\nFirst Input" << endl; // First input - no validation necessary
    cin >> previousEntries[0];

    for( currentEntryCounter = 1; currentEntryCounter = 19; currentEntryCounter++)
    {
        cout << "\nInput" << endl;
        cin >> previousEntries[currentEntryCounter];

        if( isItADupe(previousEntries[currentEntryCounter]) == true )
            cout << "Duplicate detected." << endl;
        if( isItADupe(previousEntries[currentEntryCounter]) == false )
            cout << "No duplicate detected." << endl;
    }

    system("pause");
    return 0;

The problem I'm having at the moment is that my function always returns true (for finding a duplicate) and never false! I feel like it's something small that I'm missing here.

Any ideas?

SUCCESS! ... Well, somewhat.

After tweeking my program and reading over the previous post, I was able to get my program to check for duplicates perfectly!

The trouble I'm having now is getting the non-duplicate entries into a separate, smaller array.

What I've chosen to do is to add 2 more arrays (one as a large non-duplicate array, one as the final, smaller non-duplicate array) and a counter for all non-duplicate entries. When a non-duplicate entry is recognized, it's stored into the large non-duplicate array (with an index of the new counter) and then the counter is incremented by 1. Once the entries are all finished being entered and validated, the smaller array is declared with a size equal to that of the new counter.

Here's what I've got (you may also notice validation for entries outside of 10 and 100):

#include "stdafx.h"
#include <iostream>
using namespace std;

// Defining global variables:
int previousEntries[20];
int currentEntryCounter;


bool isItADupe( int ); // function prototype

int main()
{
    // Defining local variables:
    int nonDupesCounter = 0;
    int bigNonDupesArray[20];

    // First input - No validation required
    cout << "\nFirst Input - Enter an integer between 10 and 100, inclusive." << endl;
    cin >> previousEntries[0];

    // Validating if entry is between 10 and 100, inclusive:
    if( previousEntries[0] < 10 || previousEntries[0] > 100 )
    {
        cout << "\nInvalid input. Setting to default of 10." << endl;
        previousEntries[0] = 10;
    } // end if

    bigNonDupesArray[nonDupesCounter] = previousEntries[0];
    cout << "Storing in bigNonDupesArray[" << nonDupesCounter << "]" << endl;
    nonDupesCounter++;

    // Validation for 2nd through 20th inputs. If non-duplicate, store in a separate array:
    for( currentEntryCounter = 1; currentEntryCounter <= 19; currentEntryCounter++)
    {
        cout << "\nInput" << endl;
        cin >> previousEntries[currentEntryCounter];

        // Validating if entry is between 10 and 100, inclusive:
        if( previousEntries[currentEntryCounter] < 10 || previousEntries[currentEntryCounter] > 100 )
        {
            cout << "\nInvalid input. Setting to default of 10." << endl;
            previousEntries[currentEntryCounter] = 10;
        } // end if

        // Calling isItADupe function, validating, and storing if non-duplicate:
        if( isItADupe(previousEntries[currentEntryCounter] ) == true )
            cout << "Duplicate detected." << endl;
        if( isItADupe(previousEntries[currentEntryCounter] ) == false )
        {
            cout << "No duplicate detected. Storing in bigNonDupesArray[" << nonDupesCounter << "]" << endl;
            bigNonDupesArray[nonDupesCounter] = previousEntries[currentEntryCounter];
            nonDupesCounter++;
        } // end if
    } // end for

    // Declare a smaller array that will only hold non-duplicates:
    int smallNonDupesArray[nonDupesCounter];

    // Take the larger array values and input them into the smaller array
    for( int smallCounter = 0; smallCounter <= nonDupesCounter; smallCounter++)
        smallNonDupesArray[smallCounter] = bigNonDupesArray[smallCounter];

    // Output all of the values for the smaller array
    cout << "Outputting the smaller array with only non-duplicate values: \n" << endl;

    for( int smallOutputCounter = 0; smallOutputCounter <= nonDupesCounter; smallOutputCounter++)
        cout << smallNonDupesArray[smallOutputCounter] << endl;


    // Pause to display results and return 0 for terminating successfully:
    system("pause");
    return 0;
}

bool isItADupe( int currentEntry ) // Begin function definition
{
    for( int counter = 0; counter < currentEntryCounter; counter++)
    {
        // Return true if a duplicate is found:
        if (currentEntry == previousEntries[counter])
        {
            return true;
            break;
        } // end if
    } // end for
            return false; // Return false if no duplicate is found
} // end function

But I'm having compiler issues. They all appear at line 57:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'smallNonDupesArray': unknown size

I have a feeling that this is an issue that has to do with scope. Because the final value of the nonDupesCounter is within a separate scope, it can't be used as a value to declare the size of the smallNonDupesArray.

I feel like I'm really close here, guys. How do I work around this?

But I'm having compiler issues. They all appear at line 57:
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'smallNonDupesArray': unknown size

error C2057 indicates that you are using a variable which can be modified to define your smallNonDupesArray. That is, nonDupesCounter integer can be (and is) modified in your code. smallNonDupesArray has to be defined with a constant as indicated below:

const int nonDupesCounter = 5;

This now prevents you from modifying nonDupesCounter in your code. Any attempt to modify nonDupesCounter will generate another error.

The other two errors are a byproduct of this original C2057 error.

But I'm having compiler issues. They all appear at line 57:

What compiler do you have? It compiles fine under MinGW 4.6.1, if I get out the system("pause") thing and the stdafx header.
As I said it before, this could be done without those two global arrays, if you would have followed my pseudocode.

/* 
 *    Output
 */
First Input - Enter an integer between 10 and 100, inclusive.
10
Storing in bigNonDupesArray[0]

Input
11
No duplicate detected. Storing in bigNonDupesArray[1]

Input
12
No duplicate detected. Storing in bigNonDupesArray[2]

Input
13
No duplicate detected. Storing in bigNonDupesArray[3]

Input
14
No duplicate detected. Storing in bigNonDupesArray[4]

Input
15
No duplicate detected. Storing in bigNonDupesArray[5]

Input
16
No duplicate detected. Storing in bigNonDupesArray[6]

Input
17
No duplicate detected. Storing in bigNonDupesArray[7]

Input
18
No duplicate detected. Storing in bigNonDupesArray[8]

Input
19
No duplicate detected. Storing in bigNonDupesArray[9]

Input
20
No duplicate detected. Storing in bigNonDupesArray[10]

Input
21
No duplicate detected. Storing in bigNonDupesArray[11]

Input
22
No duplicate detected. Storing in bigNonDupesArray[12]

Edited 4 Years Ago by Lucaci Andrew

This article has been dead for over six months. Start a new discussion instead.