Thread: need help using unique and counting instances of a number

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Question need help using unique and counting instances of a number

    I am having the user enter in a series of numbers into an array. I am then wanting to order that set of numbers from lowest to highest while deleting and duplicate numbers they may have entered.

    I have been able to order it from lowest to highest just fine. However, when I use 'unique' and then use a loop to print the array after I use 'unique' things go wrong.

    Also I am needing to count how many times a number was entered. For example.. if i entered: 1 2 3 2 5 6. I need it to print out something like...

    1 occurred 1 time(s)
    2 occurred 2 time(s)
    3 occurred 1 time(s)
    5 occurred 1 time(s)
    6 occurred 1 time(s)

    something to that effect. The part I need help with is finding out how to actually count how many times a number was entered in an array.



    Here is what I have.

    Code:
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int main ()
    {
    int x;
    int anArray[99999];
    
    
    /*Ask user for size of array and have them fill that array*/
    cout << "How many integers will you enter?  ";
    cin >> x;
    
    cout << "Enter integers between 0-100" << endl;
    
    for(int i = 0; i < x; i++)
    {
    	cin >> anArray[i];
    }
    
    /*Sorts the integers from lowest to highest*/
    sort(anArray, anArray+x);
    
    /*Deletes the duplicates*/ 
    unique(anArray, anArray+x);
    
    /*Prints out the new array...This is where the trouble is.*/
    for(int i = 0; i < x; i++)
    {
    	cout <<anArray[i] << endl;
    }
    Since unique deletes those numbers in the array when I try to print it out at the end it fills in the spots at the end with other numbers. If you run the program you'll see what I mean.
    Last edited by go_loco; 03-30-2010 at 10:40 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    std::unique re-arranges the elements of the array such that all the elements up to a certain point are unique. The remaining elements are not to be used. It returns an iterator to the element at the start of this "not to be used" point. Therefore, it is your responsibility to ensure that you never use elements outside of the range of unique elements.

    Since you are going to "ask user for size of array and have them fill that array", I suggest that you use a std::vector<int>. This way you would be able to create a vector of the desired size, apply std::unique, and then erase the "not to be used" range.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    I'm sorry I'm just starting to use C++. So am I not able to use unique to get rid of the duplicate numbers that may be entered?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You cannot change the size of the array at run time. You can only work with a subrange of the array. This is why I suggested using a std::vector<int>, whose size you can change.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    ok... so I've rewritten my code using a vector instead. Here is what I have so far....


    Code:
     #include <iostream>
        #include <vector>
        using namespace std;
    
        int main()
        {
            vector<double> aVector;     // no size specified: vector contains
                                              // zero elements
    
            int x;
    
            cout << "Number of integers you wish to enter: " << endl;
            cin >> x;
            
            cout << "Enter " << x << " integer(s) between 0-100: " << endl; 
    
            aVector.resize (x);
    
            for (int i = 0; i < x; i++)
            {
                
                cin >> aVector[i];
            }
            
            
    
            return 0;
        }

    So now that I have them choosing the size of the vector and then filling the vector with integers I am trying to order them now. Howver when I try to use the 'sort' it throws all sorts of errors at me. I'm new to vectors so could you point me in the right direction? Thanks!

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You forgot to #include <algorithm>. Also, you should be using a vector<int>, e.g.,
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        int x;
        cout << "Number of integers you wish to enter: " << endl;
        cin >> x;
    
        vector<int> aVector(x);
    
        cout << "Enter " << x << " integer(s) between 0-100: " << endl; 
    
        for (int i = 0; i < x; i++)
        {
            cin >> aVector[i];
        }
    
        sort(aVector.begin(), aVector.end());
        aVector.erase(unique(aVector.begin(), aVector.end()), aVector.end());
    
        // ...
    
        return 0;
    }
    Note that you are assuming that x is non-negative. You might want to make x a vector<int>::size_type instead of int.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Yea we assume X is non negative. Sorry I failed to mention that.

    Right after i posted I found the .begin that you posted above. I just added that to my sort statement and it worked just fine.

    When I try to print out after using 'unique' I'm having a little trouble. I know that if I enter doubles of a number it will delete it and change the size of the vector. So since it does that I'm not able to use this for loop to print it out...

    Code:
      for ( int i = 0; i < x; i++)
            {
            	cout << aVector[i];
            }
    Because it is no long of size 'x'. Is there something that I can use in place of 'x' that will be the current vector length after unique?


    I really appreciate all your help. Especially with showing me vectors and helping me learn!!


    Never mind I found it. I used this instead.....


    Code:
    for ( int i = 0; i < aVector.size(); i++)
            {
            	cout << aVector[i];
            }
    Last edited by go_loco; 03-30-2010 at 11:33 AM. Reason: FOUND IT!!!! :)

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    Haha ok....So now that I am using a vector I'm back to the question of " How can I count the occurrences of a certain number and then have it print out how many times that number occurred." This is the part that I'm pretty lost on.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by go_loco
    Is there something that I can use in place of 'x' that will be the current vector length after unique?
    Observe how I use erase and unique together. After you have erased those "not to be used" elements, you can use aVector.size() to get the new size of the vector.

    Quote Originally Posted by go_loco
    So now that I am using a vector I'm back to the question of " How can I count the occurrences of a certain number and then have it print out how many times that number occurred."
    Ah, but this is a different problem from one of removing duplicates. I suggest that you use a std::map<int, size_t>. The idea is to map the numbers to their counts.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    ok I'm trying to understand this map concept. Would you mind trying to explain it and maybe a small example, unrelated to my assignment. I don't want answers. I want to understand this map concept.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Take a look at this reference on std::map.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    So would I be correct in using this?

    Code:
    size_type count(const key_type& key) const;

    I was thinking I could have it search the vector for the values 0-100? If I did that where would it then store the number of times a certain value occurred?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look at the list of operators. Among them is operator[], so you could use a map like this:
    Code:
    std::map<int, int> counts;
    counts[123] = 10;
    This would mean that you are saying that the number 123 appears 10 times. So, think of how you can make use of this such that you keep track of the count of each number entered.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    ok this map stuff is kinda blowing my mind, but I think after looking at that page I'm a little close.

    So when you say:

    map<int, int> counts;

    Does this just say that you are comparing two integers? I guess my confussion comes from all the examples I've read are just comparing strings and ints instead of ints and ints.

    Sorry for the trouble.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, it declares a map object named counts that maps ints to ints. So, the key is an int, which is the number input, and the value is also an int, but is the count of the number of times the key appears in the input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. counting 16 bit number from a buffer
    By baccardi in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 03:28 PM