Is there a simple option?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 177
Reputation: Black Magic is an unknown quantity at this point 
Solved Threads: 4
Black Magic's Avatar
Black Magic Black Magic is offline Offline
Junior Poster

Is there a simple option?

 
0
  #1
May 17th, 2008
Hey, I got bored so was making this program and i know how to get the highest and lowest number but I was wondering if there was a even quicker way, or would :

  1. if( number1 > number2 && number1 > number3)
  2. highNum = number1;
  3.  
  4. else if( number2 > number1 && number2 > number3)
  5. highNum = number2;
  6.  
  7. else if( number3 > number1 && number3 > number2)
  8. highNum = number3;
  9.  
  10. if( number1 < number2 && number1 < number3)
  11. lowNum = number1;
  12.  
  13. else if( number2 < number1 && number2 < number3)
  14. lowNum = number2;
  15.  
  16. else if( number3 < number2 && number1 < number1)
  17. lowNum = number3;
C Plus Plus Coder.
Fourteen Years Of Age
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,483
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Is there a simple option?

 
0
  #2
May 17th, 2008
put the numbers in an array
  1. int array[3] = {3,1,5};
  2. int largest = array[0];
  3. int smallest = array[0];
  4. for(int i = 1; i < 3; i++)
  5. {
  6. if( array[i] > largest)
  7. largest = array[i];
  8. // now do similar for smallest
  9. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: Is there a simple option?

 
0
  #3
May 17th, 2008
Potentially quicker, you can use the STL min and max template functions. It's definitely easier, but it gets tedious with more than a few numbers:
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. int main()
  5. {
  6. int a = 0;
  7. int b = 1;
  8. int c = 2;
  9.  
  10. std::cout << "Low number: " << std::min(std::min(a, b), c) << '\n';
  11. std::cout << "High number: " << std::max(std::max(a, b), c) << '\n';
  12. }
Last edited by Radical Edward; May 17th, 2008 at 10:37 am.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Is there a simple option?

 
0
  #4
May 17th, 2008
How about:
  1. #include <algorithm>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int numbers[] = { 12, -7, 42, 9 };
  8.  
  9. cout << "largest = " << max_element( numbers, numbers +4 ) << endl;
  10. cout << "smallest = " << min_element( numbers, numbers +4 ) << endl;
  11. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Is there a simple option?

 
0
  #5
May 18th, 2008
using c++09 variadic templates http://www.osl.iu.edu/~dgregor/cpp/v...-templates.pdf
  1. #include <iostream>
  2.  
  3. template < typename T > T largest( T first, T second )
  4. { return first > second ? first : second ; }
  5.  
  6.  
  7. template < typename T, typename... U > T largest( T first, U... rest )
  8. { return largest( first, largest( rest... ) ) ; }
  9.  
  10. int main()
  11. {
  12. int n1=5, n2=7, n3=1 ;
  13. std::cout << largest( n1, 4, n2 ) << '\n' ;
  14. std::cout << largest( n1, 78, 3, n2, 12, n3 ) << '\n' ;
  15. }
  16.  
  17. // compile with: g++43 -Wall -std=c++0x -pedantic -Werror myfile.cc
Last edited by vijayan121; May 18th, 2008 at 12:20 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Is there a simple option?

 
0
  #6
May 18th, 2008
Geek.





/me runs off to try out this super coolness....
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 202
Reputation: n.aggel is an unknown quantity at this point 
Solved Threads: 11
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Posting Whiz in Training

Re: Is there a simple option?

 
0
  #7
May 18th, 2008
Originally Posted by vijayan121 View Post
using c++09 variadic templates http://www.osl.iu.edu/~dgregor/cpp/v...-templates.pdf
  1. #include <iostream>
  2.  
  3. template < typename T > T largest( T first, T second )
  4. { return first > second ? first : second ; }
  5.  
  6.  
  7. template < typename T, typename... U > T largest( T first, U... rest )
  8. { return largest( first, largest( rest... ) ) ; }
  9.  
  10. int main()
  11. {
  12. int n1=5, n2=7, n3=1 ;
  13. std::cout << largest( n1, 4, n2 ) << '\n' ;
  14. std::cout << largest( n1, 78, 3, n2, 12, n3 ) << '\n' ;
  15. }
  16.  
  17. // compile with: g++43 -Wall -std=c++0x -pedantic -Werror myfile.cc
just amazing.... {i had no intention of spamming , but this code is awesome...}
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC