Storing user inputs during while loop, in a vector

11 ビュー (過去 30 日間)
James Madison
James Madison 2019 年 11 月 5 日
編集済み: Suleman ZP 2019 年 11 月 7 日
Hi everyone,
I am trying to write a program where a user can input as many numbers as they want, the program will find the minimum number, calculate the factorial of it and display the factorial AND the minimum number. The challenge is that I can't use the built-in min() or fact() functions, i need to code them myself.
I am having trouble getting the user inputs stored in a vector. The prompts ask this:
Enter a number: 6
Do you want to continue entering numbers? Y/N Y
Enter a number: 3
Do you want to continue entering numbers? Y/N Y
Enter a number: 8
Do you want to continue entering numbers? Y/N N
And so on. This is the code for the prompts:
Y=1;
N=0;
prompt=('Enter a number: ');
result=input(prompt);
question='Do you want to continue entering numbers? Y/N ';
answer=input(question);
while answer==1
if answer==1
prompt=('Enter a number: ');
result=input(prompt);
question='Do you want to continue entering numbers? Y/N ';
answer=input(question);
elseif answer==0
break
end
end
All I need to know is how to get each input for "result" in a vector, so that i have a vector with all the user's inputs.
After that, I know what to do for the minimum and how to calculate and display the factorial of the minimum. I have tried things along this format:
for ii=1:10
x(ii)=input('enter a number');
end
x=x(1:ii)
This displays a vector made up of the 10 numbers a user inputs, but I don't have a definite number of inputs.
Thanks!

回答 (2 件)

Stephen23
Stephen23 2019 年 11 月 5 日
x = [];
while ...
x(end+1) = input(prompt);
end
  2 件のコメント
James Madison
James Madison 2019 年 11 月 6 日
編集済み: James Madison 2019 年 11 月 6 日
Where should this code be? I also have no x variable.
I put the =input(prompt); part between my while and end, and put the x=[]; outside of that, but it says "undefined function or variable 'x'".
Stephen23
Stephen23 2019 年 11 月 7 日
Y=1;
N=0;
prompt=('Enter a number: ');
result=input(prompt);
question='Do you want to continue entering numbers? Y/N ';
answer=input(question);
while answer==1
if answer==1
prompt=('Enter a number: ');
result(end+1)=input(prompt);
question='Do you want to continue entering numbers? Y/N ';
answer=input(question);
elseif answer==0
break
end
end
And tested:
Enter a number: 5
Do you want to continue entering numbers? Y/N 1
Enter a number: 3
Do you want to continue entering numbers? Y/N 1
Enter a number: 2
Do you want to continue entering numbers? Y/N 0
>> result
result =
5 3 2

サインインしてコメントする。


Suleman ZP
Suleman ZP 2019 年 11 月 7 日
編集済み: Suleman ZP 2019 年 11 月 7 日
Hi, first of all i think if conditon is not required because you are already checking answer in a while condition.
You can create your own function to fine the minimum and factorial. Here is the example code.
Y=1;
N=0;
prompt=('Enter a number: ');
result=input(prompt);
question='Do you want to continue entering numbers? Y/N ';
answer=input(question);
while answer==1
prompt=('Enter a number: ');
result(end+1)=input(prompt);
question='Do you want to continue entering numbers? Y/N ';
answer=input(question);
end
% using bubble sort
result_sorted = my_bubble_sort(result);
% get the minimum
minimum_number = result_sorted(1);
display(['Minimum Number: ' num2str(minimum_number)])
% find fictorial of minimum
f = my_factorial(minimum_number);
display(['and its Factorial is: ' num2str(f)])
custom function to sort to get the minimum or maximum value.
function final_vector = my_bubble_sort(final_vector)
n=length(final_vector);
for j=1:1:n-1
for i=1:1:n-1
if final_vector(i)>final_vector(i+1)
temp=final_vector(i);
final_vector(i)=final_vector(i+1);
final_vector(i+1)=temp;
end
end
end
end
custom function to fine the factorial of the number.
function f = my_factorial(numbr)
f = 1;
for i = 1:numbr
f = f*i;
end
end
tested results
Enter a number: 6
Do you want to continue entering numbers? Y/N 1
Enter a number: 2
Do you want to continue entering numbers? Y/N 1
Enter a number: 5
Do you want to continue entering numbers? Y/N 0
Minimum Number: 2
and its Factorial is: 2

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by