Storing user inputs during while loop, in a vector
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Stephen23
le 5 Nov 2019
x = [];
while ...
x(end+1) = input(prompt);
end
2 commentaires
Stephen23
le 7 Nov 2019
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
le 7 Nov 2019
Modifié(e) : Suleman ZP
le 7 Nov 2019
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 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!