Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

find max using class

Posted on 2011-04-26
2
Medium Priority
?
341 Views
Last Modified: 2012-05-11
Hi, I am trying to find the max using class

my input is:5 88 24 0 4
but my max is 0 instead of 88.

Can you tell me what is wrong with my code?

THANKS.
#include<iostream>

using namespace std;
class Three
{private: int a[5];
public: void Read();
		void FindMax();
};
void Three::Read()
{for(int i=0;i<5;++i)
cin>>a[i];
}

void Three::FindMax()
{int max=a[0];
for(int i=1;i<5;++i)
{if(max>a[i]) max=a[i];}
cout<< max;
}
int main(){
	Three One;
One.Read();
One.FindMax();

return 0;
}

//input: 5 88 24 0 4
//max: 0

Open in new window

0
Comment
Question by:pgmerLA
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 35468578
The line

 
{if(max>a[i]) max=a[i];}

Open in new window


should read

 
{if(max<a[i]) max=a[i];} // '<', not '>' - replace if current 'max' is smaller

Open in new window


so make that
#include<iostream>

using namespace std;
class Three
{private: int a[5];
public: void Read();
		void FindMax();
};
void Three::Read()
{for(int i=0;i<5;++i)
cin>>a[i];
}

void Three::FindMax()
{int max=a[0];
for(int i=1;i<5;++i)
{if(max<a[i]) max=a[i];}
cout<< max;
}
int main(){
	Three One;
One.Read();
One.FindMax();

return 0;
}

//input: 5 88 24 0 4
//max: 0

Open in new window

0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 1000 total points
ID: 35468579
you better use < (less than)

if(max<a[i]) max=a[i];

Open in new window


Sara
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question