CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2004
    Posts
    3

    How am I going to return the maxium value of the array's element?

    /*Define a class intList contains one private attribute:
    an array of integers of size 10. Provide member functions to accept
    and display the elements of array. Provide one member function that
    arranges the array elements in descending order and returns the maximum
    of the array elements.
    Write a driver program to test the above intList class.*/

    #include<iostream.h>

    class intList
    {

    private:
    int data[10];
    int pass, i, temp;

    public:
    void accept()
    {
    cout<<"Input integer number for array";
    cout<<"\n******************************"<<endl;

    for(i=0;i<10;i++)
    {
    cout<<"Enter value for array["<<i<<"]: ";
    cin>>data[i];
    }
    }


    void display()
    {
    for(i=0;i<10;i++)
    {
    cout<<"Value for array["<<i<<"]: "<<data[i]<<endl;
    }
    }


    void arrange()
    { int Max=10;
    for(pass=1;pass<=Max-1;pass++)
    for(i=0;i<Max-pass;i++)
    {
    if(data[i]<data[i+1]){
    temp=data[i];
    data[i]=data[i+1];
    data[i+1]=temp;
    }
    }
    }

    };

    void main(){
    intList list;

    list.accept();
    cout<<"\nThe values stored in the arrays";
    cout<<"\n*******************************"<<endl;
    list.display();
    list.arrange();
    cout<<"\nThe values stored in the arrays after sort in decending";
    cout<<"\n*******************************"<<endl;
    list.display();
    //cout<<"The maxinum of the arrays elements: "<<max;
    }

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    One solution is to wrap the list data structure around an STL set container.

    Kuphryn

  3. #3
    Join Date
    Jun 2002
    Posts
    395
    If the elements are sorted in descending order, the first array element will contain the max, so just return it:

    Code:
    int arrange()
    { 
       int Max=10;
       for(pass=1;pass<=Max-1;pass++)
          for(i=0;i<Max-pass;i++)
          { 
             if (data[i]<data[i+1]){
                temp=data[i];
                data[i]=data[i+1];
                data[i+1]=temp;
            }
         }
    
       return data[0];
    }
    
    ...
    
    int max = list.arrange();

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150
    You can sort your data using the following code:
    Code:
    #include <algorithm>
    ...
    std::sort(&data[0], &data[10]);
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured