446,187 Members | 1,069 Online
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

# find max value by Array

 P: 5 how to using the technique of Array so that I can input 5 values and then the program will find out which value is the large one. Who can help me???? THANK!!!!! Nov 14 '07 #1
9 Replies

 100+ P: 118 assign zero to a temporary variable and then loop, if there is a variable v which is greater than the temporary variable , then assign the value v to the temporary variable . when you are through with looping , the temporary variable is the largest. simple , is it? Can you do the code. Nov 14 '07 #2

 Expert 10K+ P: 11,448 assign zero to a temporary variable and then loop, if there is a variable v which is greater than the temporary variable , then assign the value v to the temporary variable . when you are through with looping , the temporary variable is the largest. simple , is it? Can you do the code. Example: -4, -3, -2,-1. Your algorithm incorrectly concludes that 0 is the maximum. kind regards, Jos Nov 14 '07 #3

 100+ P: 118 [quote=JosAH]Example: -4, -3, -2,-1. Your algorithm incorrectly concludes that 0 is the maximum. kind regards, Jos[/QUOTE Thats great stuff , you are meticulous , will definitely come up with a better algoithm , this works only if the numbers are positive. I was naive in approaching this problem. Nov 14 '07 #4

 10K+ P: 13,264 [quote=heat84] Example: -4, -3, -2,-1. Your algorithm incorrectly concludes that 0 is the maximum. kind regards, Jos[/QUOTE Thats great stuff , you are meticulous , will definitely come up with a better algoithm , this works only if the numbers are positive. I was naive in approaching this problem. Assign the first value of the array to the temporary variable ... Nov 14 '07 #5

 Expert 10K+ P: 11,448 [quote=r035198x] Assign the first value of the array to the temporary variable ... What if the array is empty? i.e.. array.length == 0 kind regards, Jos Nov 14 '07 #6

 10K+ P: 13,264 [quote=JosAH] What if the array is empty? i.e.. array.length == 0 kind regards, Jos After the OP has input 5 values? Then I'd say the code for taking the input was buggy. This is a lesson worth noting of course. Always cater for as many possibilities as you can. Nov 14 '07 #7

 Expert 10K+ P: 11,448 [quote=r035198x] After the OP has input 5 values? Then I'd say the code for taking the input was buggy. This is a lesson worth noting of course. Always cater for as many possibilities as you can. I normally return an index to the object or -1 when there are no objects. The same can be done with object references; null is a very good candidate when there are no objects. But then again I'm a nasty nitpicker ;-) kind regards, Jos Nov 14 '07 #8

 100+ P: 118 ok then , first check if the array size is greater than zero. then assign the first element to a temporary variable. loop through and if there is an array element which is greater than the temporary , then assign that value to the temporary variable. Thanks for the eye openers guys . Seems we are getting closer to the solution ....... Nov 15 '07 #9

 Expert 2.5K+ P: 3,652 Closer? I would say that, if you understand that, you have the solution in hand! Nov 15 '07 #10