필터 지우기
필터 지우기

Storing user inputs during while loop, in a vector

조회 수: 7 (최근 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 CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by