|
-
June 15th, 2004, 10:12 AM
#1
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;
}
-
June 15th, 2004, 11:26 AM
#2
One solution is to wrap the list data structure around an STL set container.
Kuphryn
-
June 15th, 2004, 02:32 PM
#3
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();
-
June 15th, 2004, 02:48 PM
#4
You can sort your data using the following code:
Code:
#include <algorithm>
...
std::sort(&data[0], &data[10]);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|