I need help writing a program to pick the largest number in a list

I was given:
clc;
n=input('how many numbers are in your list?');
for i=1:n
y=['enter a number', num2str(i)];
disp (y)
x(i)=input('');
end
and the list of numbers to plug in. I need to finish the program so it will choose the largest from my list. This is what I have so far:
if x(i)<=y
[newx,newy]=swap (x,y)
newx=y
newy=x
else x(i)>y
disp('The largest number in the list is')
end
This program runs but does not give me a single value. What should i do to fix it?

12 Comments

You forgot to ask a question. There is the max command, but you can write a loop also, which compares all numbers with the largest value found already. Try to start, post the code and ask a specific question. This is the best way to learn.
Okay I'll see what I can do. Thank you.
Hint:
Suppose you have looked at the first 5 elements and found the largest of them. How would you extend that to find the largest of the first 6 elements?
I honestly have no clue. I have about a weeks worth of matlab experience and I am not a computer person at all so progress has been slow.
Suppose I tell you a list of numbers. What is your normal procedure for finding out which one of the list is the largest? I don't mean in programming: how do you do it?
For example, is your usual method to write all of the numbers on individual pieces of paper, and put the papers in a hat, and take out one at random, remember it if it is the largest you have seen so far, throw it back in the hat, take out one at random, remember it if it is the largest seen so far, throw back in the hat, and keep doing that random selection 200 times and then decide that statistically speaking you surely have pulled out the largest number by now?
Statistically I would assume yes, I most likely have pulled outthe largest number.
No, no, the question is how YOU do this. I doubt that YOU use a hat, for example. You might use a bowl or a basket or a pot, but you probably do not use a hat. What do YOU do? And what would you do if there were more than 200 numbers given?
If there were more than 200 numbers I would go down the list and each time I found the largest number I've crossed I would write it down on a separate piece of paper.
But that is not what YOU would do for less than 200 numbers?
I would do that for any large list of numbers. Say a list of more than 20 numbers. If the list was small enough i would just look and pick the largest number. Say the list contained 10 numbers, I could easily look at the list and see which number is the largest.
Computers cannot "just look", but they can use the same kind of procedure you describe.
How would I write a program to follow that procedure? And thank you for helping and teaching me as well.

Sign in to comment.

 Accepted Answer

Try this:
my_max = -inf;
for k=1:length(y) % y is your array
if my_max < y(k)
my_max = y(k);
end
end
disp(my_max)
This is computationally expensive though. You can find the maximum easily with the function max:
my_max = max(y'); %y is your array. If it is already a column vector, you do not need to transpose it

2 Comments

The largest number in my list is 52 but when I use the max function it says my largest number is 117.
Show your code and your data, or tell us what you entered.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!