Need to create an array that consists of 5 numbers found through a while loop
26 views (last 30 days)
Show older comments
Jacob
ongeveer 19 uur ago
Commented: Walter Roberson
ongeveer 18 uur ago
I am creating a game where you loop through a while loop and get a random number between set values. If your number is within a range you get points and that is added to your total. I am trying to also display the values (distance) you get in the command window with fprintf but I am getting a struct value for the array. I have tried multiple struct commands and was eventually able to get it to a cell, but was unable to print. I am also unable to get my append to work. So how do I save these values as I loop through my function and print it out.
userInput = input("Which car variation are you testing?\n1. Gatorade wheels, gatorade bottle\n2. Gatorade wheel, water bottle\n" + ...
"3. Water bottle wheels, gatorade bottle\n4. Water bottle wheels, water bottle\n");
score = 0;
if userInput == 1
count = 0;
while count < 5
a = 10;
b = 20;
distance = (b-a).*rand(1,1) + a;
array = [];
array.append = distance;
distance1 = distance(count);
count = count + 1;
if distance >= 15 && distance <= 18
score = score + 3;
elseif distance < 18
score = score + 1;
elseif distance < 15
score = score + 1;
end
end
elseif userInput == 2
elseif userInput == 3
elseif userInput == 4
else
printf("Sorry, we could not perform your test.");
end
fprintf("You travelled %d and your total score is: %3.0f", array, score);
3 Comments
Walter Roberson
ongeveer 18 uur ago
if distance >= 15 && distance <= 18
score = score + 3;
elseif distance < 18
score = score + 1;
elseif distance < 15
score = score + 1;
end
Consider a distance value < 15. It would fail the >= 15 & <= 18 test so it would move to the elseif. Since it is < 15 it is certainly < 18 so it would pass the < 18 test. Having succeeded on that test, it would not use the "elseif" comparison against 15.
I suspect the desired test is elseif distance > 18
Note that if the requirement is for the score to be incremented by 1 whenver the distance is outside the range 15 to 18, then you could do that with a simple "else" (unless somehow the distance can end up being nan in which case specific tests for > 18 and < 15 would exclude the nan case.)
Accepted Answer
Stephen23
ongeveer 19 uur ago
Take a look at this line:
array.append = distance;
You looks as if you are trying to write Python code. However, MATLAB does not have lists and it certainly does not have methods (e.g. APPEND) that operate on lists. The MATLAB approach is to use indexing:
userInput = 1;
score = 0;
array = [];
if userInput == 1
count = 0;
while count < 5
a = 10;
b = 20;
distance = (b-a).*rand(1,1) + a;
array(end+1) = distance;
count = count + 1;
if distance >= 15 && distance <= 18
score = score + 3;
elseif distance < 18
score = score + 1;
elseif distance < 15
score = score + 1;
end
end
elseif userInput == 2
elseif userInput == 3
elseif userInput == 4
else
printf("Sorry, we could not perform your test.");
end
fprintf("You travelled %g and your total score is: %u", array(end), score);
More Answers (0)
See Also
Categories
Find more on Parallel Computing Fundamentals 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!