By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,069 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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.
<spoiler>
Assign the first value of the array to the temporary variable ...
</spoiler>
Nov 14 '07 #5

Expert 10K+
P: 11,448
[quote=r035198x]
<spoiler>
Assign the first value of the array to the temporary variable ...
</spoiler>
<nitpicker mode>
What if the array is empty? i.e.. array.length == 0
<nipicker mode/>

kind regards,

Jos
Nov 14 '07 #6

10K+
P: 13,264
[quote=JosAH]

<nitpicker mode>
What if the array is empty? i.e.. array.length == 0
<nipicker mode/>

kind regards,

Jos
<kidding>
After the OP has input 5 values? Then I'd say the code for taking the input was buggy.
</kidding>
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]
<kidding>
After the OP has input 5 values? Then I'd say the code for taking the input was buggy.
</kidding>
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

Ganon11
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

Post your reply

Sign in to post your reply or Sign up for a free account.