
-----------------------------------
gayden
Tue Apr 07, 2015 9:58 am

Finding the max/min number of an array?
-----------------------------------
Creating a program for class, need to put 10 real numbers in an array and find the largest and smallest number from it. 

One idea I had was to create a for loop with if statements in it that filtered out the other numbers, but I feel like that is too complicated to be correct.

-----------------------------------
Dreadnought
Tue Apr 07, 2015 11:16 am

Re: Finding the max/min number of an array?
-----------------------------------
It might help to think about how you might do this yourself if you were given a list of numbers. Can you get the computer to use the same approach?

Also, if 10 numbers seems too simple, consider what you would do if you were asked to find the maximum or minimum for a list of 100 or 1000 numbers.

-----------------------------------
Clayton
Tue Apr 07, 2015 2:37 pm

RE:Finding the max/min number of an array?
-----------------------------------
To add on to what Dreadnought is saying. How would you do this if you had every number on it's own piece of individual paper, and you only got to see one number at a time?

-----------------------------------
gayden
Wed Apr 08, 2015 9:05 am

Re: RE:Finding the max/min number of an array?
-----------------------------------
To add on to what Dreadnought is saying. How would you do this if you had every number on it's own piece of individual paper, and you only got to see one number at a time?
What this makes me think of doing is something like this:

if num1 > num2 and num1 > num3 and num1 > num4, etc.

but doing that for 10 numbers will give me a stupid amount of code. Is there a way I can use a function like max but for more than comparing two numbers?

-----------------------------------
Insectoid
Wed Apr 08, 2015 9:50 am

RE:Finding the max/min number of an array?
-----------------------------------
You've got an array. Arrays play really nicely with for loops. You might want to take advantage of that.

-----------------------------------
gayden
Thu Apr 09, 2015 8:59 am

Re: RE:Finding the max/min number of an array?
-----------------------------------
You've got an array. Arrays play really nicely with for loops. You might want to take advantage of that.
Think I got it.

for j : lower (range) .. upper (range)
    if maxi < range (j) then
        maxi := range (j)
    end if
    if mini > range (j) then
        mini := range (j)
    end if
end for
%Where maxi := minint and mini := maxint

