Current location: Hot Scripts Forums » Programming Languages » C/C++ » How do i return the maximum of the array elements?


How do i return the maximum of the array elements?

Reply
  #1 (permalink)  
Old 06-15-04, 10:08 AM
toezaurus81 toezaurus81 is offline
Newbie Coder
 
Join Date: Mar 2004
Location: Malaysia
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
How do i return the maximum of the array elements?

/*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;
}
Reply With Quote
  #2 (permalink)  
Old 06-17-04, 03:30 PM
EvilHaider EvilHaider is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
If the idea is to have the ONE function arrange your array's contents in descending order and return the highest, then your changes are very simple.

Change your array function from a void to an int so that it returns an int value. At the end of the function once youve arranged the contents in descending order, the first element of the array is the largest (I'm guessing thats what you mean by maximum). Just return this element and you're set. From your main function you just capture this return value in an int variable and output that variable.

(**Note** - I havent gone through your sorting logic so as long as that is correct, this should work.)

Quote:
Originally Posted by toezaurus81
/*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;
}
__________________
**
Tutorial Maniacs
**
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM
changing a specific dateformat in tigra bitesize JavaScript 0 10-30-03 08:58 AM
Help trim code down TheLaughingBandit JavaScript 0 09-02-03 09:50 AM


All times are GMT -5. The time now is 04:29 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.