Pages: [1]   Go Down
 Author Topic: Quick question - maximum value  (Read 401 times) 0 Members and 1 Guest are viewing this topic.
Just past the last tree on the left
Offline
Full Member
Karma: 0
Posts: 125
Ó Fithcheallaigh
 « on: August 19, 2011, 05:45:37 pm » Bigger Smaller Reset

Hello,

Just a quick question, is there any way to pick out a maximum value from a list of numbers, like, maybe 100 numbers, maybe more, maybe less

I know there is the max() function, but it only seems to determine the maximum value between two numbers.

Seán
 Logged

Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

Left Coast, CA (USA)
Offline
Brattain Member
Karma: 361
Posts: 17259
Measurement changes behavior
 « Reply #1 on: August 19, 2011, 05:51:04 pm » Bigger Smaller Reset

What your asking for is fundamentally a sorting function with variable size as the selection order. Search on sort functions and see what comes up. Knowing how large the number set can get is probably key to selecting the best method.

Lefty
 Logged

Just past the last tree on the left
Offline
Full Member
Karma: 0
Posts: 125
Ó Fithcheallaigh
 « Reply #2 on: August 19, 2011, 05:53:58 pm » Bigger Smaller Reset

Cool, thanks ...I will do.

The range of numbers will be variable, but I'm sure I will find something.

Thanks again.

Seán
 Logged

Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

Grand Blanc, MI, USA
Offline
Karma: 92
Posts: 3942
CODE is a mass noun and should not be used in the plural or with an indefinite article.
 « Reply #3 on: August 19, 2011, 05:55:50 pm » Bigger Smaller Reset

Sorting not necessary if we just want the maximum.  Assuming the numbers are in an array, something like this should work (compiles, but I didn't test it):
Code:
int myNumbers[100], maxValue;
//assign values to myNumbers somehow
maxValue = myNumbers[0];
for (int n=1; n<100; n++) {
if (myNumbers[n] > maxValue) maxValue = myNumbers[n];
//or, instead of the if statement above:
//maxValue = max(maxValue, myNumbers[n]);
}
 Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

New Jersey
Offline
Karma: 65
Posts: 3638
 « Reply #4 on: August 19, 2011, 06:02:08 pm » Bigger Smaller Reset

There may be elegance or performance gains to be had (if you care) by doing this as you load your array. i.e. while you're getting these numbers from whatever source & putting them in the array, check each one against the current max as in Jack Christensen's example.
 Logged

'round the world...
Online
Karma: 42
Posts: 3219
 « Reply #5 on: August 19, 2011, 06:03:13 pm » Bigger Smaller Reset

Hello,

Just a quick question, is there any way to pick out a maximum value from a list of numbers, like, maybe 100 numbers, maybe more, maybe less

I know there is the max() function, but it only seems to determine the maximum value between two numbers.

Seán

Isn't this one of the first exercises when you are learning how to program any sort of processor?
 Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Just past the last tree on the left
Offline
Full Member
Karma: 0
Posts: 125
Ó Fithcheallaigh
 « Reply #6 on: August 19, 2011, 06:10:57 pm » Bigger Smaller Reset

Hello,

Just a quick question, is there any way to pick out a maximum value from a list of numbers, like, maybe 100 numbers, maybe more, maybe less

I know there is the max() function, but it only seems to determine the maximum value between two numbers.

Seán

Isn't this one of the first exercises when you are learning how to program any sort of processor?

Obviously, not for me

And Jack Christensen, thanks, I will see if I can get the values put into an array, and see what I can do.

Thanks.
 Logged

Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

Grand Blanc, MI, USA
Offline
Karma: 92
Posts: 3942
CODE is a mass noun and should not be used in the plural or with an indefinite article.
 « Reply #7 on: August 19, 2011, 06:22:13 pm » Bigger Smaller Reset

You're most welcome, be sure to consider wildbill's suggestion too, elegance always trumps brute force! You also said the list might not be of constant length, so another variable may be appropriate to hold the length of the list. And if for some reason the max can't be calculated as the data is stored in the array, then use that variable as the upper bound in the for loop in my example.

Jack
 Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Just past the last tree on the left
Offline
Full Member
Karma: 0
Posts: 125
Ó Fithcheallaigh
 « Reply #8 on: August 19, 2011, 06:26:15 pm » Bigger Smaller Reset

I'll keep that in mind

Dunno how much elegance my code will have. I am still very much learning. But the more you know, the better you get.

Thanks again.

Seán
 Logged

Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

 Pages: [1]   Go Up