I need to write a program that returns the largest number entered by a user. The user can enter as many numbers >= 0 as they want and when they enter a -1, the program will stop and return the largest number. I am really struggling to do this. So far, I have this:
validInput = false;
while (~validInput)
fprintf('Enter a number >= 0 or a negative to quit.\n');
num = input('Enter a number or -1 to quit: ');
if(num == -1)
validinput = true;
counter = 0;
elseif(num>=0)
counter = counter+1;
end;
if(counter == 0)
fprintf('No values entered!');
else
array = (counter);
m = max(counter);
disp(m);
end
end``
validInputandvalidinputboth. Matlab is case sensitive. And there are other problems as well...for instance settingcounterto 0 if one enters-1, insteadcountershould be initialized to 0 before thewhileloop. And not storingnumanywhere, but callingmax(counter)for a scalarcounter... And that's still not the end of it.-1<0, so it spares some checks.