19.11.14

OOP : Excercise-7 : Search an element in an Array of 10 integer values. Use Constructor and Destructor with Class

Search an element in an Array of 10 integer values. Use Constructor and Destructor with Class

10 comments:

  1. #include
    using namespace std;
    class sarray
    {
    public:
    int rem=0,a[10],i,p;
    sarray();
    ~sarray();
    void show();
    };
    sarray::sarray()
    {
    for(i=0;i<10;i++)
    cin>>a[i];
    }
    sarray::~sarray()
    {
    cout<<"distructing"<>p;
    for(i=0;i<10;i++)
    {
    if(a[i]==p)
    {
    cout<<"found"<<endl;
    rem=1;
    break;
    }
    }
    if(rem==0)
    cout<<"Not found"<<endl;
    }
    int main()
    {
    sarray sa;
    sa.show();
    }

    ReplyDelete
  2. #include
    using namespace std;
    class sarray
    {
    public:
    sarray();
    ~sarray();
    void show();
    };
    sarray::sarray()
    {
    int a[10],r,x;
    for(r=0;r<10;r++)
    cin>>a[r];
    }
    sarray::~sarray()
    {
    cout<<"Destructing";
    }
    void sarray::show()
    {
    cout<<"Enter on element"<>x;
    for(r=0;r<10;r++)
    {
    if(a[r]==x)
    {
    cout<<"Array Found"<<endl;
    break;
    }
    if(a[r]==0)
    {
    cout<<"Array Not Found"<<endl;
    }
    }
    }
    int main()
    {
    sarray sa;
    sa.show();
    }

    ReplyDelete
    Replies
    1. Its better to declare variable in base class under public: section

      Delete
  3. #include
    using namespace std;
    class sarray
    {
    public:
    int t=0,m[10],i,n;
    sarray ();
    ~sarray();
    void show();
    };
    sarray:: sarray()
    {
    for(i=0;i<10;i++)
    cin>>m[i];
    }sarray:: ~sarray()
    {
    cout<<"destructing"<>n;
    for(i=0;i<10;i++)
    {
    if(m[i]==n)
    {
    cout<<"found"<<endl;
    t=1;
    break;
    if(t==0)
    cout<<"not found"<<endl;
    }
    }
    int main()
    {

    sarray sa;
    sa.show();
    }
    }




    ReplyDelete
  4. Dear sir Please visit below link:
    http://itlearn24.blogspot.com/2014/11/search-element-in-array-of-n-integer.html

    ReplyDelete
  5. #include
    using namespace std;

    class cons
    {
    public:
    cons(int i, int arr[]);
    ~cons();
    };

    cons::cons(int i, int arr[])
    {
    int chk = 0;
    for (int j = 0; j < 10; j++){
    if (i == arr[j]){
    cout << "Number found......";
    j = 10;
    chk = 1;
    }
    else
    chk = 0;
    }
    if (chk == 0)
    cout << "Number not found....";
    }

    cons::~cons()
    {

    }

    int main()
    {
    int arr[10], key;
    for (int i = 0; i < 10; i++){
    cout << "Enter " << i+1 << " Number --: ";
    cin >> arr[i];
    }
    cout << "Enter the number for search --: ";
    cin >> key;

    cons obj(key, arr);

    return 0;
    }

    ReplyDelete

Comment Here