How do i convert this for loop into a while loop?
Show older comments
I just couldn't figure out how to convert it into a while loop. here is the code:
clc;
clear;
clear all;
n=input('Enter the number of elements in your array:'); % number of array elements
for i=1:n
values(i)=input('Enter the values:')
if values(i)<0
values(i)=values(i)*(-1)
end
end
for k=1:(n-1)
d=k+1
Xaverage(k)=(values(d)+values(k))/2
end
y=1:1:n;
plot(values,y)
hold on
z=1:1:(n-1);
plot(Xaverage,z)
1 Comment
Ilker Enes Çirkin
on 28 May 2019
Accepted Answer
More Answers (1)
Ilker Enes Çirkin
on 28 May 2019
2 Comments
James Tursa
on 28 May 2019
You did not copy the form of the while loop correctly. Incrementing the index variable is the LAST thing you do in the loop as I have written it. So your i=i+1 and k=k+1 lines need to be the LAST things you do in the loop, not the first.
Ilker Enes Çirkin
on 28 May 2019
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!