How to find out greatest number from a List of numbers.

Post your questions and help other users.

Moderator: Martin

Post Reply
akhileshg1988
Posts: 109
Joined: 16 Apr 2014 04:57

How to find out greatest number from a List of numbers.

Post by akhileshg1988 » 13 Jun 2017 11:07

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

pmgnt
Posts: 26
Joined: 15 Jul 2016 00:34

Re: How to find out greatest number from a List of numbers.

Post by pmgnt » 14 Jun 2017 02:04

list=newList(1,2,3,4,5,4,3,2,1);

for (i in list)
{ if (i > s) s = i }
log(s)

akhileshg1988
Posts: 109
Joined: 16 Apr 2014 04:57

Re: How to find out greatest number from a List of numbers.

Post by akhileshg1988 » 14 Jun 2017 05:52

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

pmgnt
Posts: 26
Joined: 15 Jul 2016 00:34

Re: How to find out greatest number from a List of numbers.

Post by pmgnt » 14 Jun 2017 15:51

The code is exactly doing what u asked for! Variable 's' contains biggest number.

akhileshg1988
Posts: 109
Joined: 16 Apr 2014 04:57

Re: How to find out greatest number from a List of numbers.

Post by akhileshg1988 » 15 Jun 2017 11:51

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

pmgnt
Posts: 26
Joined: 15 Jul 2016 00:34

Re: How to find out greatest number from a List of numbers.

Post by pmgnt » 17 Jun 2017 14:52

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.

akhileshg1988
Posts: 109
Joined: 16 Apr 2014 04:57

Re: How to find out greatest number from a List of numbers.

Post by akhileshg1988 » 06 Jul 2017 03:55

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

pmgnt
Posts: 26
Joined: 15 Jul 2016 00:34

Re: How to find out greatest number from a List of numbers.

Post by pmgnt » 06 Jul 2017 20:36

'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.

Post Reply