Need to create an array that consists of 5 numbers found through a while loop
Show older comments
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
MATLAB doesn't support Python style vector.append() operations. What you're doing here is creating a new field called 'append' and adding the value of 'distance' to it.
Equivalent syntaxes could be:
array(end+1) = distance;
array = [array, distance];
Additionally, you're clearing the value of array on each loop, move the
array = [];
Expression outside the while loop.
Finally, remember that MATLAB indexes from 1, not 0. You initialize your counter to 0 then try to index a list with it, which will throw errors. Finally, it looks like the expression
distance1 = distance(count)
should be:
distance1 = array(count)
The way you've defined distacne means it will only ever have one element, while array will grow as the script progresses. You may also want to review how your print statement is working at the end, since it will print once per element in array the way it's written currently.
Jacob
on 20 Nov 2024
Walter Roberson
on 20 Nov 2024
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
More Answers (0)
Categories
Find more on Creating and Concatenating Matrices 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!