Clear Filters
Clear Filters

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

13 views (last 30 days)
I was given:
n=input('how many numbers are in your list?');
for i=1:n
y=['enter a number', num2str(i)];
disp (y)
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)
else x(i)>y
disp('The largest number in the list is')
This program runs but does not give me a single value. What should i do to fix it?
Walter Roberson
Walter Roberson on 13 Feb 2017
Computers cannot "just look", but they can use the same kind of procedure you describe.
justin simmons
justin simmons on 13 Feb 2017
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

Seyedali Mirjalili
Seyedali Mirjalili on 12 Feb 2017
Edited: Seyedali Mirjalili on 12 Feb 2017
Try this:
my_max = -inf;
for k=1:length(y) % y is your array
if my_max < y(k)
my_max = y(k);
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
justin simmons
justin simmons on 12 Feb 2017
The largest number in my list is 52 but when I use the max function it says my largest number is 117.

Sign in to comment.

More Answers (0)


Find more on Historical Contests 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!