Thread: Removing elements from an array

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    33

    Question Removing elements from an array

    So some of you might have seen my other thread regarding this problem. I appreciated the help I got on that issue but now I am unfortunately stuck with one more hiccup.

    This program is intended to read in an array of numbers (size of the array is determined by the user). It then is to take that list, count the number of times each number is stored, sort the numbers in ascending order and print out what numbers were input and how many times each occurred. The problem i am having is that it is not supposed to repeat numbers that were entered more than once. i.e. if you enter
    Code:
     5 4 3 2 1
    it should ouput
    Code:
     
    1      1
    2      1
    3      1 
    4      1
    5       1
    but if you enter
    Code:
     5 5 5 5 5
    it should ouput
    Code:
     
    5       5
    As you can see I have implemented unique, but it does not seem to be working. Also, my printing loop is depending on the size of the original array, which I know will not work, but I cannot figure out what else to do. Any suggestions?

    Code:
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
            int sizeOfList=0;
            int a[101] = {0};
    		int store[101] = {0};
    		int store[101] = {0};
    		int i;
    		int j=0;
    
            cout << "Enter an integer: " << endl;
            cin >> sizeOfList;
            cout << "Enter an integer in the range 0-100:" << endl;
    
    /*fills the array with user input*/
            while (j < sizeOfList) {
                    cin >> i;
                    store[j] = i;
    		a[i]++;
    		j++;  
    	}
    
    /*Displays the values as entered*/
            cout << "----------" << endl;
            cout << "pre sort" << endl;
    
            cout << "----------" << endl;
            for (j = 0; j < sizeOfList; j++) {
           		cout << "store[" << j << "]: " << store[j] << endl;	
    	}
            cout << "----------" << endl;
    
    /*sorts the array*/
    /*sort was called from the c++ library*/
    
    		sort(store, store+j);
    
    /*Displays the values in order*/
    		cout << "post sort" << endl;
            cout << "----------" << endl;
            for (j = 0; j < sizeOfList; j++) {
                    cout << "store[" << j << "]: " << store[j] << endl;
            }
            cout << "----------" << endl;
    
    		unique(store, store+j);
    /*Displays the unique values*/
    		cout << "unique" << endl;
            cout << "----------" << endl;
            for (j = 0; j < sizeOfList; j++) {
                    cout << "store[" << j << "]: " << store[j] << endl;
            }
            cout << "----------" << endl;		
    		
    
    /*print out*/
    	cout << "Entries        Count" << endl;
    		for (j = 0; j < sizeOfList; j++) {
    			cout << "   "<< store[j] <<  "             ";
    			cout  << a[i] << endl;
    			i++;	
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So, the "unique"-ified array should be smaller than the original, right? The return value of unique is the new "stop here or else" point -- it doesn't resize the array (and even if it did, that wouldn't magically change the value of your sizeOfList variable).

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    Thanks for the reply. Thats the information I know, I need a better method of printing them out. I just dont know how

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    33
    figured it out thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  2. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  3. Shifting elements in an Array
    By mmarab in forum C Programming
    Replies: 5
    Last Post: 12-10-2007, 12:11 PM
  4. Problem with assigning value to array elements
    By sagitt13 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 11:26 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 10:37 PM