Page 1 of 1
How to find out greatest number from a List of numbers.
Posted: 13 Jun 2017 11:07
by akhileshg1988
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
Re: How to find out greatest number from a List of numbers.
Posted: 14 Jun 2017 02:04
by pmgnt
list=newList(1,2,3,4,5,4,3,2,1);
for (i in list)
{ if (i > s) s = i }
log(s)
Re: How to find out greatest number from a List of numbers.
Posted: 14 Jun 2017 05:52
by akhileshg1988
pmgnt wrote:list=newList(1,2,3,4,5,4,3,2,1);
for (i in list)
{ if (i > s) s = i }
log(s)
Hey
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.
Posted: 14 Jun 2017 15:51
by pmgnt
The code is exactly doing what u asked for! Variable 's' contains biggest number.
Re: How to find out greatest number from a List of numbers.
Posted: 15 Jun 2017 11:51
by akhileshg1988
pmgnt wrote:The code is exactly doing what u asked for! Variable 's' contains biggest number.
Indeed, it does. Flawless. Just wanted to know its working. If you could oblige me by explaining how it works.
Akhilesh
Re: How to find out greatest number from a List of numbers.
Posted: 17 Jun 2017 14:52
by pmgnt
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.
Re: How to find out greatest number from a List of numbers.
Posted: 06 Jul 2017 03:55
by akhileshg1988
I get it but not entirely. Variable 'i' contains current element, fine!
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.
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.
Pardon my ignorance in this regard.
Thanks
Akhilesh Chandra Gairola
Re: How to find out greatest number from a List of numbers.
Posted: 06 Jul 2017 20:36
by pmgnt
'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.