While-Loops display each iteration of a variable

32 visualizzazioni (ultimi 30 giorni)
Daniil Hart
Daniil Hart il 30 Ott 2020
Modificato: DGM il 11 Nov 2024
I am very new to MATLAB, so bear with me please.
I'm working on a school assignment regarding while loops and I am required to print a tally of all entered x values after the loop has ended, I have no clue how to do that however.
This is the assignment:
  1. Using different variable names than in the examples above, write a script to let the user type in values from the keyboard and sum them up. This time make the script only total up zeroes or negative values. If a positive value is entered the script should stop at that time. Or, if a negative value less than 500, such as -501 is entered, the script is to stop. When the script stops, give the tally, and in a separate statement indicate what caused the program to end.
This is an example of what should show up after the loop has ended:
-491
-12
0
Total = -503
A positive value of 5 was entered and stopped the summation.
(Or, a value of -682 was entered and stopped the summation.)
This is what I have:
clear
clc
fprintf(' This script will take an x value, as long as it is larger than -501,')
fprintf('\n and less than 1, and will add it to the previous x value.\n\n')
sum = 0;
a = -500;
x = input(' Enter a value greater than -501 and less than 1: ');
while ( 0 >= x ) && ( x >= a )
sum = sum + x;
x = input(' Enter another value greater than -501 and less than 1: ');
end
%????????? need each new x value to show here
% x 1st loop
% x 2nd loop
% x 3rd loop
% ...
fprintf( 'Total = ')
fprintf( '%.f' , sum)
if ( x > 0)
fprintf('\n\nLoop ended, a positive value of ')
fprintf("%.f" , x)
fprintf(' was entered \n\n')
end
if ( x < a )
fprintf(' Loop ended, a negative value of ')
fprintf("%.f" , x)
fprintf(' was entered \n\n')
end

Risposte (2)

Daniil Hart
Daniil Hart il 30 Ott 2020
Never mind, I tinkered and used I guess a sort of counter within the loop to produce a new variable?
sum = 0;
a = -500;
x = input(' Enter a value greater than -501 and less than 1: ');
n = 1; % counter variable starting at 1
while ( 0 >= x ) && ( x >= a )
sum = sum + x;
x = input(' Enter another value greater than -501 and less than 1: ');
n = n+1; % counter increasing
b(n)=x; % new variable that is a vector that grows in columns with each loop
end
b = b';
fprintf('\n All entered values of x:\n')
disp(b)

Ayesha
Ayesha il 11 Nov 2024
Modificato: DGM il 11 Nov 2024
n=5
fac=1
while(n>=1)
n=n-1
fac=fac*n
end
  1 Commento
DGM
DGM il 11 Nov 2024
Use semicolons to suppress output..
% use a unique name for the parameter
% so that it doesn't get lost
n0 = 5;
% you could use a while loop,
% but while loops are more complicated and have extra hazards
fac = 1;
n = n0;
while n >= 1
fac = fac*n;
n = n-1; % order of operations!
end
fac
fac = 120
% but there's no good reason to use a while loop
% if the number of iterations are known
fac = 1;
for n = 1:n0
fac = fac*n;
end
fac
fac = 120
% you could eliminate the loop completely with basic vectorization
fac = prod(1:n0)
fac = 120
% or you could just use factorial()
fac = factorial(n0)
fac = 120

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by