current position:Home>C + + small job - create an array class arr to delete duplicate elements in the array

C + + small job - create an array class arr to delete duplicate elements in the array

2022-05-15 03:05:30Programming Xiaopeng

One 、 Problem description

【 Problem description 】 Yes 16 Number :{1,2,2,3,4,4,5,6,6,7,8,8,8,9,10,10}, It has been arranged in order from small to large , Stored in an array a in , Try to create a class ARR, Finish deleting the same number so that there is only one . After deletion ,a The contents of the array by {1,2,3,4,5,6,7,8,9,10}, The specific requirements are as follows :

    (1)  Private data members 

    int n:  Number of actual elements of the array .

    int a[100]: Store the original array and result array .

    (2)  Public member functions 

    ARR(int x[ ], int size): Constructors , use size initialization n, use x initialization a Array .

    void delsame(): Complete array a Deletion of the same element in .

    void show():  Array the results in rows 10 Output to the screen in the form of number .

    (3)  Define the array in the main program int b[16], The initial value is as above . Define a class ARR The object of v, use b And the number of array elements to initialize the object , Then complete the test of this class according to the above requirements .

【 Input form 】 nothing
【 Output form 】 Output the data of array object before and after deleting duplicate elements .

【 The sample input 】 nothing
【 Sample output 】

Before deleting :

1    2    2    3    4    4    5    6    6    7

8    8    8    9   10   10

After deleting :

1    2    3    4    5    6    7    8    9   10

Two 、 Code implementation

#include<iostream>
using namespace std;
#include<iomanip>
class ARR
{
    
    private:
        int n;
        int a[100];
    public:
        ARR(int x[], int size)
        {
    
            n=size;
            for(int i=0;i<n;i++)
            {
    
                a[i]=x[i];
            }

        }
        void delsame()
         {
    
             for(int i=0;i<n-1;i++)
             {
    
                 for(int j=i+1;j<n;j++)
                 {
    
                     if(a[i]==a[j])
                     {
    
                         for(int k=j;k<n-1;k++)
                         {
    
                             a[k]=a[k+1];
                         }
                         --n;
                         --j;
                     }
                 }
             }
         }
         void show()
         {
    
             for(int i=0;i<n;i++)
             {
    
                 
                    cout<<setw(5)<<a[i];
                 if((i+1)%10==0)cout<<endl;
             }
         }
};
int  main(void)
{
    
        int  b[]={
    1,2,2,3,4,4,5,6,6,7,8,8,8,9,10,10};  // Define the array first ,  Use this array to initialize the array members in the object of the class 
        int  n=sizeof(b)/sizeof(int);  // Calculation b Element length of array 
        ARR    v(b,n);  // Define the object of the class and use an array b And data n Initialize the members of the object 
        cout<<" Before deleting :";
        v.show  ();    // Output the data in the array member of the object ( The deletion operation has not been done yet ,  The output is the initialized data )
        cout<<endl;
        v.delsame  ();  // Operate on objects ,  Delete the same element in the array member of the object 
        cout<<" After deleting :";
        v.show  ();  // After the delete operation, the data in the array member of the output object 
        cout<<endl;
        return  0;
}

copyright notice
author[Programming Xiaopeng],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/132/202205112357301323.html

Random recommended