Go Down

### Topic: Quick question - maximum value (Read 979 times)previous topic - next topic

#### MoonBeam

##### Aug 20, 2011, 12:45 am
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
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

#### retrolefty

#1
##### Aug 20, 2011, 12:51 am
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

#### MoonBeam

#2
##### Aug 20, 2011, 12:53 am
Cool, thanks ...I will do.

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

Thanks again.

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

#### JChristensen

#3
##### Aug 20, 2011, 12:55 am
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: [Select]
`    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]);    }`

#### wildbill

#4
##### Aug 20, 2011, 01:02 am
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.

#### bubulindo

#5
##### Aug 20, 2011, 01:03 am

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?
This... is a hobby.

#### MoonBeam

#6
##### Aug 20, 2011, 01:10 am

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.
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

#### JChristensen

#7
##### Aug 20, 2011, 01:22 am
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

#### MoonBeam

#8
##### Aug 20, 2011, 01:26 am
I'll keep that in mind  XD

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
Ná bac le mac an bhacaigh is ní bhacfaidh mac an bhacaigh leat.

Go Up