Hey there!
How can I find out the greatest number from a List of numbers?
I know that there's this function in Automagic i.e. max(n1,n2) but how do I use it to my end?
I mean it just compares two numbers/integers but I want to evaluate the largest number out of a List of numbers.
Kindly help.
Akhilesh Chandra Gairola
How to find out greatest number from a List of numbers.
Moderator: Martin
-
- Posts: 109
- Joined: 16 Apr 2014 04:57
Re: How to find out greatest number from a List of numbers.
list=newList(1,2,3,4,5,4,3,2,1);
for (i in list)
{ if (i > s) s = i }
log(s)
for (i in list)
{ if (i > s) s = i }
log(s)
-
- Posts: 109
- Joined: 16 Apr 2014 04:57
Re: How to find out greatest number from a List of numbers.
Heypmgnt wrote:list=newList(1,2,3,4,5,4,3,2,1);
for (i in list)
{ if (i > s) s = i }
log(s)
Could you please explain how is it going to calculate the greatest no. out of the list?
Because, the log function just writes a value to automagic log.
Thanks
Re: How to find out greatest number from a List of numbers.
The code is exactly doing what u asked for! Variable 's' contains biggest number.
-
- Posts: 109
- Joined: 16 Apr 2014 04:57
Re: How to find out greatest number from a List of numbers.
Indeed, it does. Flawless. Just wanted to know its working. If you could oblige me by explaining how it works.pmgnt wrote:The code is exactly doing what u asked for! Variable 's' contains biggest number.
Akhilesh
Re: How to find out greatest number from a List of numbers.
Actually very simple and kinda basic knowledge..
'for' loops thru the comma seperated list and variable 'i' contains current element. Now all you have to do is comparing value of current element with the value of variable 's'. If value is greater save it in variable 's' otherwise continue with next element.
'for' loops thru the comma seperated list and variable 'i' contains current element. Now all you have to do is comparing value of current element with the value of variable 's'. If value is greater save it in variable 's' otherwise continue with next element.
-
- Posts: 109
- Joined: 16 Apr 2014 04:57
Re: How to find out greatest number from a List of numbers.
I get it but not entirely. Variable 'i' contains current element, fine!
Pardon my ignorance in this regard.
Thanks
Akhilesh Chandra Gairola
What value does variable 's' contain initially? The variable 's' hasn't been assigned any value by us here, it just seems to pop up and do its job of saving the largest number.pmgnt wrote:Now all you have to do is comparing value of current element with the value of variable 's'. If value is greater save it in variable 's' otherwise continue with next element.
Pardon my ignorance in this regard.
Thanks
Akhilesh Chandra Gairola
Re: How to find out greatest number from a List of numbers.
's' is empty/null until it gets overwritten. You could initially assign 0 but it might cause wrong result e.g. with negative values like..
list=newList(-1,-2,-3,-4,-5,-4,-3,-2,-1)
s=0;
...
In this case 's' would still contain initally assigned value 0 which is not even in list.
list=newList(-1,-2,-3,-4,-5,-4,-3,-2,-1)
s=0;
...
In this case 's' would still contain initally assigned value 0 which is not even in list.