question how to handle dupes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

    question how to handle dupes

    I am trying to write a program that will input numbers in a array and then loop throught the array and look for dupes. The final output of the array should have the numbers in the array printed only once. And zero signals the end of the array and shouldn't be outputed. This is what I have so far can anyone show me my mistake.



    Code:
    #include <iostream>
    using namespace std;
    int main() {
    
            int scores[20]; //array of numbers
            int value;
            int dupe;
            int count;
    
            //prompt user for the list of numbers with zero being the end of the input.
    
            cout << "enter a list of numbers: ";
            for (int i=0; i < 20; i++){
    
                    cin >> value;
                     if (value == 0)
                    {
                    break;
                    } 
                    
                    scores[i] =  value;
                    if (scores[i] == value) 
                    {
                    dupe = 1;
                    }
                    }
                    if (dupe != 1)
                    {
                    scores[count] = value;
                    }
                    count++;
    
                    cout << value;
    }
  • DavidJones
    New Member
    • Nov 2006
    • 6

    #2
    Hi,

    In your code.

    Code:
    scores[i] =  value;
                    if (scores[i] == value) 
                    {
                    dupe = 1;
                    }
    if statement will always return true because you are assigning value to scores[i] in the previous statement. So this doesnt seems correct.

    In the code
    Code:
    if (dupe != 1)
                    {
                    scores[count] = value;
                    }
    count is not initialized. It may contain garbage character scores[count] will not indicate an array element.


    To detect dupes you need to traverse the array to check whether the value is present in array or not. For first value you can ignore the check and directly store it in array. For the second read you need to check array element 1. Similarly for next read you should check each array element to detect whether dupes are there.

    for n th read you should check scores[0] to scores[n-1-count], where count is the number of dupes previously detected.

    I wont give you the exact code for this. Try it urself and if you are in trouble, you will get help here...

    ~David

    Comment

    Working...