Storing user inputs during while loop, in a vector
10 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(2 个)
Stephen23
2019-11-5
x = [];
while ...
x(end+1) = input(prompt);
end
2 个评论
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!